Advertisement
Guest User

Untitled

a guest
Dec 26th, 2012
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. package com.m7.nomad;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import android.content.Context;
  7. import android.util.Log;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.Filter;
  13. import android.widget.Filterable;
  14. import android.widget.ImageView;
  15. import android.widget.TextView;
  16.  
  17. public class PlacesListAdapter extends ArrayAdapter<Place> implements
  18. Filterable {
  19.  
  20. public Context mContext;
  21.  
  22. List<Place> mPlaces, orig;
  23.  
  24. public PlacesListAdapter(Context context, int resource, List<Place> places) {
  25. super(context, resource, places);
  26. // TODO Auto-generated constructor stub
  27. this.mPlaces = places;
  28. this.mContext = context;
  29.  
  30. orig = new ArrayList<Place>(this.mPlaces);
  31.  
  32. }
  33.  
  34. @Override
  35. public Place getItem(int position) {
  36. // TODO Auto-generated method stub
  37. return mPlaces.get(position);
  38. }
  39.  
  40. @Override
  41. public long getItemId(int position) {
  42. return position;
  43. }
  44.  
  45. @Override
  46. public View getView(int position, View convertView, ViewGroup parent) {
  47.  
  48. ViewHolder holder;
  49.  
  50. if (convertView == null) {
  51. LayoutInflater viewInflater;
  52. viewInflater = LayoutInflater.from(getContext());
  53. convertView = viewInflater.inflate(R.layout.list_item_places, null);
  54.  
  55. holder = new ViewHolder();
  56. holder.placeTitle = (TextView) convertView
  57. .findViewById(R.id.place_title);
  58. holder.placeDistance = (TextView) convertView
  59. .findViewById(R.id.place_distance);
  60. holder.placeCategoryIcon = (ImageView) convertView
  61. .findViewById(R.id.place_category_icon);
  62.  
  63. convertView.setTag(holder);
  64.  
  65. } else {
  66. holder = (ViewHolder) convertView.getTag();
  67. }
  68.  
  69. Log.i("Nomad", mPlaces.get(position).getPlaceName());
  70. holder.placeTitle.setText(mPlaces.get(position).getPlaceName());
  71. holder.placeDistance.setText(mPlaces.get(position).getPlaceDistance());
  72. holder.placeCategoryIcon
  73. .setImageResource(R.drawable.icon_category_hillstation);
  74.  
  75. // // Setting Alternative Row Colors
  76. if (position % 2 == 0) {
  77. convertView
  78. .setBackgroundResource(R.drawable.list_view_places_row_1);
  79. } else {
  80. convertView
  81. .setBackgroundResource(R.drawable.list_view_places_row_2);
  82. }
  83.  
  84. return convertView;
  85. }
  86.  
  87. static class ViewHolder {
  88. TextView placeId;
  89.  
  90. TextView placeTitle;
  91.  
  92. TextView placeDistance;
  93.  
  94. ImageView placeCategoryIcon;
  95. }
  96.  
  97. @Override
  98. public Filter getFilter() {
  99. // TODO Auto-generated method stub
  100. return new PlaceFilter();
  101. }
  102.  
  103. private class PlaceFilter extends Filter {
  104.  
  105. @Override
  106. protected FilterResults performFiltering(CharSequence constraint) {
  107.  
  108. constraint = constraint.toString().toLowerCase();
  109. FilterResults result = new FilterResults();
  110.  
  111. if (constraint != null && constraint.toString().length() > 0) {
  112.  
  113. List<Place> founded = new ArrayList<Place>();
  114. for (Place item : mPlaces) {
  115. if (item.toString().toLowerCase().contains(constraint)) {
  116. founded.add(item);
  117. }
  118. }
  119.  
  120. result.values = founded;
  121. result.count = founded.size();
  122. } else {
  123. result.values = orig;
  124. result.count = orig.size();
  125. }
  126. return result;
  127. }
  128.  
  129. @Override
  130. protected void publishResults(CharSequence constraint,
  131. FilterResults results) {
  132. clear();
  133. for (Place item : (List<Place>) results.values) {
  134. add(item);
  135. }
  136. notifyDataSetChanged();
  137. }
  138.  
  139. }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement