0

Am using expandable list view to load the data from DB using asynchronous task. When i load approximate 10 to 15 data in expandable list view its work fine,when i load more than that it throws error when i click the parent item The content of the adapter has changed but ListView did not receive a notification

Asynchronous task class

public class All_call_asyc extends AsyncTask<String, Void, String> {

Activity activity;

Fragment fragment = null;
FragmentManager frgManager;
FragmentTransaction ft;

DatabaseHandler db;
ImageView progress_bar;
AnimationDrawable frameAnimation;

Bundle b = new Bundle();
public All_call_asyc(Activity activity, FragmentManager frgManager, ImageView progress_bar) {
    // TODO Auto-generated constructor stub
    this.activity = activity;
    this.frgManager = frgManager;
    this.progress_bar = progress_bar;
}

protected void onPreExecute() {
    progress_bar.setVisibility(View.VISIBLE);
    frameAnimation = (AnimationDrawable) progress_bar.getBackground();
    frameAnimation.start();
}

protected String doInBackground(String... params) {

    db = new DatabaseHandler(activity);
    Constant.whole_array.clear();
    Constant.whole_array = db.getInfo("ALL",params[0],params[1]);
    db.get_parent_item(params[0],params[1]);

    return "";
}

protected void onPostExecute(String result) {

    if (Constant.parentItems.size()>0) {
        fragment = new All_calls();
        ft = frgManager.beginTransaction();
        ft.replace(R.id.frame_content, fragment);
        ft.addToBackStack(null);
        ft. commitAllowingStateLoss();
    }else{
        fragment = new Error_lay();
        ft = frgManager.beginTransaction();
        ft.replace(R.id.frame_content, fragment);
        ft.addToBackStack(null);
        b.putString("error", "There is no call details");
        fragment.setArguments(b);
        ft. commitAllowingStateLoss();
    }
    frameAnimation.stop();
    progress_bar.setVisibility(View.GONE);

}
}

Fragments Class

public class All_calls extends Fragment{

ExpandableListView lv_allcalls,List_lay;
All_call_exp_adp exp_adp;
FragmentManager frgManager;
int lastExpandedPosition = -1;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    if (container == null) {
        return null;
    }
    List_lay = (ExpandableListView) inflater.inflate(R.layout.all_calls, container, false);

    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int width = metrics.widthPixels;

    frgManager = getActivity().getSupportFragmentManager();
    lv_allcalls = (ExpandableListView)List_lay.findViewById(R.id.all_call_lv);

    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        lv_allcalls.setIndicatorBounds(width - GetPixelFromDips(50), width - GetPixelFromDips(10));
    } else {
        lv_allcalls.setIndicatorBoundsRelative(width - GetPixelFromDips(50), width - GetPixelFromDips(10));
    }

    lv_allcalls.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            if (lastExpandedPosition != -1
                    && groupPosition != lastExpandedPosition) {
                lv_allcalls.collapseGroup(lastExpandedPosition);
            }
            lastExpandedPosition = groupPosition;
        }
    });

    MainActivity.inc_total_time.setText(Constant.inc_going_duration);
    MainActivity.out_total_time.setText(Constant.out_going_duration);

    exp_adp = new All_call_exp_adp(getActivity(),Constant.parentItems,Constant.childData);
    lv_allcalls.setAdapter(exp_adp);
    exp_adp.notifyDataSetChanged();


    return List_lay;
}

public int GetPixelFromDips(float pixels) {
    // Get the screen's density scale
    final float scale = getResources().getDisplayMetrics().density;
    // Convert the dps to pixels, based on density scale
    return (int) (pixels * scale + 0.5f);
}
}

Error in Log-Cat

11-15 10:11:39.405: E/MessageQueue-JNI(26262): 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(2131230810, class android.widget.ExpandableListView) with Adapter(class android.widget.ExpandableListConnector)]
11-15 10:11:39.405: E/MessageQueue-JNI(26262):  at android.widget.ListView.layoutChildren(ListView.java:1555)
11-15 10:11:39.405: E/MessageQueue-JNI(26262):  at android.widget.AbsListView.onTouchUp(AbsListView.java:3624)
11-15 10:11:39.405: E/MessageQueue-JNI(26262):  at android.widget.AbsListView.onTouchEvent(AbsListView.java:3436)
11-15 10:11:39.405: E/MessageQueue-JNI(26262):  at android.view.View.dispatchTouchEvent(View.java:7736)
11-15 10:11:39.405: E/MessageQueue-JNI(26262):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2212)
11-15 10:11:39.405: E/MessageQueue-JNI(26262):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
11-15 10:11:39.405: E/MessageQueue-JNI(26262):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
11-15 10:11:39.405: E/MessageQueue-JNI(26262):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
11-15 10:11:39.405: E/MessageQueue-JNI(26262):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)

am not able to solve this issue.Can any one know help me to solve this issue.

Yugesh
  • 4,030
  • 9
  • 57
  • 97

2 Answers2

1

To overcome with this error, You need to update your UI in onPostExecute() method in your AsyncTask. So means you have to set your custom adapter to your ExpandableListView in onPostExecute() method of your All_call_asyc class.

exp_adp = new All_call_exp_adp(getActivity(),Constant.parentItems,Constant.childData);
lv_allcalls.setAdapter(exp_adp);
exp_adp.notifyDataSetChanged();
Piyush
  • 18,895
  • 5
  • 32
  • 63
  • Its working fine but the little bit delay to show data in expandable list view after the progress bar dismiss. – Yugesh Nov 15 '14 at 17:08
0

Try calling requestLayout, sometimes its fix the problem when it seems nothing wrong with the code :

lv_allcalls.setAdapter(exp_adp);
lv_allcalls.requestLayout();
exp_adp.notifyDataSetChanged();

If its not working, take a look at this question :

Android, ListView IllegalStateException: "The content of the adapter has changed but ListView did not receive a notification"

Community
  • 1
  • 1
Blaze Tama
  • 10,828
  • 13
  • 69
  • 129