适配器的内容已更改,但ListView未收到通知。(使用后台服务)

时间:2015-08-21 17:10:38

标签: java android multithreading listview

我的活动我正在使用后台服务,我每7秒钟从json获取数据。并在UI线程上更新我的列表视图。有时会有大量日期,列表视图会滚动它的崩溃。使用此java.lang.IllegalStateException:适配器的内容已更改但ListView未收到通知。确保不从后台线程修改适配器的内容,而只是从UI线程修改。

更新服务

android.os.Handler handler_service = new android.os.Handler() {
    public void handleMessage(android.os.Message msg) {
        runOnUiThread(new Runnable() {
            @Override
            public synchronized void run() {    
                if (jon_list_Adapter == null) {
                    try {
                        jon_list_Adapter = new Different_Job_List_Adapter(
                        getApplicationContext(),
                        Different_Job_List.getInstance(),
                        Driver_Request_JobActivity.this);

                        listdriver_invites.setAdapter(jon_list_Adapter);
                        jon_list_Adapter.notifyDataSetChanged();

                        // notifyDataSetChanged();
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                } else {
                    try {
                        jon_list_Adapter.notifyDataSetChanged();
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                }
            }
        });
    };
};

1 个答案:

答案 0 :(得分:0)

You need to call notifyDataSetChanged() in the adapter, so set up a method called refreshData in the adapter like this:

public void refreshData(ArrayList<yourList> data) {
    mValues.clear();
    mValues.addAll(data);
    notifyDataSetChanged();
}

and call from your activity:

json_list_Adapter.refreshData(newData) 

with the new data as an argument.