2015-02-23 82 views
0

有人可以幫助我使用自定義適配器來過濾列表我一直在此卡住了一段時間..我遵循logcat和它輸出的過濾結果我的問題是更新列表視圖與過濾結果。Android的ListView過濾器不工作

// list of data items 
     private static List<ListData> mDataList = Arrays.asList(
     new ListData("Apple"), 
     new ListData("Banana"), 
     new ListData("Caramel"), 
     new ListData("Dog"), 
     new ListData("Cat"), 
     new ListData("Snake"), 
     new ListData("Lion"), 
     new ListData("Zebra"), 
     new ListData("Ant"), 
     new ListData("Bee"), 
     new ListData("Bird"), 
     new ListData("Leopard"), 
     new ListData("Flamingo"), 
     new ListData("Tusk"), 
     new ListData("Elephant"), 
     new ListData("Chicken") 
); 
SampleAdapter work = new SampleAdapter(); 
private Button btnoe; 
private AutoCompleteTextView aoe; 
    private TextView txtHistory, ao; 
      private ListView lv; 
     private String[] places; 
     private String p = ""; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_search); 
    final EditText e = (EditText) findViewById(R.id.edt_search); 
    // init the list view and its adapter 
    final ListView lv = (ListView) findViewById(R.id.listView); 
    lv.setAdapter(work); 


    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      e.setText("" + mDataList.get(position).data); 
     } 
    }); 
    //setFields(); 
    e.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 

      work.getFilter().filter(s.toString()); 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }); 
} 


private static class ListData { 

    private String data; 

    public ListData(String data) { 
     this.data = data; 
    } 

} 

private class SampleAdapter extends BaseAdapter implements Filterable { 

    private List<String> filtered = null; 
    private ItemFilter mFilter = new ItemFilter(); 


    @Override 
    public int getCount() { 
     return mDataList.size(); 
    } 

    @Override 
    public ListData getItem(int position) { 
     return mDataList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 


    @Override 
    public Filter getFilter() { 

     return mFilter; 
    } 

    private class ItemFilter extends Filter { 

     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 

      String filterString = constraint.toString().toLowerCase(); 

      FilterResults results = new FilterResults(); 

      final List<ListData> list = mDataList; 

      int count = list.size(); 

      final List<String> nlist = new ArrayList<>(count); 

      String FilterableString; 

      for (int i = 0; i < count; i++) { 
       FilterableString = list.get(i).data; 

       if (FilterableString.toLowerCase().contains(filterString)) { 
        mDataList.contains(FilterableString); 
        Log.d("mDataList", ""+mDataList.get(i).data); 
        nlist.add(FilterableString); 
       } 

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

     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      filtered = (ArrayList<String>) results.values; 
      notifyDataSetChanged(); 
     } 
    } 


    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 
     int pos = 0; 
     String s = ""; 
     if (convertView == null) { 
      convertView = View.inflate(ListActivity.this, R.layout.sample_list_item, null); 
      holder = new ViewHolder(convertView); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     ListData item = getItem(position); 
     s = item.data.substring(0, 1); 

     holder.textView.setText(item.data); 

     for (pos = 0; pos > mDataList.size(); pos++) { 
      s = mDataList.get(pos).data.substring(0, 1); 
     } 
    convertView.findViewById(R.id.imageView); 

     return convertView; 
    } 


} 

private class ViewHolder { 

    private View view; 

    private ImageView imageView, checkIcon; 

    private TextView textView; 

    private ViewHolder(View view) { 
     this.view = view; 
     imageView = (ImageView) view.findViewById(R.id.imageView); 
     textView = (TextView) view.findViewById(R.id.textView); 
     checkIcon = (ImageView) view.findViewById(R.id.check_icon); 
    } 
} 

}

回答

0

對象work是您的ArrayAdapter但它不會將數據傳遞到SampleAdapter。示例代碼(類似於我的)是這樣的:

SampleAdapter work = null; 
lvAdapter = new SampleAdapter(getActivity(), R.layout.list_item, mDataList); 
lv.setAdapter(work); 

基本上你需要將數據傳遞到構造函數的Adapter子類。另一個,SampleAdapter需要getView方法的佈局ID(XML)。

玩得開心...