3

我在这篇用 notifyDataSetChanged() 更新 ExpandableListView 的帖子中发现了我的问题

“每次使用 setNotifyDatasetChanged 刷新视图时,适配器都会调用列表周围的循环。由于您所做的更改,适配器中的列表会获得空值。”

我是初学者,无法正确更改列表

我的消息来源

public class UrlArrayAdapter extends ArrayAdapter<UrlItem> {

    private LayoutInflater inflater;
    private ListView urlListView1;
    private ArrayList<UrlItem> urlItems1;

    public UrlArrayAdapter(Context context, ListView urlListView,
            ArrayList<UrlItem> urlLists) {
        super(context, urlListView.getId(), urlLists);
        this.urlItems1 = urlLists;
        this.urlListView1 = urlListView;


        inflater = LayoutInflater.from(context);

如何从基本适配器的 urlLists 列表中删除项目?

4

2 回答 2

0

删除项目:urlList.remove(item_index); 然后通知数据集已更改。

根据您在下面的评论,我正在更新我的答案。
remove()方法接受被删除元素的索引,而不是元素本身。

因此,要从列表中删除特定数字,您应该首先找到它的索引,然后将该索引传递给该remove()方法。

前任:

public void deleteItem(int numberToDelete) {
    int index=-1;

    // Find the index of numberToDelete
    if(urlLists.contains(numberToDelete)){
        index = urlLists.indexOf(numberToDelete);
    }

    // Delete the number by spefying the index found.
    if(index!=-1){
        urlLists.remove(index);
    }

//...
}
于 2012-08-16T15:25:02.287 回答
0

除了覆盖之外public View getView(int position, View view, ViewGroup parent),请确保您的扩展类ArrayAdapter覆盖这些方法:

public int getCount()
public UrlItem getItem(int position)
public int getPosition(Hold item)
public long getItemId(int position)

我相信notifyDataSetChanged()会调用getCount()您的适配器来确定有多少项目。如果您不覆盖此方法,return urlItems1.size();那么IndexOutOfBoundException似乎迫在眉睫,因为您的自定义适配器将无法告诉客户其大小和内容。

于 2012-08-20T20:20:06.687 回答