可扩展的listview notifydatasetchanged()在asynchtask

时间:2015-04-28 09:42:34

标签: android

我的活动包括一个ExpandableListView和几个按钮以及底部的文本视图,如下图所示。

enter image description here

当页面加载时,listview将根据TextView中的日期填充。它工作正常。当我单击下一个或上一个按钮时,它应该根据下一个或上一个日期更新列表视图,但我不在哪里调用notifydatasetchanged或如何。我在页面创建和按钮单击时调用相同的AsynchTask,并以日期作为参数。当我点击任何按钮时,应用程序崩溃。我看到了其他类似的主题,但找不到解决方案。任何帮助,将不胜感激。

问候。

public class GetPatientVitalsTask extends AsyncTask<String, String, String> {

    public VitalListActivity vitalsActivity;
    private Activity activity;
    private Context context;
    private View rootView;
    private GogchProgressDialog progressDialog;

    public GetPatientVitalsTask(VitalListActivity activity, View rootView){
        super();
        this.activity = activity;
        this.rootView = rootView;
        this.context = this.activity.getApplicationContext();
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new GogchProgressDialog(activity, R.drawable.gogch_progress_spinner);
        progressDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        String sDate = params[0];
        String message = null;
        String url = "http://test.gogch.com/gch-restful/vitals/calender?date=" + sDate;
        HttpClient httpClient = ServiceHelper.getHttpClient();
        HttpContext localContext = new BasicHttpContext();

        HttpPost httpPost = new HttpPost(url);
        try {
            HttpResponse response = httpClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            message = EntityUtils.toString(response.getEntity());
            Log.d("Http Post Response:", message);
        }
        catch(ClientProtocolException e){
            e.printStackTrace();
        }
        catch (IOException e){
            e.printStackTrace();
        }
        return message;
    }

    protected void onPostExecute(String responseText) {
        ExpandableListView expListView = (ExpandableListView) rootView.findViewById(R.id.vitalsListView);
        final List<Vitals> vitalsList = VitalService.buildVitalsList(responseText);

        VitalListAdapter vitalAdapter = null;
        vitalAdapter = new VitalListAdapter(activity, vitalsList);
        expListView.setAdapter(vitalAdapter);
        vitalAdapter.notifyDataSetChanged();
        progressDialog.dismiss();
    }
}

logcat的

04-28 15:09:22.627    4654-4654/com.cinch.gogch E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.cinch.gogch, PID: 4654
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)' on a null object reference
            at com.cinch.gogch.tasks.GetPatientVitalsTask.onPostExecute(GetPatientVitalsTask.java:89)
            at com.cinch.gogch.tasks.GetPatientVitalsTask.onPostExecute(GetPatientVitalsTask.java:36)
            at android.os.AsyncTask.finish(AsyncTask.java:636)
            at android.os.AsyncTask.access$500(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:0)

用于在您的Activty中声明: final ExpandableListView expListView =(ExpandableListView)rootView.findViewById(R.id.vitalsListView);

并使用Asynctask中的expListView来设置适配器