2011-08-09 2 views
3

次のコードは、検索の文字を入力するときにlistviewのビューを0に戻します。EditText 次のメソッドは、これは私のアダプタクラスである文字列ArrayAdapterを基にしたListViewでTextWatcherを使用してフィルタリングすると、空の結果が返されます。

private void setupList() { 
    final ListView lv = (ListView) findViewById(R.id.contactList); 
    ArrayAdapter<Info> la = new MyListAdapter(this, mInfoList); 
    lv.setAdapter(la); 
    lv.setTextFilterEnabled(true); 
    EditText edit = (EditText) findViewById(R.id.searchbar); 
    edit.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 

     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 

     } 

     @Override 
     public void afterTextChanged(Editable text) { 
      Log.d("search", ""+text); 
      ArrayAdapter<Info> la = (ArrayAdapter<Info>) lv.getAdapter(); 
      la.getFilter().filter(text); 
      la.notifyDataSetChanged(); 
     } 
    }); 
} 

activtyクラスからである

public class MyListAdapter extends ArrayAdapter<Info> { 
private Bitmap mDefaultProfilePic = null; 
Context mContext = null; 

public MyListAdapter(Context ctxt, ArrayList<Info> mFriendsAccounts) { 
    super(ctxt, R.id.name, mFriendsAccounts); 
    mContext = ctxt; 

    mDefaultProfilePic = BitmapFactory.decodeResource(ctxt.getResources(), R.drawable.face); 
} 


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

    if (convertView == null) { 
     LayoutInflater inf = (LayoutInflater) mContext.getSystemService(Service.LAYOUT_INFLATER_SERVICE); 
     convertView = inf.inflate(R.layout.layout_list_view, null); 
    } 
    Info usr = getItem(position); 
    ((TextView)convertView.findViewById(R.id.name)).setText(usr.Name); 
    ((ImageView)convertView.findViewById(R.id.invite)).setTag(position); 

    if (mImageBitmaps.get(position) != null) { 
     ((ImageView)convertView.findViewById(R.id.profilePic)).setImageBitmap(mImageBitmaps.get(position)); 
    } else { 
     ((ImageView)convertView.findViewById(R.id.profilePic)).setImageBitmap(mDefaultProfilePic); 
    } 

    return convertView; 
} 

} 
+0

コードをデバッグしてステップ実行して、正確にgetAdapterが返すものを確認しましたか? – Jack

答えて

4

最後に問題を修正しました。 InfoオブジェクトのtoString()メソッドを無効にする必要がありました。私の場合、フィルタリングはnameフィールドに基づいていますので、toString()によって返されます。
フィルタ処理では、アダプタ内の各オブジェクトのtoString()が呼び出されます。

+0

userSeven7s私は同じ問題がありますが、私は問題を表示することはできません。あなたは少し明白になりますか? Thx – Lasecun

+0

ここに質問を投稿しましたか?リンクをお願いします。私の場合、 'toString'メソッドをオーバーライドして情報オブジェクトデータを文字列形式で返します。 – Ronnie

+0

Here - http://stackoverflow.com/questions/12476645/filter-listview-i-cant-understand-filter-rule – Lasecun

1

Hereそれは言う:

返さアダプタが setAdapterに渡された同じアダプタではないかもしれません( ListAdapter)でも、WrapperListAdapterである可能性があります。

これはあなたの問題と関係がありますか?

+0

いいえアダプターを保持するメンバーを追加しましたが、問題を解決しませんでした。 – Ronnie

+0

デバッグしてコードをステップ実行して、何が起こっているのかを正確に確認しましたか? – Jack

+0

はい。私は任意のキーを押したときにリストビューはアダプタからのすべてのビュー(フィルタリングされたものだけではなく)を取得していますが、リストビューに表示されません。それはフィルターが問題であることを意味しますか? – Ronnie

関連する問題