我有延伸ArrayAdapter定制适配器一个ListView。这类型的艺术家的ArrayAdapter。

艺术家是有一个名字和一个id一个非常小的类。艺术家类具有的toString()覆盖,以返回只是名字。

我有一个EditText。该EditText上有一个TextChangeListener在那里我打电话.getFilter()。过滤器(字符,回调)我的适配器上。

在Filter.Filterlistener()的onComplete()回调我打印的数量和它看起来非常好。作为i型计数减少。因此,作为标榜它缝一切正常,但清单保持不变。我试着打电话给artistAdapter.notifyDataSetChanged()以强制列表重新绘制,但没有任何反应。 [参见2)。]

我现在正在摆弄周围的天!我绝望..希望有人能对我的代码看,并告诉我什么,我做错了!

谢谢!

下面是我做了什么:

定义一个ListView和一个EditText这样

1):

<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)设置我的ListView在活动的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。我增加了一个删除()和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)我的艺术家也具有了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;
    }
}
有帮助吗?

解决方案

我找到了解决办法。我已经重写从头整个代码,发现问题。

在我的自定义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