我正在尝试过滤自定义listview适配器,但效果不佳。
我正在获取所有值(在过滤后,在publishResults()方法中),但它不会影响我的列表视图。 我的适配器类中的错误在哪里。
这是ModelFilter所在的Adapter类Mlt_sixTextviewAdapter.java 要执行过滤操作的类。 执行过滤操作后,我在publishResults
中获得了过滤值 public class Mlt_sixTextviewAdapter extends ArrayAdapter<MLT_Details> implements
Filterable {
List<MLT_Details> alllst;
List<MLT_Details> list;
List<MLT_Details> filterlst;
Context context;
TextView txtitem;
TextView txtdesc;
TextView txtfrom;
TextView txtto;
TextView txtrecqty;
TextView txtuom;
private ModelFilter filter;
@Override
public Filter getFilter() {
if (filter == null) {
filter = new ModelFilter();
}
return filter;
}
public Mlt_sixTextviewAdapter(Context context, List<MLT_Details> value) {
super(context, R.layout.eight_textview, value);
this.context = context;
this.list = value;
this.alllst = new ArrayList<MLT_Details>(list);
this.filterlst = new ArrayList<MLT_Details>(alllst);
getFilter();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos = position;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.eight_textview, null);
txtitem = (TextView) convertView.findViewById(R.id.txt_fullItemNo);
txtdesc = (TextView) convertView.findViewById(R.id.txt_fullDesc);
txtfrom = (TextView) convertView.findViewById(R.id.txt_fullLoc);
txtto = (TextView) convertView.findViewById(R.id.txt_fullord);
txtrecqty = (TextView) convertView
.findViewById(R.id.txt_fullrecqty);
txtuom = (TextView) convertView.findViewById(R.id.txt_fullUom);
convertView.setTag(new OrdersViewHolder(txtitem, txtdesc, txtfrom,
txtto, txtrecqty, txtuom));
} else {
OrdersViewHolder viewHolder = (OrdersViewHolder) convertView
.getTag();
}
OrdersViewHolder holder = (OrdersViewHolder) convertView.getTag();
holder.txtitem.setText(alllst.get(pos).getMlt_itemno());
holder.txtdesc.setText(alllst.get(pos).getMlt_description());
holder.txtfrom.setText(alllst.get(pos).getMlt_from());
holder.txtto.setText((alllst.get(pos).getMlt_to()));
holder.txtrecqty.setText((alllst.get(pos).getMlt_qty()));
holder.txtuom.setText(alllst.get(pos).getMlt_uom());
return convertView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return filterlst.size();
}
@Override
public MLT_Details getItem(int position) {
// TODO Auto-generated method stub
return filterlst.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
/** Holds child views for one row. */
static class OrdersViewHolder {
TextView txtitem;
TextView txtdesc;
TextView txtfrom;
TextView txtto;
TextView txtrecqty;
TextView txtuom;
/*
* TextView txtshipvia; TextView txtcomment;
*/
public OrdersViewHolder(TextView txtitem, TextView txtdesc,
TextView txtpick, TextView txtordqty, TextView txtrecqty,
TextView txtuom) {
// TODO Auto-generated constructor stub
this.txtitem = txtitem;
this.txtdesc = txtdesc;
this.txtfrom = txtpick;
this.txtto = txtordqty;
this.txtrecqty = txtrecqty;
this.txtuom = txtuom;
}
public TextView getTxtitem() {
return txtitem;
}
public TextView getTxtdesc() {
return txtdesc;
}
public TextView getTxtfrom() {
return txtfrom;
}
public TextView getTxtto() {
return txtto;
}
public TextView getTxtrecqty() {
return txtrecqty;
}
public TextView getTxtuom() {
return txtuom;
}
public void setTxtitem(TextView txtitem) {
this.txtitem = txtitem;
}
public void setTxtdesc(TextView txtdesc) {
this.txtdesc = txtdesc;
}
public void setTxtfrom(TextView txtfrom) {
this.txtfrom = txtfrom;
}
public void setTxtto(TextView txtto) {
this.txtto = txtto;
}
public void setTxtrecqty(TextView txtrecqty) {
this.txtrecqty = txtrecqty;
}
public void setTxtuom(TextView txtuom) {
this.txtuom = txtuom;
}
}
private class ModelFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
if (constraint != null && constraint.toString().length() > 0) {
ArrayList<MLT_Details> filteredItems = new ArrayList<MLT_Details>();
for (int i = 0, l = alllst.size(); i < l; i++) {
MLT_Details customer = alllst.get(i);
String strNum = customer.getMlt_itemno();// tHhCustomer_number();
String strDesc = customer.getMlt_description();
if (strNum.toLowerCase().contains(constraint))
/* || strDesc.toLowerCase().contains(constraint)) */
filteredItems.add(customer);
}
result.count = filteredItems.size();
result.values = filteredItems;
} else {
synchronized (this) {
result.values = alllst;// ModelItemsArray;
result.count = alllst.size();
}
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
filterlst = (ArrayList<MLT_Details>) results.values;
if (results.count == 0)
notifyDataSetInvalidated();
else
for (int i = 0, l = filterlst.size(); i < l; i++) {
add(filterlst.get(i));
notifyDataSetChanged();
System.out.println("Fileter Item coming"
+ filterlst.get(i).getMlt_itemno());
}
}
}
}