Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. public class AnimalNames {
  2. private String animalName;
  3. private String country;
  4. private Integer population;
  5. private Integer population_a;
  6. public AnimalNames(String country, String animalName, Integer population, Integer population_a) {
  7. this.animalName = animalName;
  8. this.country = country;
  9. this.population = population;
  10. this.population_a = population_a;
  11.  
  12. }
  13.  
  14. public String getAnimalName() {
  15. return this.animalName;
  16. }
  17. public String getCountry() {
  18. return this.country;
  19. }
  20. public Integer getPopulation() {
  21. return this.population;
  22. }
  23. public Integer getPopulation_a() {
  24. return this.population_a;
  25. }
  26.  
  27. }
  28.  
  29. public class ListViewAdapter extends BaseAdapter{
  30.  
  31. Context mContext;
  32. LayoutInflater inflater;
  33. private List<AnimalNames> animalNamesList = null;
  34. private ArrayList<AnimalNames> arraylist;
  35. public ListViewAdapter(Context context, List<AnimalNames> animalNamesList) {
  36. mContext = context;
  37. this.animalNamesList = animalNamesList;
  38. inflater = LayoutInflater.from(mContext);
  39. this.arraylist = new ArrayList<AnimalNames>();
  40. this.arraylist.addAll(animalNamesList);
  41. }
  42.  
  43. public class ViewHolder {
  44. TextView name;
  45. TextView tema;
  46. ImageView imgid;
  47. ImageView imgid1;
  48. Button button;
  49. }
  50.  
  51. @Override
  52. public int getCount() {
  53. return animalNamesList.size();
  54. }
  55.  
  56. @Override
  57. public AnimalNames getItem(int position) {
  58. return animalNamesList.get(position);
  59. }
  60.  
  61. @Override
  62. public long getItemId(int position) {
  63. return position;
  64. }
  65. public View getView(final int position, View view, ViewGroup parent) {
  66. final ViewHolder holder;
  67. if (view == null) {
  68. holder = new ViewHolder();
  69. view = inflater.inflate(R.layout.content_main, null,true);
  70. // Locate the TextViews in listview_item.xml
  71. holder.name = (TextView) view.findViewById(R.id.name_tema);
  72. holder.tema = (TextView) view.findViewById(R.id.txtStatusMsg);
  73. holder.imgid = (ImageView) view.findViewById(R.id.profilePic);
  74. holder.imgid1 = (ImageView) view.findViewById(R.id.profile);
  75. view.setTag(holder);
  76. } else {
  77. holder = (ViewHolder) view.getTag();
  78. }
  79. // Set the results into TextViews
  80. holder.name.setText(animalNamesList.get(position).getAnimalName());
  81. holder.tema.setText(animalNamesList.get(position).getCountry());
  82. holder.imgid.setImageResource(animalNamesList.get(position).getPopulation());
  83. holder.imgid1.setImageResource(animalNamesList.get(position).getPopulation_a());
  84. Button button = (Button) view.findViewById(R.id.butt);
  85. button.setClickable(false);
  86. button.setFocusable(false);
  87. return view;
  88. }
  89.  
  90. // Filter Class
  91. public void filter(String charText) {
  92. charText = charText.toLowerCase(Locale.getDefault());
  93. animalNamesList.clear();
  94. if (charText.length() == 0) {
  95. animalNamesList.addAll(arraylist);
  96. } else {
  97. for (AnimalNames wp : arraylist) {
  98. if (wp.getAnimalName().toLowerCase(Locale.getDefault()).contains(charText)
  99. ||wp.getCountry().toLowerCase(Locale.getDefault()).contains(charText) ) {
  100. animalNamesList.add(wp);
  101. }
  102. }
  103. }
  104. notifyDataSetChanged();
  105. }
  106.  
  107.  
  108. }
  109.  
  110. list = (ListView) findViewById(R.id.list);
  111. for (int i = 0; i < animalNameList.length; i++) {
  112. AnimalNames animalNames = new AnimalNames(animalNameList[i],country[i],population[i],population_a[i]);
  113. // Binds all strings into an array
  114. arraylist.add(animalNames);
  115. }
  116.  
  117. // Pass results to ListViewAdapter Class
  118. adapter = new ListViewAdapter(this, arraylist);
  119.  
  120. // Binds the Adapter to the ListView
  121. list.setAdapter(adapter);
  122. list.setOnItemClickListener(new AdapterView.OnItemClickListener()
  123. {
  124. public void onItemClick(AdapterView<?> parent, View view, int position, final long id) {
  125.  
  126. }
  127. });
  128.  
  129. // Locate the EditText in listview_main.xml
  130. editsearch = (SearchView) findViewById(R.id.search_view);
  131. editsearch.setOnQueryTextListener(this);
  132. @Override
  133. public boolean onQueryTextSubmit(String query) {
  134. return false;
  135. }
  136.  
  137. @Override
  138. public boolean onQueryTextChange(String newText) {
  139. String text = newText;
  140. adapter.filter(text);
  141. return false;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement