如何在Expandable Listview中解决错误“适配器的内容已更改但ListView未收到通知”?

时间:2014-11-14 12:59:57

标签: android listview android-listview

使用可扩展列表视图使用异步任务从 DB 加载数据。当我在可扩展列表视图中加载大约10到15个数据时它的工作正常,当我加载的时候,当我点击父项时它会抛出错误适配器的内容已经改变但是ListView没有收到通知

异步任务类

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

}
}

碎片类

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

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)

我无法解决这个问题。任何人都知道帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

要解决此错误,您需要更新UIonPostExecute()方法中的AsyncTask。因此,您必须将自定义适配器设置为ExpandableListView类的onPostExecute()方法中的All_call_asyc

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

答案 1 :(得分:0)

尝试调用requestLayout,有时它可以在代码出现问题时解决问题:

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

如果它不起作用,请看一下这个问题:

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