.notifydatasetchanged在android的可扩展列表视图中不工作
问题描述:
在xml解析中,我在刷新可扩展列表视图中的数据后调用了notifydatasetchanged,但应用程序崩溃。而我的适配器类正在扩展BaseExpandableListAdapter。 我用下面的代码:.notifydatasetchanged在android的可扩展列表视图中不工作
从方法“projected_values”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;
}
});
为组点击收听,我给的值。以下错误的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)
请帮我
答
首先,不要在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();
您正在创建适配器类的新实例,并将其设置为listview并在相同的位置通知它 时间。 此时没有数据修改,因此您无需在此处致电通知。
答
你们班的主要活动文件写listAdapter.notifyDataSetChanged();
而不是在onPostExecute
方法写它。
我修改了我的代码..在Runonuithread中编写了notifydatasetchanged,并在oncreate中编写了onclicklisteners ..但它给出了相同的错误 – Chowdary102 2014-09-23 10:00:48