0

我有一个带有列表和 EditText 的 Activity,EditText 用于输入字符串,以便列表包含该字符串。ListView 基于 BaseAdapter,包含 F 类型的元素。

public class lstAdaptF extends BaseAdapter implements Filterable {
Context ctx;
LayoutInflater lInflater;
ArrayList<F> objectsF;
ArrayList<F> origobjectsF;
...
private Filter FirmaBBFilter;

lstAdaptF(Context context, ArrayList<F> f) {
    ctx = context;
    objectsF = f;
    origobjectsF = f;
    lInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
...
@Override
public Filter getFilter() {
    if (FFilter == null)
        FFilter = new FFilter();
    return FFilter;
}

...
private class FFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        if (constraint == null || constraint.length() == 0) {
            //get all inital list
            results.values = origobjectsF;
            results.count = origobjectsF.size();
            Log.d("lstAdaptF","empty filter");
        } else {
            // We perform filtering operation
            ArrayList<F> nF = new ArrayList<F>();

            for (F p : origobjectsF) {
                if (p.getName().toUpperCase().contains(constraint.toString().toUpperCase())){
                    nF.add(p);

                }
            }
            results.values = nF;
            results.count = nF.size();
        }
        return results;
    }

一切正常,例如列表包含字符串

Awork
Aworth
winter

我输入“w”:Awork、Aworth、winter

我输入“wo”:Awork,Aworth

我输入“woo”:Awork,Aworth - 列表仍然相同,但它应该是空的!我该如何处理这种情况?谢谢你。

升级版:

@Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        // Now we have to inform the adapter about the new list filtered
        if (results.count == 0)
            notifyDataSetInvalidated();
        else {          
            objectsF = (ArrayList<F>) results.values;
            notifyDataSetChanged();

        }
    }

UPD2:

@Override
public Object getItem(int position) {
    return objectsF.get(position);
}

 @Override
public int getCount() {
    return objectsF.size();
} 
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;
    FHolder holder = new FHolder();

    if (view == null) {
        view = lInflater.inflate(R.layout.itemf, parent, false);
        TextView tvAF = (TextView) view.findViewById(R.id.tvA);
...
        holder.tvAF = tvAF;
...
        view.setTag(holder);
    }
    else 
        holder = (FHolder) view.getTag();

    F p = getF(position);
    holder.tvAF.setText(p.a + "");
    ..
    return view;
}
4

1 回答 1

1

好像有什么问题notifyDataSetInvalidated()。尝试删除这个if-else

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
    // Now we have to inform the adapter about the new list filtered       
    objectsF = (ArrayList<F>) results.values;
    notifyDataSetChanged();
}
于 2013-02-16T18:12:21.670 回答