Search code examples
androidexpandablelistviewnotifydatasetchanged

.notifydatasetchanged not working in expandable listview in android


In xml parsing, i had called notifydatasetchanged after refreshing the data in my expandable listview., but the app is getting crashed. and my adapter class is extending BaseExpandableListAdapter. I had used the following code:

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;
            }
        });

for group click listener, i am giving values from a method "projected_values". The logcat shows following error

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)

please help me out


Solution

  • First of all, don't create click listener's in Async.

    The error is below :-

    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.
    

    "Make sure the content of your adapter is not modified from a background thread, but only from the UI thread"

    In your post execute :-

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

    You are creating a new instance of your adapter class and setting it to a listview and notifying it at the same time. At this point there is no data modified so you need not to call notify here.