Android source code: Get a list of cities across the country similar to contact sorting

Android source code: Get a list of cities across the country similar to contact sorting

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

  1. package com.liucanwen.citylist;
  2.   
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.   
  6. import android.app.Activity;
  7. import android.content.Context;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10. import android.text.Editable;
  11. import android.text.TextWatcher;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.widget.AdapterView;
  15. import android.widget.EditText;
  16. import android.widget.Toast;
  17.   
  18. import com.liucanwen.citylist.adapter.CityAdapter;
  19. import com.liucanwen.citylist.data.CityData;
  20. import com.liucanwen.citylist.model.CityItem;
  21. import com.liucanwen.citylist.widget.ContactItemInterface;
  22. import com.liucanwen.citylist.widget.ContactListViewImpl;
  23.   
  24. public   class CityListActivity extends Activity implements TextWatcher
  25. {
  26. private Context context_ = CityListActivity.this ;
  27.   
  28. private ContactListViewImpl listview;
  29.   
  30. private EditText searchBox;
  31. private String searchString;
  32.   
  33. private Object searchLock = new Object();
  34. boolean inSearchMode = false ;
  35.   
  36. private   final   static String TAG = "MainActivity2" ;
  37.   
  38. List<contactiteminterface> contactList;
  39. List<contactiteminterface> filterList;
  40. private SearchListTask curSearchTask = null ;
  41.   
  42. @Override  
  43. public   void onCreate(Bundle savedInstanceState)
  44. {
  45. super .onCreate(savedInstanceState);
  46. setContentView(R.layout.citylist);
  47.   
  48. filterList = new ArrayList<contactiteminterface>();
  49. contactList = CityData.getSampleContactList();
  50.   
  51. CityAdapter adapter = new CityAdapter( this ,R.layout.city_item, contactList);
  52.   
  53. listview = (ContactListViewImpl) this .findViewById(R.id.listview);
  54. listview.setFastScrollEnabled( true );
  55. listview.setAdapter(adapter);
  56.   
  57. listview.setOnItemClickListener( new AdapterView.OnItemClickListener()
  58. {
  59. @Override  
  60. public   void onItemClick(AdapterView parent, View v, int position,
  61. long id)
  62. {
  63. List<contactiteminterface> searchList = inSearchMode? filterList
  64. : contactList;
  65.   
  66. Toast.makeText(context_,
  67. searchList.get(position).getDisplayInfo(),
  68. Toast.LENGTH_SHORT).show();
  69. }
  70. });
  71.   
  72. searchBox = (EditText) findViewById(R.id.input_search_query);
  73. searchBox.addTextChangedListener( this );
  74. }
  75.   
  76. @Override  
  77. public   void afterTextChanged(Editable s)
  78. {
  79. searchString = searchBox.getText().toString().trim().toUpperCase();
  80.   
  81. if (curSearchTask != null  
  82. && curSearchTask.getStatus() != AsyncTask.Status.FINISHED)
  83. {
  84. try  
  85. {
  86. curSearchTask.cancel( true );
  87. } catch (Exception e)
  88. {
  89. Log.i(TAG, "Fail to cancel running search task" );
  90. }
  91.   
  92. }
  93. curSearchTask = new SearchListTask();
  94. curSearchTask.execute(searchString);
  95. }
  96.   
  97. @Override  
  98. public   void beforeTextChanged(CharSequence s, int start, int count,
  99. int after)
  100. {
  101. }
  102.   
  103. @Override  
  104. public   void onTextChanged(CharSequence s, int start, int before, int count)
  105. {
  106. // do nothing  
  107. }
  108.   
  109. private   class SearchListTask extends AsyncTask<string, void ,= "" string= "" >
  110. {
  111.   
  112. @Override  
  113. protected String doInBackground(String... params)
  114. {
  115. filterList.clear();
  116.   
  117. String keyword = params[ 0 ];
  118.   
  119. inSearchMode = (keyword.length() > 0 );
  120.   
  121. if (inSearchMode)
  122. {
  123. // get all the items matching this  
  124. for (ContactItemInterface item : contactList)
  125. {
  126. CityItem contact = (CityItem) item;
  127.   
  128. boolean isPinyin = contact.getFullName().toUpperCase().indexOf(keyword) > - 1 ;
  129. boolean isChinese = contact.getNickName().indexOf(keyword) > - 1 ;
  130.   
  131. if (isPinyin || isChinese)
  132. {
  133. filterList.add(item);
  134. }
  135.   
  136. }
  137.   
  138. }
  139. return   null ;
  140. }
  141.   
  142. protected   void onPostExecute(String result)
  143. {
  144.   
  145. synchronized (searchLock)
  146. {
  147.   
  148. if (inSearchMode)
  149. {
  150.   
  151. CityAdapter adapter = new CityAdapter(context_,R.layout.city_item, filterList);
  152. adapter.setInSearchMode( true );
  153. listview.setInSearchMode( true );
  154. listview.setAdapter(adapter);
  155. } else  
  156. {
  157. CityAdapter adapter = new CityAdapter(context_,R.layout.city_item, contactList);
  158. adapter.setInSearchMode( false );
  159. listview.setInSearchMode( false );
  160. listview.setAdapter(adapter);
  161. }
  162. }
  163.   
  164. }
  165. }
  166.   
  167. }
  168. </string,></contactiteminterface></contactiteminterface></contactiteminterface></contactiteminterface>

Source code download address: http://down..com/data/1977281

<<:  Android source code download: Sina Weibo

>>:  Using pointers in Swift

Recommend

Don’t be confused! 4 career suggestions for novice B-side designers

I have found that many newcomers who join B-side ...

A brief analysis of the principles of mobile rendering

Author| Shang Huaijun Rendering on a computer or ...

Earthworms are good at splitting in two? My sea anemone can split in a hundred

As summer comes to an end, the best time of the y...

The battle for supremacy between Tencent and Alibaba—where has Baidu gone?

[[139349]] In 2015, the Chinese stock market was ...

Advanced Operations: 7 Habits for High-Performance Event Operations

The day before yesterday, I finished reading Step...

ZTE explains how to access the Internet on airplanes

Regarding the "Air China provides in-flight b...