Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. public class MainActivity extends Activity implements
  2. SearchView.OnQueryTextListener {
  3.  
  4. ListView lv;
  5. SearchView search_view;
  6.  
  7. String[] country_names, iso_codes;
  8. TypedArray country_flags;
  9.  
  10. ArrayList<Country> countrylist;
  11. CustomAdapter adapter;
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17.  
  18. lv = (ListView) findViewById(R.id.list_view);
  19. search_view = (SearchView) findViewById(R.id.search_view);
  20.  
  21. country_names = getResources().getStringArray(R.array.country_names);
  22. iso_codes = getResources().getStringArray(R.array.iso_Code);
  23. country_flags = getResources().obtainTypedArray(R.array.country_flags);
  24.  
  25. countrylist = new ArrayList<Country>();
  26. for (int i = 0; i < country_names.length; i++) {
  27. Country country = new Country(country_names[i], iso_codes[i],
  28. country_flags.getResourceId(i, -1));
  29. countrylist.add(country);
  30. }
  31.  
  32. adapter = new CustomAdapter(getApplicationContext(), countrylist);
  33. lv.setAdapter(adapter);
  34.  
  35. lv.setOnItemClickListener(new OnItemClickListener(){
  36.  
  37. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  38. long arg3) {
  39.  
  40.  
  41. **// I DON'T KNOW WHAT PUT HERE**
  42.  
  43.  
  44.  
  45.  
  46.  
  47. }
  48.  
  49. });
  50.  
  51. search_view.setOnQueryTextListener(this);
  52. }
  53.  
  54. @Override
  55. public boolean onQueryTextChange(String newText) {
  56. adapter.getFilter().filter(newText);
  57. return false;
  58. }
  59.  
  60. @Override
  61. public boolean onQueryTextSubmit(String query) {
  62. return false;
  63. }
  64.  
  65. }
  66.  
  67. String name;
  68. int flag;
  69. String iso_code;
  70.  
  71. Country(String name, String iso_code, int flag) {
  72. this.name = name;
  73. this.iso_code = iso_code;
  74. this.flag = flag;
  75. }
  76.  
  77. public String getIso_code() {
  78. return iso_code;
  79. }
  80.  
  81. public void setIso_code(String iso_code) {
  82. this.iso_code = iso_code;
  83. }
  84.  
  85. public String getName() {
  86. return name;
  87. }
  88.  
  89. public void setName(String name) {
  90. this.name = name;
  91. }
  92.  
  93. public int getFlag() {
  94. return flag;
  95. }
  96.  
  97. public void setFlag(int flag) {
  98. this.flag = flag;
  99. }
  100.  
  101. }
  102.  
  103. public class CustomAdapter extends BaseAdapter implements Filterable {
  104.  
  105. Context context;
  106. ArrayList<Country> countrylist;
  107. ArrayList<Country> mStringFilterList;
  108. ValueFilter valueFilter;
  109.  
  110. CustomAdapter(Context context, ArrayList<Country> countrylist) {
  111. this.context = context;
  112. this.countrylist = countrylist;
  113. mStringFilterList = countrylist;
  114. }
  115.  
  116. @Override
  117. public int getCount() {
  118. return countrylist.size();
  119. }
  120.  
  121. @Override
  122. public Object getItem(int position) {
  123. return countrylist.get(position);
  124. }
  125.  
  126. @Override
  127. public long getItemId(int position) {
  128. return countrylist.indexOf(getItem(position));
  129. }
  130.  
  131. @Override
  132. public View getView(int position, View convertView, ViewGroup parent) {
  133.  
  134. LayoutInflater mInflater = (LayoutInflater) context
  135. .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
  136.  
  137. convertView = null;
  138. if (convertView == null) {
  139. convertView = mInflater.inflate(R.layout.list_item, null);
  140.  
  141. TextView name_tv = (TextView) convertView.findViewById(R.id.name);
  142. TextView iso_tv = (TextView) convertView.findViewById(R.id.code);
  143. ImageView iv = (ImageView) convertView.findViewById(R.id.flag);
  144.  
  145. Country country = countrylist.get(position);
  146.  
  147. name_tv.setText(country.getName());
  148. iso_tv.setText(country.getIso_code());
  149. iv.setImageResource(country.getFlag());
  150. }
  151. return convertView;
  152. }
  153.  
  154. @Override
  155. public Filter getFilter() {
  156. if (valueFilter == null) {
  157. valueFilter = new ValueFilter();
  158. }
  159. return valueFilter;
  160. }
  161.  
  162. private class ValueFilter extends Filter {
  163. @Override
  164. protected FilterResults performFiltering(CharSequence constraint) {
  165. FilterResults results = new FilterResults();
  166.  
  167. if (constraint != null && constraint.length() > 0) {
  168. ArrayList<Country> filterList = new ArrayList<Country>();
  169. for (int i = 0; i < mStringFilterList.size(); i++) {
  170. if ((mStringFilterList.get(i).getName().toUpperCase())
  171. .contains(constraint.toString().toUpperCase())) {
  172.  
  173. Country country = new Country(mStringFilterList.get(i)
  174. .getName(), mStringFilterList.get(i)
  175. .getIso_code(), mStringFilterList.get(i)
  176. .getFlag());
  177.  
  178. filterList.add(country);
  179. }
  180. }
  181. results.count = filterList.size();
  182. results.values = filterList;
  183. } else {
  184. results.count = mStringFilterList.size();
  185. results.values = mStringFilterList;
  186. }
  187. return results;
  188.  
  189. }
  190.  
  191. @Override
  192. protected void publishResults(CharSequence constraint,
  193. FilterResults results) {
  194. countrylist = (ArrayList<Country>) results.values;
  195. notifyDataSetChanged();
  196. }
  197.  
  198. }
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement