2016-03-16 68 views
0

我已經在列表中實現了自定義過濾器,但沒有工作,可以告訴我我的代碼中的問題在哪裏?自定義過濾器不工作在android

這是我的創建方法。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_testsample); 

    sv = (SearchView) findViewById(R.id.searchView); 


    item_list = (ArrayList<Item_Category>) getLastNonConfigurationInstance(); 

    if (item_list == null) { 
     lv = (ListView) findViewById(R.id.listView3); 
     lv.setItemsCanFocus(false); 
     lv.setChoiceMode(lv.CHOICE_MODE_MULTIPLE); 
     item_list = new ArrayList<Item_Category>(); 

     item_list.add(new Item_Category("gold", false)); 
     item_list.add(new Item_Category("sugar", false)); 
     item_list.add(new Item_Category("gulli", false)); 
     item_list.add(new Item_Category("silver", false)); 
     item_list.add(new Item_Category("chmacham", false)); 


     arrayList = new ArrayList<Item_Category>(); 

     arrayList.addAll(item_list); 
     arrayAdapter = new ItemCategoryArrayAdapter(this, arrayList); 

     lv.setAdapter(arrayAdapter); 
    } 

    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 
     @Override 
     public boolean onQueryTextSubmit(String query) { 

      return false; 
     } 

     @Override 
     public boolean onQueryTextChange(String text) { 



      selected = text.trim(); 

      //lv = (ListView)findViewById(R.id.listView); 
      if (arrayAdapter != null) { 
       arrayAdapter.getFilter().filter(selected); 
       lv.setAdapter(arrayAdapter); 
       return true; 
      } 
      return true; 
     } 
    }); 

} 

,這是所有的自定義列表視圖和自定義過濾器..

private class Item_Category 
{ 

    String category_Name ; 
    boolean checked ; 

    public String getCategory_Name() { 
     return category_Name; 
    } 

    public void setCategory_Name(String category_Name) { 
     this.category_Name = category_Name; 
    } 

    public boolean isChecked() { 
     return checked; 
    } 

    public void setChecked(boolean checked) { 
     this.checked = checked; 
    } 

    public Item_Category(String category_Name, boolean checked) { 
     this.category_Name = category_Name; 
     this.checked = checked; 
    } 



} 

private static class Item_Category_ViewHolder 
{ 
    TextView categoryName; 
    CheckBox checkBox; 

    public TextView getCategoryName() { 
     return categoryName; 
    } 

    public void setCategoryName(TextView categoryName) { 
     this.categoryName = categoryName; 
    } 

    public CheckBox getCheckBox() { 
     return checkBox; 
    } 

    public void setCheckBox(CheckBox checkBox) { 
     this.checkBox = checkBox; 
    } 


    public Item_Category_ViewHolder(TextView categoryName, CheckBox checkBox) { 
     this.categoryName = categoryName; 
     this.checkBox = checkBox; 
    } 
} 


public class ItemCategoryArrayAdapter extends ArrayAdapter<Item_Category> implements Filterable { 
    private LayoutInflater inflater; 
    ArrayList<String> itemList; 
    List<Item_Category> filtered; 
    List<Item_Category> mStringFilterList; 
    ItemCategoryFilter categoryFilter; 

    public ItemCategoryArrayAdapter(Context context, List<Item_Category> categoryList) { 
     super(context, R.layout.everyday_item_category_row, R.id.itemName, categoryList); 
     // Cache the LayoutInflate to avoid asking for a new one each time. 

     filtered = new ArrayList<Item_Category>(categoryList); 
     this.inflater = LayoutInflater.from(context); 
     mStringFilterList = new ArrayList<Item_Category>(categoryList); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // Planet to display 
     Item_Category item_list = (Item_Category) this.getItem(position); 

     // The child views in each row. 
     CheckBox checkBox; 
     TextView itemCategory; 

     // Create a new row view 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.everyday_item_category_row, null); 

      // Find the child views. 
      itemCategory = (TextView) convertView.findViewById(R.id.itemName); 
      checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox); 

      // Optimization: Tag the row with it's child views, so we don't have to 
      // call findViewById() later when we reuse the row. 
      convertView.setTag(new Item_Category_ViewHolder(itemCategory, checkBox)); 

      // If CheckBox is toggled, update the planet it is tagged with. 
      checkBox.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        CheckBox cb = (CheckBox) v; 
        Item_Category item_list1 = (Item_Category) cb.getTag(); 
        item_list1.setChecked(cb.isChecked()); 
       } 
      }); 
     } 
     // Reuse existing row view 
     else { 
      // Because we use a ViewHolder, we avoid having to call findViewById(). 
      Item_Category_ViewHolder viewHolder = (Item_Category_ViewHolder) convertView.getTag(); 
      checkBox = viewHolder.getCheckBox(); 
      itemCategory = viewHolder.getCategoryName(); 

     } 

     // Tag the CheckBox with the Planet it is displaying, so that we can 
     // access the planet in onClick() when the CheckBox is toggled. 
     checkBox.setTag(item_list); 

     // Display planet data 
     checkBox.setChecked(item_list.isChecked()); 
     itemCategory.setText(item_list.getCategory_Name()); 

     CheckBox c1 = (CheckBox) convertView.findViewById(R.id.CheckBox); 
     final TextView t1 = (TextView) convertView.findViewById(R.id.itemName); 
     c1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 


       if (isChecked) { 
        itemList.add(t1.getText().toString()); 
       } else { 
        if (itemList.contains(t1.getText().toString())) { 
         itemList.remove(t1.getText().toString()); 
        } 

       } 
      } 
     }); 


     return convertView; 
    } 
    @Override 
    public Filter getFilter() { 
     if (categoryFilter == null) 
      categoryFilter = new ItemCategoryFilter(); 

     return categoryFilter; 
    } 


    private class ItemCategoryFilter extends Filter { 
     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 

      mStringFilterList = new ArrayList<Item_Category>(); 
      FilterResults results = new FilterResults(); 

      if (constraint != null && constraint.length() > 0) { 
       ArrayList<Item_Category> filterList = new ArrayList<Item_Category>(); 
       for (int i = 0; i < mStringFilterList.size(); i++) { 
        if ((mStringFilterList.get(i).getCategory_Name().toUpperCase()) 
          .contains(constraint.toString().toUpperCase())) { 

         Item_Category itemCategory = new Item_Category(mStringFilterList.get(i) 
           .getCategory_Name(), false); 

         filterList.add(itemCategory); 
        } 
       } 
       results.count = filterList.size(); 
       results.values = filterList; 
      } else { 
       results.count = mStringFilterList.size(); 
       results.values = mStringFilterList; 
      } 
      return results; 
     } 

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

      // Now we have to inform the adapter about the new list filtered 
      filtered = (ArrayList<Item_Category>) results.values; 
      notifyDataSetChanged(); 
     } 

    } 
+0

你面臨的問題是什麼? –

+0

您可以請出示錯誤日誌或告訴我們發生了什麼? – heloisasim

+0

我的自定義過濾器沒有過濾(Textview)在列表視圖 – Sid

回答

0

更改適配器構造如下。

public ItemCategoryArrayAdapter(Context context, List<Item_Category> categoryList) { 
    filtered = new ArrayList<Item_Category>(categoryList); 
    super(context, R.layout.everyday_item_category_row, R.id.itemName, filtered); 
    // Cache the LayoutInflate to avoid asking for a new one each time. 

您正在傳遞完整列表,即categoryList,而不是過濾List,因此它不反映。

+0

我已經嘗試刪除lv.setAdapter()仍然無法正常工作。問題是 - 過濾器沒有過濾listview中的內容。 – Sid

+0

我改變了我的答案。試試這個,讓我知道它是否工作。 –

+0

抱歉不工作,據我所知,我們無法初始化超級關鍵字之前的任何東西。 – Sid

1

將適配器創建爲自定義網格視圖或列表視圖,兩者在過濾時都存在一些問題。您看到,適配器不會過濾網格或列表視圖中的數據。

我不能粘貼代碼,但我會建議你看看執行ArrayAdapter.class。確保ItemCategoryArrayAdapter.class具有ArrayAdapter.class中的大多數方法。

只要確保ArrayAdapter.class中存在的大多數方法都在ItemCategoryArrayAdapter.class中執行。

這會修復它...