简体   繁体   English

Android ArrayAdapter notifyDataSetChanged不起作用

[英]Android ArrayAdapter notifyDataSetChanged doesn't work

In this code, after inserting new items to adaper, my list could not refresh and update with notifyDataSetChanged() . 在此代码中,将新项目插入adaper后,我的列表无法使用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. 之后,我在列表和适配器中有10个项目。

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 . 算是10 Now, in this below function after getting a new item from the server, my list count can change and update to 11: 现在,从服务器中获取新项目后,在以下功能中,我的列表计数可以更改并更新为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 . 计数是11 I update the adapter with adapter.update() , and in the adapter my list after using the addDataToList() function is 11 , but adapter.notifyDataSetChanged(); 我使用adapter.update()更新适配器,在适配器中,使用addDataToList()函数后的列表为11 ,但是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. 将新项目插入并添加到items中总是可以的,而且我没有任何问题。

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. 您不需要使用列表将项目存储在扩展了ArrayAdatper的Adapter中,因为它将为您维护项目列表。

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. 当您通过其构造函数将List实例传递给ArrayAdapter时,它将直接持有该实例,这意味着您在ArrayAdapter之外对该List实例进行的每次更改也会影响其数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM