我是 Android 的初学者,但我尝试进行自定义listview
过滤,但它以某种方式工作。我唯一的问题是ArrayList
我有图像视图,当我搜索列表时它可以工作,但图像显示错误。我无法解释这一点,但我认为你可以以某种方式帮助我。
无论如何,这里是自定义ArrayAdaptor
:
public class Listadapter extends ArrayAdapter<HashMap<String, String>> {
ArrayList<HashMap<String, String>> originalList;
ArrayList<HashMap<String, String>> productlist;
private ProductFilter filter;
public Listadapter(Context context, int textViewResourceId,
ArrayList<HashMap<String, String>> Strings) {
super(context, textViewResourceId, Strings);
this.productlist = new ArrayList<HashMap<String, String>>();
this.productlist.addAll(productList);
this.originalList = new ArrayList<HashMap<String, String>>();
this.originalList.addAll(productList);
}
@Override
public Filter getFilter() {
if (filter == null) {
filter = new ProductFilter();
}
return filter;
}
/*private class ViewHolder {
TextView txtprodName;
TextView txtcategory;
TextView txtOfferDate;
ImageView ProductImage;
}*/
public int getCount() {
return productList.size();
}
public long getItemId(int position) {
// return productList.indexOf(getItem(position));
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//ViewHolder holder = null;
TextView txtprodName, txtcategory, txtOfferDate;
ImageView ProductImage;
Log.v("ConvertView", String.valueOf(position));
//if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.product_list_item, null);
//holder = new ViewHolder();
txtprodName = (TextView) convertView
.findViewById(R.id.txtprodName);
txtcategory = (TextView) convertView
.findViewById(R.id.txtcategory);
txtOfferDate = (TextView) convertView
.findViewById(R.id.txtOfferDate);
ProductImage = (ImageView) convertView
.findViewById(R.id.ProductImage);
//convertView.setTag(holder);
/*} else {
//holder = (ViewHolder) convertView.getTag();
}*/
HashMap<String, String> hm = productList.get(position);
// txtUserName.setText(lstUsers.get(position).getFirst_Name()+" "+lstUsers.get(position).getLast_Name());
txtprodName.setText(hm.get(TAG_PRODUCT_NAME));
txtcategory.setText(hm.get(TAG_CATEGORY_NAME));
txtOfferDate.setText(hm.get(TAG_OFFER_START_TIME));
if (drawable.get(position) != null)
ProductImage.setImageDrawable(drawable.get(position));
else
ProductImage.setImageResource(R.drawable.nopic_place);
}
return convertView;
}
private class ProductFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
if (constraint != null && constraint.toString().length() > 0) {
ArrayList<HashMap<String, String>> filteredItems =
new ArrayList<HashMap<String, String>>();
for (int i = 0, l = originalList.size(); i < l; i++) {
// ArrayList<HashMap<String, String>> p =
// originalList.get(i);
HashMap<String, String> p = originalList.get(i);
if (p.toString().toLowerCase().contains(constraint))
filteredItems.add(p);
}
result.count = filteredItems.size();
result.values = filteredItems;
}
else {
synchronized (this) {
result.values = originalList;
result.count = originalList.size();
}
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// TODO Auto-generated method stub
productList = (ArrayList<HashMap<String, String>>) results.values;
notifyDataSetChanged();
clear();
for (int i = 0, l = productList.size(); i < l; i++)
add(productList.get(i));
notifyDataSetInvalidated();
}
}
以下是结果