2012-08-07 13 views
5

の後に更新されないので、私はカスタムArrayAdapterを持っていますので、ListViewで利用可能なタイトル/字幕ビューを使用できます。私はEditTextを受け取り、文字列を受け取り、アダプタをフィルタリングします。AndroidカスタムArrayAdapterはフィルタ

フィルタは、それが正しいオブジェクトをフィルタリングしているという意味で動作します(私はそれをクリックして伝えることができ、それが正しい「エキストラ」と意図を開始します。)

しかしフィルタリングが機能していても、アダプターの項目は、正しい情報を表示するように更新されません。タイトルとサブタイトルが正しくありません。

ListViewには0〜9の項目があり、検索では3つの項目に絞り込み、フィルタリングされた項目は5,6,9 ... 3つの項目が表示されますが、最初の3つ元の事前検索ListView(0-2)の項目。アイテム2(3番目のアイテム)をクリックすると、9の内容が新しいインテントに含まれます。これは検索基準には正しいですが、タイトルには正しい情報が反映されています。

私はListViewにリフレッシュするために何が必要なのか分かりません。 私はそれとは思わないnotifyDataSetChanged();

何か助けていただければ幸いです。 ありがとう!

public class myListAdapter extends ArrayAdapter<Pizza>{ 
    private ArrayList<Pizza> items; 
    private PizzaViewHolder myListHolder; 

    private class PizzaViewHolder{ 
     TextView title; 
     TextView subtitle; 
    } 

    public myListAdapter(Context context, int textViewResourceId, ArrayList<Pizza> items) { 
     super(context, textViewResourceId, items); 
     this.items = items; 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public View getView(int pos, View convertView, ViewGroup parent){ 
     View v = convertView; 
     if(v == null){ 
      LayoutInflater vi = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.myList_item, null); 
      myListHolder = new PizzaViewHolder(); 
      myListHolder.title = (TextView)v.findViewById(R.id.title); 
      myListHolder.subtitle = (TextView)v.findViewById(R.id.subtitle); 
      v.setTag(myListHolder); 
     }else myListHolder = (PizzaViewHolder)v.getTag(); 

     Pizza myList = items.get(pos); 

     if (myList != null){ 
      myListHolder.title.setText(myList.getTitle()); 
      myListHolder.subtitle.setText(myList.getSubTitle()); 
     }  
     return v;   
    } 
} 

これは私が判断したものから、検索

private TextWatcher filterTextWatcher = new TextWatcher(){ 

    public void afterTextChanged(Editable s) { 
     // TODO Auto-generated method stub 
     if(!s.equals("")){ 
      ((Filterable) this.listView1.getAdapter()).getFilter().filter(s); 
      } 
    } 

    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
     // TODO Auto-generated method stub 

    } 

    public void onTextChanged(CharSequence s, int start, int before, 
      int count) { 
     // TODO Auto-generated method stub 

    } 

}; 
+0

標準のアダプタを使用すると、notifyDataSetChangedを使用する必要はありません。私のカスタムアダプタは、インターネット上で最も簡単で簡単な例のうちの1つをリフレッシュしません( –

答えて

13

あり、私が私のカスタムArrayAdapterのためのカスタムフィルタが必要だと思われます。 カスタムArrayAdapterは、ここでは、フィルターのオーバーライドされた実装を持っているコードです:

import java.util.ArrayList; 
import android.content.Context; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.Filter; 
import android.widget.TextView; 

public class PizzaAdapter extends ArrayAdapter<Pizza>{ 

private ArrayList<Pizza> original; 
private ArrayList<Pizza> fitems; 
private Filter filter; 


public PizzaAdapter(Context context, int textViewResourceId, ArrayList<Pizza> items) { 
    super(context, textViewResourceId, items); 

    this.original = new ArrayList<Pizza>(items); 
    this.fitems = new ArrayList<Pizza>(items); 
    this.filter = new PizzaFilter(); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent){ 
    View v = convertView; 

    if(v == null){ 
     LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.pizza_list_item, null); 
    } 

    Pizza pizza = fitems.get(position); 

    if(pizza != null){ 
     String subtitleString = new String("[" + pizza.getPizzaType() + "] " + pizza.getPizzaCategory() + ": " + pizza.getPizzaCode()); 

     TextView title = (TextView)v.findViewById(R.id.title); 
     TextView subtitle = (TextView)v.findViewById(R.id.subtitle); 

     if(title != null){ 
      title.setText(pizza.getPizzaName()); 
     } 
     if(subtitle != null){ 
      subtitle.setText(subtitleString); 
     } 
    } 
    return v; 
} 

@Override 
public Filter getFilter(){ 

    if(filter == null){ 
     filter = new PizzaFilter(); 
    } 
    return filter; 
} 

private class PizzaFilter extends Filter{ 
    @Override 
    protected FilterResults performFiltering(CharSequence constraint){ 
     FilterResults results = new FilterResults(); 
     String prefix = constraint.toString().toLowerCase(); 

     if (prefix == null || prefix.length() == 0){ 
      ArrayList<Pizza> list = new ArrayList<Pizza>(original); 
      results.values = list; 
      results.count = list.size(); 
     }else{ 
      final ArrayList<Pizza> list = new ArrayList<Pizza>(original); 
      final ArrayList<Pizza> nlist = new ArrayList<Pizza>(); 
      int count = list.size(); 

      for (int i = 0; i<count; i++){ 
       final Pizza pizza = list.get(i); 
       final String value = Pizza.getPizzaName().toLowerCase(); 

       if(value.contains(prefix)){ 
        nlist.add(pizza); 
       } 
       results.values = nlist; 
       results.count = nlist.size(); 
      } 
     } 
     return results; 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    protected void publishResults(CharSequence constraint, FilterResults results) { 
     fitems = (ArrayList<Pizza>)results.values; 
     notifyDataSetChanged(); 
     clear(); 
     int count = fitems.size(); 
     for(int i = 0; i<count; i++){ 
      add(fitems.get(i)); 
      notifyDataSetInvalidated(); 
     } 
    } 
} 
} 

それはあなたが検索時にフィルターのカスタムimplmentationは表示を更新していることが判明しました。うまくいけば、これは一部の人々を助けるでしょう。

+3

)。 + – AlexAndro

+0

これを解決するためにコードをどのように使用すればよいか考えてみましょう:http://stackoverflow.com/questions/20524417/listview-is-blank-while-using-getfilter-function – Si8

+0

'String prefix = constraint.toString()。 toLowerCase(); '' NullPointerException'をスローすることができます – mallaudin

関連する問題