Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ArrayAdapter notifyDataSetChanged不';行不通_Android_Listview - Fatal编程技术网

Android ArrayAdapter notifyDataSetChanged不';行不通

Android ArrayAdapter notifyDataSetChanged不';行不通,android,listview,Android,Listview,在此代码中,在将新项目插入adaper后,我的列表无法刷新并更新为notifyDataSetChanged()。例如,对于这一行,我的适配器可以设置为没有任何问题: getRequestFromServer(0, 10); adapter = new ReceivedAdapter(G.context, items); setListAdapter(adapter); 之后,我在列表和适配器中有10项 private String getRequestFromServer(long lastI

在此代码中,在将新项目插入adaper后,我的列表无法刷新并更新为
notifyDataSetChanged()
。例如,对于这一行,我的适配器可以设置为没有任何问题:

getRequestFromServer(0, 10);
adapter = new ReceivedAdapter(G.context, items);
setListAdapter(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;
}
项目。添加(项目)计数为
11
。我使用
adapter.update()
更新适配器,使用
addDataToList()
函数后,在适配器列表中我的列表是
11
,但
adapter.notifyDataSetChanged()不起作用,我在适配器和列表视图中看不到新项。在
项目中插入和添加新项目总是可以的,我没有任何问题

我的适配器是:

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*/
    }
}
公共类ReceivedAdapter扩展了ArrayAdapter{
私有数组列表;
public ReceivedAdapter(上下文c,数组列表项){
超级(c,0,项目);
列表=项目;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
ReceiveItemView itemView=(ReceiveItemView)convertView;
if(null==itemView)
itemView=ReceiveItemView.inflate(父项);
setItem(getItem(position));
返回项目视图;
}
公共作废更新(ArrayList项){
列表=项目;
/*此行可以更新列表方法*/
}
}

更改以下代码:

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

我想这应该能解决你的问题

您不需要使用列表在扩展了ArrayAdapter的适配器中存储项目,因为它将为您维护一个项目列表

更新:

然后尝试将项目列表的副本传递给适配器。尝试以下更改:

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

adapter=newreceivedapter(G.context,newarraylist(items));

当您通过ArrayAdapter的构造函数将List实例传递给ArrayAdapter时,它将直接保存than实例,这意味着您在ArrayAdapter之外对该列表实例所做的每一次更改都将影响其数据集。

在设置listadapter之前使用adapter.notifydatasetchanged并告诉我它是否有效not@AbhishekChaubey然后你说我必须是move
adapter.notifyDataSetChanged()之后<代码>适配器=新的ReceivedAdapter(G.context,items);setListAdapter(适配器)???@AbhishekChaubey移动到不工作的位置。我累了,请帮帮我好吧,让我试着重新审视这个问题@andbee@AbhishekChaubey先生,你能找到问题吗
BaseAdapter
可以解决我的问题吗?更改后,我的列表为空。@并且您的适配器现在使用不同的数据集实例,因此您需要调用adapter.addAll(列表);或适配器。添加(项);获取新数据并希望其显示后。例如,您需要在此处调用adapter.addAll(items):public void run(){getRequestFromServer(0,10);db.saveReceivedItemsToDatabase(items,config_username);setListAdapter(adapter);}@乐高我正在将ArrayAdapter更改为BaseAdapter,但这就是我的问题所在
adapter.add(item);
adapter.notifyDataSetChanged();
adapter = new ReceivedAdapter(G.context, items);
adapter = new ReceivedAdapter(G.context, new ArrayList<ReceivedItemStructure>(items));