Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.m7.nomad;
- import java.util.ArrayList;
- import java.util.List;
- import android.content.Context;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.Filter;
- import android.widget.Filterable;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class PlacesListAdapter extends ArrayAdapter<Place> implements
- Filterable {
- public Context mContext;
- List<Place> mPlaces, orig;
- public PlacesListAdapter(Context context, int resource, List<Place> places) {
- super(context, resource, places);
- // TODO Auto-generated constructor stub
- this.mPlaces = places;
- this.mContext = context;
- orig = new ArrayList<Place>(this.mPlaces);
- }
- @Override
- public Place getItem(int position) {
- // TODO Auto-generated method stub
- return mPlaces.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder;
- if (convertView == null) {
- LayoutInflater viewInflater;
- viewInflater = LayoutInflater.from(getContext());
- convertView = viewInflater.inflate(R.layout.list_item_places, null);
- holder = new ViewHolder();
- holder.placeTitle = (TextView) convertView
- .findViewById(R.id.place_title);
- holder.placeDistance = (TextView) convertView
- .findViewById(R.id.place_distance);
- holder.placeCategoryIcon = (ImageView) convertView
- .findViewById(R.id.place_category_icon);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- Log.i("Nomad", mPlaces.get(position).getPlaceName());
- holder.placeTitle.setText(mPlaces.get(position).getPlaceName());
- holder.placeDistance.setText(mPlaces.get(position).getPlaceDistance());
- holder.placeCategoryIcon
- .setImageResource(R.drawable.icon_category_hillstation);
- // // Setting Alternative Row Colors
- if (position % 2 == 0) {
- convertView
- .setBackgroundResource(R.drawable.list_view_places_row_1);
- } else {
- convertView
- .setBackgroundResource(R.drawable.list_view_places_row_2);
- }
- return convertView;
- }
- static class ViewHolder {
- TextView placeId;
- TextView placeTitle;
- TextView placeDistance;
- ImageView placeCategoryIcon;
- }
- @Override
- public Filter getFilter() {
- // TODO Auto-generated method stub
- return new PlaceFilter();
- }
- private class PlaceFilter extends Filter {
- @Override
- protected FilterResults performFiltering(CharSequence constraint) {
- constraint = constraint.toString().toLowerCase();
- FilterResults result = new FilterResults();
- if (constraint != null && constraint.toString().length() > 0) {
- List<Place> founded = new ArrayList<Place>();
- for (Place item : mPlaces) {
- if (item.toString().toLowerCase().contains(constraint)) {
- founded.add(item);
- }
- }
- result.values = founded;
- result.count = founded.size();
- } else {
- result.values = orig;
- result.count = orig.size();
- }
- return result;
- }
- @Override
- protected void publishResults(CharSequence constraint,
- FilterResults results) {
- clear();
- for (Place item : (List<Place>) results.values) {
- add(item);
- }
- notifyDataSetChanged();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement