質問

私はArrayAdapterを拡張するカスタムアダプタとリストビューを持っています。これは、タイプアーティストのArrayAdapterです。

アーティストは、名前とIDを持っている非常に小さなクラスです。アーティストクラスのtoString()名前だけを返すようにオーバーライドしています。

私はのEditTextを持っています。 EditTextは私が私のアダプタ上.getFilter()。フィルター(文字、コールバック)を呼び出しTextChangeListenerを持っています。

Filter.Filterlistenerで

()。onCompleteの()私は、カウントを印刷するコールバックし、それは本当によさそうです。私が入力すると、カウントは減少します。それ縫い目宣伝として、すべての作品が、リストの滞在と同じようにします。私は再描画するために、リストを強制的にartistAdapter.notifyDataSetChanged()を呼び出ししようとしましたが、何も起こりません。 [2を参照。)

私は今日間の周りいじっています!私は必死だ..うまくいけば、誰かが私のコードの表情を持っていると私は間違ってやっているものを私に伝えることができます!

ありがとうございます。

ここに私がやっていることです。

1)、このようなリストビューとのEditText定義:

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_search_text"
    android:layout_width="fill_parent"
    android:layout_height="35dip"
    android:layout_below="@id/header">
</EditText>       
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_search"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
</ListView>

2)セットアップ活動のonCreate(の私のリストビュー):ます。

private ListView listView = null;
private ArtistAdapter artistAdapter = null;

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

    artistAdapter = new ArtistAdapter(this, R.layout.row, list); // 'list' is an ArrayList<Artist>

    listView = (ListView) findViewById(R.id.list_search);
    listView.setAdapter(artistAdapter);
    listView.setFastScrollEnabled(true);
    listView.setTextFilterEnabled(true);

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> av, View v, int position, long id) {
            // do something
        }
    });

    EditText txtSearch = (EditText) findViewById(R.id.list_search_text);
    txtSearch.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable arg0) { }

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

        public void onTextChanged(CharSequence chars, int start, int before, int count) {
            artistAdapter.getFilter().filter(chars, new Filter.FilterListener() {
                public void onFilterComplete(int count) {
                    Log.d(Config.LOG_TAG, "filter complete! count: " + count);
                    artistAdapter.notifyDataSetChanged();
                }
            });
        }
    });
}

3。)これは要するに私ArtistAdapterです。私は、remove()とadd()を追加する方法:

public class ArtistAdapter extends ArrayAdapter<Artist> implements SectionIndexer {
    private List<Artist> items;

    /* other stuff like overridden getView, getPositionForSection, getSectionForPosition and so on */

    @Override
    public void remove(Artist object) {
        super.remove(object);
        items.remove(object);
    }

    @Override
    public void add(Artist object) {
        super.add(object);
        items.add(object);
    }
}    

4)MyアーティストものtoStringを(持っている)上書き:ます。

public class Artist implements Comparable<Artist> {
    public String   uid;
    public String   name;

    public Artist(String id, String name) {
        this.uid = id;
        this.name = name;
    }

    public int compareTo(Artist another) {
        return this.name.compareToIgnoreCase(another.name);
    }

    @Override
    public String toString() {
        return this.name;
    }
}
役に立ちましたか?

解決

私は解決策を見つけました。私は最初から全体のコードを書き換えて、問題を発見した。

アイテムを格納するためのVARを持っている場合は、

私のカスタムArrayAdapter呼ばArtistAdapterでは(問題の3を参照)。 プライベートリストの項目;

そして、私のオーバーライドされたgetViewメソッド(中)私は私がやった現在の項目を取得したいとき: items.getItem(位置) 今私が行います。 getItems(項目は、基本クラスと自分ではないの記憶装置から検索されるように)(位置)と、これはトリックを行うようだ!

このは、それが(働いているバージョン、)今のように)私のgetViewメソッド(ですます:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LinearLayout artistView;
    Artist art = getItem(position);

    if (convertView == null) {
        artistView = new LinearLayout(getContext());
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater vi;
        vi = (LayoutInflater)getContext().getSystemService(inflater);
        vi.inflate(resId, artistView, true);
    } else {
        artistView = (LinearLayout) convertView;
    }
    TextView nameView = (TextView)artistView.findViewById(R.id.txt_name);
    TextView uidView = (TextView)artistView.findViewById(R.id.txt_uid);
    nameView.setText(art.name);
    uidView.setText(art.uid);
    return artistView;
}

のPfu、これは本当に迷惑なバグだった...

他のヒント

listView.setFilterText()はなく、アダプタフィルタを使用してみてください。

あなたがフィルタにしたいテキストを返すために、toString()メソッドをオーバーライドする必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top