2012-11-06 213 views
0

我有一個自定義ArrayAdaptor。 添加getFilter功能時,我意識到篩選器項目對於我的下一個搜索越來越小。 即使我刪除EditText中的文字,它仍然是我最後的搜索記錄。Android - 自定義適配器陣列列表替換過濾器陣列列表

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) { 
     } 
       }); 

回答

0

你需要創建一個setfilte rfunction而不是調用getFilter.filter(arg0)。

public void SetFilter(CharSequence arg0) 
{ 

// clean old filter and set new filter params 

} 
+0

無法工作,你能告訴我一些Setfilter代碼嗎? –