在异步任务中调用adapter.notifyDataSetChanged的位置?

时间:2015-04-24 12:11:35

标签: android

我知道为什么会出现如下所示的错误 (代码中缺少adapterD.notifyDataSetChanged();语句)但我不确切地知道在哪里调用此方法?

AsyncTask

public class GetAllCustomerTask2 extends AsyncTask<ApiConnector, Long, Void>{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        next.setVisibility(View.GONE);
        previous.setVisibility(View.GONE);
    }
    @Override
    protected Void doInBackground(ApiConnector... params) {
        List<NameValuePair> nameValuePairss = new ArrayList<NameValuePair>(1);
        nameValuePairss.add(new BasicNameValuePair("token",token));
        nameValuePairss.add(new BasicNameValuePair("pageSize",pageSize+""));
        nameValuePairss.add(new BasicNameValuePair("pageNo",pageNumber+""));
        try {
            if(!isCancelled()) {
                jsonString = jsonParser.makeHttpRequest(url2+driverId+"/bookings", "GET", nameValuePairss);
                jsonObject=new JSONObject(jsonString);
                jsonResponseArray = jsonObject.getJSONArray("response");
                if(jsonResponseArray!=null){
                    DutiesArrayList.clear();
                    duties.clear();
                    for (int i = 0;
                    i < jsonResponseArray.length();
                    i++) {
                        Duties d=new Duties();
                        JSONObject c = jsonResponseArray.getJSONObject(i);
                        destinationLabel = c.getString("destinationLabel");
                        if (destinationLabel.length() <= 4) {
                            destinationLabel="";
                        }
                        // if(destinationLabel.length()==0 || destinationLabel.length()==4 ){
                            String iddD = c.getString("id");
                            d.setid(iddD);
                            String pickupLabelD = c.getString("pickupLabel");
                            d.setpickupLabel(pickupLabelD);
                            String statusD = c.getString("status");
                            d.setstatus(statusD);
                            d.setdestinationLabel(destinationLabel);
                            duties.add(d);
                            // destinationLabelD = c.getString("destinationLabel");
                            // if(statusD!="pending"){
                                HashMap<String, String> contactD = new HashMap<String, String>();
                                // contact.put(TAG_ID, Status);
                                contactD.put(D1, iddD);
                                contactD.put(D2, pickupLabelD);
                                contactD.put(D3, statusD);
                                contactD.put(D4, destinationLabel);
                                DutiesArrayList.add(contactD);
                                // Collections.reverse(DutiesArrayList);
                                //
                            }
                            //
                        }
                    }
                    count=DutiesArrayList.size();
                }
                else {
                    Toast.makeText(getActivity(), "No More Duties", Toast.LENGTH_LONG).show();
                }
            }
            else  Toast.makeText(getActivity(), "Cancelled bhai cancelled",Toast.LENGTH_LONG).show();
        }
        catch (JSONException e) {
            // TODO Auto-generated catch block  e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void jsonArray) {
        if(getActivity()!=null){
            if(tap.equals("2")){
                tap="1";
            }
            next.setVisibility(View.VISIBLE);
            previous.setVisibility(View.VISIBLE);
            if(pageNumber>1){
                previous.setVisibility(View.VISIBLE);
            }
            else {
                previous.setVisibility(View.GONE);
            }
            if(count<10){
                next.setVisibility(View.GONE);
                previous.setVisibility(View.VISIBLE);
            }
            adapterD = new DutiesAdpter(getActivity(), duties);
            lvDuties.setAdapter(adapterD);
            adapterD.notifyDataSetChanged();
            lvDuties.setEmptyView(getView().findViewById(R.id.emptyElementD));
        }
        else{
            gtask2.cancel(true);
            gtask.cancel(true);
        }
    }
}

堆栈追踪:

04-24 17:14:11.325: E/AndroidRuntime(8432): FATAL EXCEPTION: main
04-24 17:14:11.325: E/AndroidRuntime(8432): 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. [in ListView(2131099863, class com.example.driverapp.PullToRefreshListView) with Adapter(class android.widget.HeaderViewListAdapter)]
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.widget.ListView.layoutChildren(ListView.java:1548)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.widget.AbsListView.onLayout(AbsListView.java:2037)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.View.layout(View.java:14118)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.ViewGroup.layout(ViewGroup.java:4467)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.View.layout(View.java:14118)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.ViewGroup.layout(ViewGroup.java:4467)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.View.layout(View.java:14118)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.ViewGroup.layout(ViewGroup.java:4467)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.View.layout(View.java:14118)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.ViewGroup.layout(ViewGroup.java:4467)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1594)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.View.layout(View.java:14118)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.ViewGroup.layout(ViewGroup.java:4467)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.View.layout(View.java:14118)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.ViewGroup.layout(ViewGroup.java:4467)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.View.layout(View.java:14118)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.ViewGroup.layout(ViewGroup.java:4467)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.View.layout(View.java:14118)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.ViewGroup.layout(ViewGroup.java:4467)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.View.layout(View.java:14118)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.ViewGroup.layout(ViewGroup.java:4467)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2183)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1947)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4872)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.Choreographer.doCallbacks(Choreographer.java:579)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.Choreographer.doFrame(Choreographer.java:548)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.os.Handler.handleCallback(Handler.java:800)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.os.Handler.dispatchMessage(Handler.java:100)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.os.Looper.loop(Looper.java:194)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at android.app.ActivityThread.main(ActivityThread.java:5371)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at java.lang.reflect.Method.invoke(Method.java:525)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-24 17:14:11.325: E/AndroidRuntime(8432):     at dalvik.system.NativeStart.main(Native Method)

请帮助:)

2 个答案:

答案 0 :(得分:0)

尝试添加此内容。它应该从主线程调用。

runOnUiThread(new Runnable() {

    @Override
    public void run() {
    lvDuties.setAdapter(adapterD);
    adapterD.notifyDataSetChanged();
    }
});

答案 1 :(得分:0)

将适配器设置为listview,在其中初始化listview,而不是在后台线程中。每当您的数据集发生更改时,请在后台线程中使用notifyDataSetChanged()更新适配器。