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。请帮助我。
答案 0 :(得分:0)
您需要清除旧列表并添加新项目。 试试这个,
public void setFilter(ArrayList<Product> newList){
mProductList.clear();
mProductList.addAll(newList);
notifyDataSetChanged();
}