Functional classification: Tools Supported platforms: Android Operating environment: Eclipse Development language: Java Development tool: Eclipse Source code size: 1.24MB Source code download address: http://down..com/data/1977281 Source code introduction Project source address: https://github.com/kk-java/ChineseCityList The Chinese city list is displayed similarly to a mobile phone address book. You can quickly locate the city by touching the first letter of the city's pinyin on the right side of the screen, and you can also quickly search by Chinese or pinyin. ** Fixed the issue of demo sorting errors on Xiaomi phones Friends in need can refer to it! Please forgive me if there is any infringement.. Source code running screenshot Run screenshot Run screenshot Run screenshot
Source code snippet - package com.liucanwen.citylist;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import android.app.Activity;
- import android.content.Context;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.util.Log;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.EditText;
- import android.widget.Toast;
-
- import com.liucanwen.citylist.adapter.CityAdapter;
- import com.liucanwen.citylist.data.CityData;
- import com.liucanwen.citylist.model.CityItem;
- import com.liucanwen.citylist.widget.ContactItemInterface;
- import com.liucanwen.citylist.widget.ContactListViewImpl;
-
- public class CityListActivity extends Activity implements TextWatcher
- {
- private Context context_ = CityListActivity.this ;
-
- private ContactListViewImpl listview;
-
- private EditText searchBox;
- private String searchString;
-
- private Object searchLock = new Object();
- boolean inSearchMode = false ;
-
- private final static String TAG = "MainActivity2" ;
-
- List<contactiteminterface> contactList;
- List<contactiteminterface> filterList;
- private SearchListTask curSearchTask = null ;
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super .onCreate(savedInstanceState);
- setContentView(R.layout.citylist);
-
- filterList = new ArrayList<contactiteminterface>();
- contactList = CityData.getSampleContactList();
-
- CityAdapter adapter = new CityAdapter( this ,R.layout.city_item, contactList);
-
- listview = (ContactListViewImpl) this .findViewById(R.id.listview);
- listview.setFastScrollEnabled( true );
- listview.setAdapter(adapter);
-
- listview.setOnItemClickListener( new AdapterView.OnItemClickListener()
- {
- @Override
- public void onItemClick(AdapterView parent, View v, int position,
- long id)
- {
- List<contactiteminterface> searchList = inSearchMode? filterList
- : contactList;
-
- Toast.makeText(context_,
- searchList.get(position).getDisplayInfo(),
- Toast.LENGTH_SHORT).show();
- }
- });
-
- searchBox = (EditText) findViewById(R.id.input_search_query);
- searchBox.addTextChangedListener( this );
- }
-
- @Override
- public void afterTextChanged(Editable s)
- {
- searchString = searchBox.getText().toString().trim().toUpperCase();
-
- if (curSearchTask != null
- && curSearchTask.getStatus() != AsyncTask.Status.FINISHED)
- {
- try
- {
- curSearchTask.cancel( true );
- } catch (Exception e)
- {
- Log.i(TAG, "Fail to cancel running search task" );
- }
-
- }
- curSearchTask = new SearchListTask();
- curSearchTask.execute(searchString);
- }
-
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after)
- {
- }
-
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count)
- {
-
- }
-
- private class SearchListTask extends AsyncTask<string, void ,= "" string= "" >
- {
-
- @Override
- protected String doInBackground(String... params)
- {
- filterList.clear();
-
- String keyword = params[ 0 ];
-
- inSearchMode = (keyword.length() > 0 );
-
- if (inSearchMode)
- {
-
- for (ContactItemInterface item : contactList)
- {
- CityItem contact = (CityItem) item;
-
- boolean isPinyin = contact.getFullName().toUpperCase().indexOf(keyword) > - 1 ;
- boolean isChinese = contact.getNickName().indexOf(keyword) > - 1 ;
-
- if (isPinyin || isChinese)
- {
- filterList.add(item);
- }
-
- }
-
- }
- return null ;
- }
-
- protected void onPostExecute(String result)
- {
-
- synchronized (searchLock)
- {
-
- if (inSearchMode)
- {
-
- CityAdapter adapter = new CityAdapter(context_,R.layout.city_item, filterList);
- adapter.setInSearchMode( true );
- listview.setInSearchMode( true );
- listview.setAdapter(adapter);
- } else
- {
- CityAdapter adapter = new CityAdapter(context_,R.layout.city_item, contactList);
- adapter.setInSearchMode( false );
- listview.setInSearchMode( false );
- listview.setAdapter(adapter);
- }
- }
-
- }
- }
-
- }
- </string,></contactiteminterface></contactiteminterface></contactiteminterface></contactiteminterface>
Source code download address: http://down..com/data/1977281 |