【问题标题】:Unable to get Search View items clickable in correct position无法在正确位置获得可点击的搜索视图项目
【发布时间】:2018-12-04 04:57:26
【问题描述】:
private ListView lvProduct;
private ListProductAdapter adapter;
private List<Product> mProductList;
private DatabaseHelper mDBHelper;     

adapter = new ListProductAdapter(this, mProductList);

    lvProduct.setAdapter(adapter);

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int 
        position2, long id) {

        }
    });

**这里是搜索查询列表器**

 @Override
 public boolean onQueryTextChange(String s) {
    s=s.toLowerCase();
    ArrayList<Product> newList = new ArrayList<>();
    for (Product listProductAdapter : mProductList){
        String name = listProductAdapter.getSearchName().toLowerCase();
        if(name.contains(s)){
            newList.add(listProductAdapter);
            ;
        }
    }
    adapter.setFilter(newList);
    return true;
}

这里是 ListAdapter 和 Filter 适配器

public ListProductAdapter(Context mContext, List<Product> mProductList) {
    this.mContext = mContext;
    this.mProductList = mProductList;
}


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


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

@Override
public long getItemId(int position2) {
    return mProductList.get(position2).getId();
}


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

    View v = View.inflate(mContext, R.layout.item_listview, null);
    v.setTag(mProductList.get(position2).getId());
    TextView tvName = (TextView)v.findViewById(R.id.tv_product_name);
    tvName.setText(mProductList.get(position2).getName());
    return v;
}

 public void setFilter(ArrayList<Product> newList){
    mProductList = new ArrayList<>();
    mProductList.addAll(newList);
    notifyDataSetChanged();
}

我想用 mProductList.get(position2).getId() 获得点击的项目 我正在为 searchview 获得一个排序的新适配器,但我正在获得以前的适配器位置 ID。但我想要一个更新的列表视图位置 ID 和更新的列表视图适配器。请帮助我。

【问题讨论】:

    标签: java android arrays searchview baseadapter


    【解决方案1】:

    您需要清除旧列表并添加新项目.. 试试这个,

        public void setFilter(ArrayList<Product> newList){
        mProductList.clear();
        mProductList.addAll(newList);
        notifyDataSetChanged();
    }
    

    【讨论】:

    • list 卡住了更新的列表内容,但它没有再次返回所有列表视图元素。你能帮我解决这个问题吗?
    • @AlfredKommina 如果查询字符串为空,您将从活动中添加列表对象以传递适配器调用集过滤方法
    • 我没找到你@GobuCSG 对不起。如果 setfilter 在搜索过滤器中有两个元素,则过滤器正在使用两个元素更新 mProduct 列表(列表视图在搜索后仅填充两个元素)。关闭搜索栏后我无法获取所有元素。
    • @Alfred-kommina 这样做,@Override public boolean onQueryTextChange(String s) { s=s.toLowerCase(); if(!TextUtils.isEmpty(s)){} ArrayList&lt;Product&gt; newList = new ArrayList&lt;&gt;(); for (Product listProductAdapter : mProductList){ String name = listProductAdapter.getSearchName().toLowerCase(); if(name.contains(s)){ newList.add(listProductAdapter); ; } } adapter.setFilter(newList); }else{ adapter.setFilter(mProductList); } return true; }
    猜你喜欢
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    相关资源
    最近更新 更多