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

问题描述

在此code,插入新项目adaper后,我的清单无法刷新,并以 notifyDataSetChanged更新()。例如,对于此行我的适配器可以设置没有任何问题:

  getRequestFromServer(0,10);
适配器=新ReceivedAdapter(G.context,项目);
setListAdapter(适配器);

之后,我在列表和适配器10个项目。

 私人字符串getRequestFromServer(长lastID,诠释计数){
    串接收=;
    尝试{
        收到=新JsonService(config_username,config_password,lastID,计数,G.F_RECEIVE_SMS).request();
        JSONArray data_array中=新JSONArray(收);        字符串mUserID = config_username;
        的for(int i = 0; I< data_array.length();我++){
            JSONObject的json_obj = data_array.getJSONObject(I)            串mLastID = json_obj.getString(id_recived_sms);
            串mSmsBody = json_obj.getString(SMS_BODY);
            串mSmsNumber = json_obj.getString(sms_number);
            串mSenderName = json_obj.getString(MOBILE_NUMBER);
            字符串mContactName = json_obj.getString(CONTACT_NAME);
            串mDate = json_obj.getString(recived_date);            ReceivedItemStructure项目=新ReceivedItemStructure(
                    mLastID,
                    mUserID,
                    mSmsBody,
                    mSmsNumber,
                    mSenderName,
                    mContactName,
                    mDate
            );
            items.add(项目);
        }    }赶上(例外五){
        e.printStackTrace();
    }
    收到回报;
}

items.add(项目); 计数是 10 。现在,在从服务器获取一个新的项目后,下面的这个功能,我的表计数值可以改变和更新,以11:

 私人无效addDataToList(字符串LastID,字符串SmsBody,字符串SmsNumber,字符串发送者姓名,联系人姓名字符串,字符串日期){
    字符串mLastID = LastID;
    字符串mUserID = config_username;
    字符串mSmsBody = SmsBody;
    字符串mSmsNumber = SmsNumber;
    字符串mSenderName =发送者姓名;
    字符串mContactName =联系人姓名;
    字符串mDate =日期;
    ReceivedItemStructure项目=新ReceivedItemStructure(
            mLastID,
            mUserID,
            mSmsBody,
            mSmsNumber,
            mSenderName,
            mContactName,
            mDate
    );
    items.add(项目);
    adapter.update(项目);
    adapter.notifyDataSetChanged();
}

items.add(项目); 计数是 11 。我更新与适配器 adapter.update(),并在适配器中使用 addDataToList()功能后,我的名单为 11 ,但 adapter.notifyDataSetChanged(); 不工作,我没有看到在新项目适配器和列表视图。插入和添加新项项目总是OK,我没有任何问题。

我的适配器是:

 公共类ReceivedAdapter扩展ArrayAdapter< ReceivedItemStructure> {    私人的ArrayList< ReceivedItemStructure>清单;    公共ReceivedAdapter(上下文C,ArrayList的< ReceivedItemStructure>项目){
        超(C,0,项目);
        表=项目;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        ReceiveItemView ItemView控件=(ReceiveItemView)convertView;
        如果(空== ItemView控件)
            ItemView控件= ReceiveItemView.inflate(父);        itemView.setItem(的getItem(位置));
        返回ItemView控件;
    }    公共无效更新(ArrayList的< ReceivedItemStructure>项目){
        表=项目;
        / *这条线可以更新列表的方法* /
    }
}


解决方案

更改以下code:

  items.add(项目);
adapter.update(项目);
adapter.notifyDataSetChanged();

  adapter.add(项目);
adapter.notifyDataSetChanged();

我想这应该解决您的问题。

您不需要使用列表存储在扩展的ArrayAdatper一个适配器的项目,因为它会保持项目清单给你。

更新:

然后尝试您的项目名单的副本传递给您的适配器。请尝试以下变化:

 适配器=新ReceivedAdapter(G.context,项目);

 适配器=新ReceivedAdapter(G.context,新的ArrayList< ReceivedItemStructure>(项目));

