Android - 自定义自定义适配器arraylist由过滤器arraylist替换

时间:2012-11-06 11:25:28

标签: android arraylist

我有一个自定义的ArrayAdaptor。 添加getFilter功能时,我意识到我的下一次搜索过滤项目越来越少。 即使我删除了EditText中的文本,它也只是我最后一次搜索记录。

prodlistrowadapter.java


public prodlistrowAdapter(Context context, int layoutResourceId,
        ArrayList<clsProducts> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.itemsdata = data;

    //Log.e("context", String.valueOf(data.size()));
    uploadedimgpath = context.getString(R.string.uploadedimgpath);
    //this.original = new ArrayList<clsProducts> data;
    originalItems = data;
}
static class ListHolder {
    ImageView imgIcon;
    TextView txtTitle;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    final clsProducts prod = getItem(position);

    final  ListHolder holder =  new ListHolder();
    try {

        // if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        // LayoutInflater inflater = LayoutInflater.from(context);
        row = inflater.inflate(layoutResourceId, parent, false);

        holder.imgIcon = (ImageView) row
                .findViewById(R.id.rowProdImageView);
        holder.txtTitle = (TextView) row.findViewById(R.id.rowProdTextView);
        // row.setTag(holder);
        // } else {
        // holder = (ListHolder) row.getTag();
        // }

        // clsProducts prod = items.get(position);

        // holder.imgIcon.setImageResource(R.drawable.a1326965813select);
        Drawable image = ImageOperations(context, uploadedimgpath
                + prod.image, "image.jpg");
        holder.imgIcon.setImageDrawable(image);
        holder.txtTitle.setText(prod.prod_name);

        row.setFocusable(false);
        row.setTag(prod.server_id);

        // row.setClickable(true);

        row.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                try {
                    Intent packlistIntent = new Intent(v.getContext(),
                            catelist.class);
                    packlistIntent.putExtra("prodidselected",
                            String.valueOf(v.getTag()));
                    v.getContext().startActivity(packlistIntent);
                } catch (Exception e) {
                    // Log.e("Exception - prodlistrowadapter",
                    // e.toString());
                }
            }
        });
    } catch (Exception ex) {
        // Log.e("prodlistrowadap", ex.toString());
    }
    return row;
}

@Override
public void add(clsProducts item) {
    itemsdata.add(item);
}

public Filter getFilter() {
    if (filter == null)
        filter = new PkmnNameFilter();
    return filter;
}

@Override
public int getCount() {
    synchronized (mLock) {
        return itemsdata.size();
    }
}
protected FilterResults performFiltering(CharSequence constraint) {
    FilterResults results = new FilterResults();
    String prefix = constraint.toString().toLowerCase();

    if (prefix == null || prefix.length() == 0) {
        synchronized (mLock) {
            results.values = itemsdata;
            results.count = itemsdata.size();
    }
    } else {

            synchronized (mLock) {
                //Log.e("second",prefix);
                final ArrayList<clsProducts> localItems = new ArrayList<clsProducts>();
                final ArrayList<clsProducts> filteredItems = new ArrayList<clsProducts>();
                localItems.addAll(itemsdata);
                final int count = localItems.size();
                for (int i = 0; i < count; i++) {
                    final clsProducts pkmn = localItems.get(i);
                    final String value = pkmn.getprodname().toLowerCase();

                    if (value.startsWith(prefix)) {
                        filteredItems.add(pkmn);
                    }
                }
                results.values = filteredItems;
                results.count = filteredItems.size();
            }
        }
        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {

        synchronized (mLock) {
            final ArrayList<clsProducts> localItems = (ArrayList<clsProducts>) results.values;
            notifyDataSetChanged();
            clear();
            for (clsProducts gi : localItems) {
                add(gi);
            }
        }
    }
}

catelist.java


List<clsProducts> prodList = cls.prodGetList(strSql);
ArrayList<clsProducts> proddata = (ArrayList<clsProducts>) prodList;
final prodlistrowAdapter adapter = new prodlistrowAdapter(this,                         R.layout.prodlistrow, proddata);
listView1 = (ListView) findViewById(R.id.lvCateList);       
listView1.setTextFilterEnabled(true);
listView1.setAdapter(adapter);
EditText etinputSearch = (EditText)findViewById(R.id.etinputSearch);
etinputSearch.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
          adapter.getFilter().filter(arg0);
          //adapter.notifyDataSetChanged();
   }

public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                        int arg3) {
    }
    public void afterTextChanged(Editable text) {
       }
                });

1 个答案:

答案 0 :(得分:0)

你需要创建一个setfilterfunction而不是调用getFilter.filter(arg0)。

public void SetFilter(CharSequence arg0) 
{

// clean old filter and set new filter params

}