Android 使用筛选器时未刷新ListView
有许多类似的问题问,但没有一个对我有帮助。。。我正在根据整个单词筛选列表,但列表没有刷新和显示。下面是我的listview和筛选代码…请签出。。。提前谢谢Android 使用筛选器时未刷新ListView,android,listview,android-filter,Android,Listview,Android Filter,有许多类似的问题问,但没有一个对我有帮助。。。我正在根据整个单词筛选列表,但列表没有刷新和显示。下面是我的listview和筛选代码…请签出。。。提前谢谢 public class MyInventoryAdapter extends ArrayAdapter<ProductInventoryItem> implements OnClickListener { private final Context context; ArrayList<ProductInvent
public class MyInventoryAdapter extends ArrayAdapter<ProductInventoryItem>
implements OnClickListener {
private final Context context;
ArrayList<ProductInventoryItem> itemsArrayList = new ArrayList<ProductInventoryItem>();
private ArrayList<ProductInventoryItem> originalList;
private MFilter filter;
public MyInventoryAdapter(Context context,
ArrayList<ProductInventoryItem> itemsArrayList) {
super(context, R.layout.custom_product_inventory, itemsArrayList);
this.context = context;
this.itemsArrayList = itemsArrayList;
this.originalList = new ArrayList<ProductInventoryItem>();
this.originalList.addAll(itemsArrayList);
}
@Override
public Filter getFilter() {
if (filter == null) {
filter = new MFilter();
}
return filter;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View convertView1 = inflater.inflate(R.layout.custom_product_inventory,
parent, false);
// 3. Get the two text view from the rowView
LinearLayout llview = (LinearLayout) convertView1
.findViewById(R.id.inventory_row);
TextView txtpname = (TextView) convertView1
.findViewById(R.id.tvProductName);
TextView txtpdesc = (TextView) convertView1
.findViewById(R.id.tvProductDesc);
TextView txtpcost = (TextView) convertView1
.findViewById(R.id.tvProductPrice1);
TextView txtcat = (TextView) convertView1
.findViewById(R.id.tvProductcat);
TextView txtsubcat = (TextView) convertView1
.findViewById(R.id.tvProductsubcat);
// holder.btn_edit = (ImageButton)
// convertView.findViewById(R.id.imageButton1);
ImageView imageView = (ImageView) convertView1
.findViewById(R.id.list_image);
ImageView chk_check = (ImageView) convertView1.findViewById(R.id.check);
// 4. Set the text for textView
final ProductInventoryItem rowItem = (ProductInventoryItem) getItem(position);
txtpname.setText(rowItem.getPname());
txtpdesc.setText(rowItem.getPdesc());
txtpcost.setText(rowItem.getPcost());
txtcat.setText(rowItem.getCat());
txtsubcat.setText(rowItem.getSubcat());
boolean status = rowItem.isStatus();
String url = "null";// rowItem.getImgurl();
chk_check.setTag(rowItem);
if (status) {
chk_check.setImageResource(R.drawable.ic_check_mark);
llview.setBackgroundResource(R.drawable.gradient_bg_hover);
} else {
chk_check.setImageResource(R.drawable.ic_add_black_old);
llview.setBackgroundResource(R.drawable.gradient_bg);
}
if (url.equalsIgnoreCase("null")) {
// System.out.println("myadap:" + url);
imageView.setImageResource(R.drawable.ic_camera);
} else {
Bitmap bitmap, bit;
bitmap = BitmapFactory.decodeFile(url);
bit = Bitmap.createScaledBitmap(bitmap, 75, 75, true);
imageView.setImageBitmap(bit);
}
chk_check.setOnClickListener(this);
// 5. retrn rowView
return convertView1;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ProductInventoryItem item = (ProductInventoryItem) v.getTag();
String pid = item.getPid();
System.out.println("pid" + pid);
if (!item.isStatus()) {
item.setStatus(true);
} else {
item.setStatus(false);
}
this.notifyDataSetChanged();
}
class MFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString();
FilterResults result = new FilterResults();
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
result.values = originalList;
result.count = originalList.size();
} else {
// We perform filtering operation
ArrayList<ProductInventoryItem> nItemList = new ArrayList<ProductInventoryItem>();
for (ProductInventoryItem listp : originalList) {
if (listp.getCat().toUpperCase()
.startsWith(constraint.toString().toUpperCase())
|| listp.getSubcat()
.toUpperCase()
.startsWith(
constraint.toString().toUpperCase()))
nItemList.add(listp);
}
result.values = nItemList;
result.count = nItemList.size();
System.out.println(nItemList.size());
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
itemsArrayList = (ArrayList<ProductInventoryItem>)results.values;
notifyDataSetChanged();
clear();
for(int i = 0, l = itemsArrayList.size(); i < l; i++)
add(itemsArrayList.get(i));
notifyDataSetInvalidated();
}
}
}
默认情况下,返回一个名为ArrayFilter的过滤器,该过滤器正在执行类似的操作
此过滤器的说明如下:
数组筛选器使用
前缀不以提供的前缀开头的每个项目都是
从列表中删除
因此,只要看看grepcode中应该如何实现它就可以了。也许你实际上不需要自己去实现它。如果这样做,只需复制粘贴代码并进行修改即可解决所有编译问题