当你传递一个List实例通过其构造方法的ArrayAdapter,它会持有超过例如直接,意味着每个改变你做出一个ArrayAdapter以外的List实例会影响其数据集为好。

In this code, after inserting new items to adaper, my list could not refresh and update with notifyDataSetChanged(). For example, for this line my adapter could set without any problem:

getRequestFromServer(0, 10);
adapter = new ReceivedAdapter(G.context, items);
setListAdapter(adapter);

After that I have 10 items in list and adapter.

private String getRequestFromServer(long lastID, int count) {
    String received = "";
    try {
        received = new JsonService(config_username, config_password, lastID, count, G.F_RECEIVE_SMS).request();
        JSONArray data_array = new JSONArray(received);

        String mUserID = config_username;
        for (int i = 0; i < data_array.length(); i++) {
            JSONObject json_obj = data_array.getJSONObject(i);

            String mLastID = json_obj.getString("id_recived_sms");
            String mSmsBody = json_obj.getString("sms_body");
            String mSmsNumber = json_obj.getString("sms_number");
            String mSenderName = json_obj.getString("mobile_number");
            String mContactName = json_obj.getString("contact_name");
            String mDate = json_obj.getString("recived_date");

            ReceivedItemStructure item = new ReceivedItemStructure(
                    mLastID,
                    mUserID,
                    mSmsBody,
                    mSmsNumber,
                    mSenderName,
                    mContactName,
                    mDate
            );
            items.add(item);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return received;
}

items.add(item); count is that 10. Now, in this below function after getting a new item from the server, my list count can change and update to 11:

private void addDataToList(String LastID, String SmsBody, String SmsNumber, String SenderName, String ContactName, String Date) {
    String mLastID      = LastID;
    String mUserID      = config_username;
    String mSmsBody     = SmsBody;
    String mSmsNumber   = SmsNumber;
    String mSenderName  = SenderName;
    String mContactName = ContactName;
    String mDate = Date;
    ReceivedItemStructure item = new ReceivedItemStructure(
            mLastID,
            mUserID,
            mSmsBody,
            mSmsNumber,
            mSenderName,
            mContactName,
            mDate
    );
    items.add(item);
    adapter.update(items);
    adapter.notifyDataSetChanged();
}

items.add(item); count is that 11. I update the adapter with adapter.update(), and in the adapter my list after using the addDataToList() function is 11, but adapter.notifyDataSetChanged(); doesn't work and I don't see the new item in adapter and list view. Inserting and adding a new item into items is always OK and I don't have any problem.

My adapter is:

public class ReceivedAdapter extends ArrayAdapter<ReceivedItemStructure> {

    private ArrayList<ReceivedItemStructure> list;

    public ReceivedAdapter(Context c, ArrayList<ReceivedItemStructure> items) {
        super(c,0,items);
        list = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ReceiveItemView itemView = (ReceiveItemView)convertView;
        if (null == itemView)
            itemView = ReceiveItemView.inflate(parent);

        itemView.setItem(getItem(position));
        return itemView;
    }

    public void update(ArrayList<ReceivedItemStructure> items) {
        list = items;
        /* this line could update list method*/
    }
}
解决方案

Change the following code:

items.add(item);
adapter.update(items);
adapter.notifyDataSetChanged();

to:

adapter.add(item);
adapter.notifyDataSetChanged();

I think this should solve your problem.

You don't need to use a list to store items in an Adapter that is extended the ArrayAdatper, because it will maintain a list of items for you.

Update:

Then try to pass a copy of your items List to your adapter. Try the following change:

adapter = new ReceivedAdapter(G.context, items);

to:

adapter = new ReceivedAdapter(G.context, new ArrayList<ReceivedItemStructure>(items));

When you pass a List instance to an ArrayAdapter via its constructor, it will hold than instance directly, means that every change you make to that List instance outside the ArrayAdapter will affect its data set as well.

这篇关于Android的ArrayAdapter notifyDataSetChanged不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 00:37