单击项目时,筛选器列表视图无法获得正确的位置

时间:2017-08-24 12:21:58

标签: java android android-layout listview

当搜索过滤器列表中的列表视图数据时工作正常但是当我点击过滤器列表数据时没有得到列表上数据的位置,例如,如果我有列表中的A,B,C数据,则搜索B它的工作正常但是当我点击B打开A活动而不是B活动时,请提前帮助感谢这里是我的代码

 @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;
        }

    });

1 个答案:

答案 0 :(得分:0)

“onItemClick”返回已过滤列表中元素的位置,但您使用此位置从原始列表中获取该项目。尝试这样的事情:

  

myintent.putExtra( “位置”,initialList.indexOf(restaurantadapter.getItem(位置)));