yDataSetChanged时出现IndexOutOfBoun

yDataSetChanged时出现IndexOutOfBoun

本文介绍了我使用notifyDataSetChanged时出现IndexOutOfBoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这篇文章中发现了我的问题使用notifyDataSetChanged()更新ExpandableListView

I Founded my problem in this post Updating ExpandableListView with notifyDataSetChanged()

每次使用setNotifyDatasetChanged刷新视图时,适配器都会调用List周围的循环.由于所做的更改,适配器中的List会得到空值."

"each time you refresh the the views using setNotifyDatasetChanged the Adapter will call the loop around the List. and you List in the Adapter gets a null value due to the changes you made."

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

I am beginner and can not properly make changes to the list

我的来源

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中的列表中删除项目?

推荐答案

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

In addition to overriding public View getView(int position, View view, ViewGroup parent), make sure your class that extends ArrayAdapter overrides these methods:

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

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

I believe notifyDataSetChanged() will call getCount() on your adapter to determine how many items there are. If you don't override this method with return urlItems1.size(); then an IndexOutOfBoundException seems imminent because there will be no way for your custom adapter to tell clients about its size and contents.

这篇关于我使用notifyDataSetChanged时出现IndexOutOfBoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:02