.notifydatasetchanged在android中的expandable listview中不起作用

时间:2014-09-23 07:02:44

标签: android expandablelistview notifydatasetchanged

在xml解析中,我在刷新可扩展列表视图中的数据后调用了notifydatasetchanged,但应用程序崩溃了。我的适配器类正在扩展BaseExpandableListAdapter。 我使用了以下代码:

class PushNotification extends AsyncTask<String, Void, Void> {


    @Override
    protected Void doInBackground(String... arg0) {
        makeGetRequest();
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        listAdapter = new ExpandableListAdapter(ct, values, scores);
        for(int i=0;i<values.size();i++){

        }
        expListView.setAdapter(listAdapter);
        listAdapter.notifyDataSetChanged();

        expListView.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                    values.clear();
                    projected_values(groupPosition);
                listAdapter.notifyDataSetChanged();
                return false;
            }
        });

对于组点击监听器,我从方法“projected_values”给出值。 logcat显示以下错误

09-23 02:45:43.469: E/AndroidRuntime(3563): FATAL EXCEPTION: main
09-23 02:45:43.469: E/AndroidRuntime(3563): Process: com.example.xmlparsing, PID: 3563
09-23 02:45:43.469: E/AndroidRuntime(3563): java.lang.IllegalStateException: The content of the
    adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131296256, class android.widget.ExpandableListView) with Adapter(class android.widget.ExpandableListConnector)]
09-23 02:45:43.469: E/AndroidRuntime(3563):     at android.widget.ListView.layoutChildren(ListView.java:1555)
09-23 02:45:43.469: E/AndroidRuntime(3563):     at android.widget.AbsListView.onTouchUp(AbsListView.java:3617)
09-23 02:45:43.469: E/AndroidRuntime(3563):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3429)
09-23 02:45:43.469: E/AndroidRuntime(3563):     at android.view.View.dispatchTouchEvent(View.java:7690)
09-23 02:45:43.469: E/AndroidRuntime(3563):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
09-23 02:45:43.469: E/AndroidRuntime(3563):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)

请帮帮我

2 个答案:

答案 0 :(得分:2)

首先,不要在Async中创建点击监听器。

错误如下: -

The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes.

“确保不会从后台线程修改适配器的内容,只能从UI线程修改”

在你的帖子中执行: -

    listAdapter = new ExpandableListAdapter(ct, values, scores);
    for(int i=0;i<values.size();i++){

    }
    expListView.setAdapter(listAdapter);
    listAdapter.notifyDataSetChanged();

您正在创建适配器类的新实例,并将其设置为列表视图并同时通知它。 此时没有修改数据,因此您无需在此处调用通知。

答案 1 :(得分:0)

在您的课程的主要活动文件中写下listAdapter.notifyDataSetChanged();,而不是将其写入onPostExecute方法。