There are many similar questions asked but none of them were helpful to me... i m filtering the list based on whole word but the list is not getting refreshed and not displayed.Below is my listview and filter code...so plz check out... thanks in advance
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();
}
}
}
By default ArrayAdapter.getFilter() returns a filter called ArrayFilter that is doing something similar.
Description of this filter says:
So just have a look in there(grepcode) how it should be implemented. Maybe you don't actually need to implement it by your own. If you do, just copy - paste the code and modify to solve all compilation issues.