简体   繁体   中英

filter list view not get the right position when click the item

when search the list view data in filter list working good but when i click on the filter list data not get the position on the data against the list for example if i have A,B,C data in list i search the B its working fine but when i click on B open the A activity not B activity please help thanks in advance here is my code

 @Override
public int getCount() {
    return models.size();
}

@Override
public Object getItem(int position) {
    return models.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = View.inflate(context, R.layout.listitem, null);
    }

    ImageView images = (ImageView) convertView.findViewById(R.id.imageView);
    TextView title = (TextView) convertView.findViewById(R.id.name);
    TextView about = (TextView) convertView.findViewById(R.id.desc);
    RatingBar ratingBar= (RatingBar) convertView.findViewById(R.id.rating);

    Restaurantmodel model = models.get(position);
    images.setImageResource(model.getImage());
    title.setText(model.getTitle());
    about.setText(model.getAbout());
    ratingBar.setRating(ratings[position]);
    return convertView;
}


@Override
public Filter getFilter() {
    if(filter == null)
    {
        filter=new CustomFilter();
    }
    return filter;
}

class CustomFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();

        if (constraint != null && constraint.length() > 0) {
            constraint = constraint.toString().toUpperCase();
            ArrayList<Restaurantmodel> filters = new ArrayList<Restaurantmodel>();
            for (int i = 0; i < filterList.size(); i++) {
                if (filterList.get(i).getTitle().toUpperCase().contains(constraint.toString().toUpperCase())) {
                    Restaurantmodel R = new Restaurantmodel(filterList.get(i).getImage(), filterList.get(i).getTitle(), filterList.get(i).getAbout() );
                    filters.add(R);
                }
            }
            results.values=filters;
            results.count=filters.size();

        }
        else
        {
            results.values=filterList;
            results.count=filterList.size();

        }
        return results;
    }

    @Override
    protected void publishResults (CharSequence constraint, FilterResults results){
        models=(ArrayList<Restaurantmodel>) results.values;
        notifyDataSetChanged();
    }
}


listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (position == 0) {
                Intent myintent = new Intent(Restaurant.this, NOSHAKH.class);
                myintent.putExtra("position",restaurantadapter.getItemId(position));
                startActivity(myintent);
            }
            if (position == 1) {
                Intent intent = new Intent(Restaurant.this, Barbqtonite.class);
                intent.putExtra("position", restaurantadapter.getItemId(position));
                startActivity(intent);
            }
        }
    });
  svR = (SearchView) findViewById(R.id.searchViewxray);
    svR.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String query) {
                restaurantadapter.getFilter().filter(query);
            return false;
        }
             @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

    });

The "onItemClick" returns the position of the element from the filtered list, but you use this position to get the item from the original list. Try something like this:

myintent.putExtra("position",initialList.indexOf(restaurantadapter.getItem(position)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM