功能分类:工具
支持平台:Android
运行环境:Eclipse
开发语言:Java
开发工具:Eclipse
源码大小:1.24MB
源码下载地址:http://down.51cto.com/data/1977281
源码简介
项目源地址:https://github.com/kk-java/ChineseCityList
中国城市列表,类似手机通讯录显示方式,可以通过触摸屏幕右边城市拼音首字母快速定位,还可通过中文、拼音快速查找。
** 修改demo在小米手机上排序错误的问题
需要的朋友可以参考一下!
如有侵权,请见谅..
源码运行截图
源码片段
- 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)
- {
- // do nothing
- }
- 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)
- {
- // get all the items matching this
- 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>