Android,synctask在progressDialog之后没有显示列表

时间:2010-11-02 19:19:51

标签: android

我正在使用AsyncTask在List上显示数据,但加载是可见的,但它不显示列表..

  public void getLocations(){
    Connect client = new Connect(SERVICE_URI + "/GetLocations");
    client.AddHeader("Accept", "application/json");
    client.AddHeader("Content-Type", "application/json");

    try {
        client.Execute(RequestMethod.GET);
        JSONObject rootJson = new JSONObject(client.getResponse());
        JSONArray jsonArray = rootJson.getJSONArray("GetLocationsResult");

        String[] names = null;
        if (jsonArray != null) {
            names = new String[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json = jsonArray.getJSONObject(i);
                names[i] = json.getString("Name");
            }
        }

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, names));
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}


  public void getLocations(){
 Connect client = new Connect(SERVICE_URI + "/GetLocations");
    client.AddHeader("Accept", "application/json");
    client.AddHeader("Content-Type", "application/json");

    try {
     client.Execute(RequestMethod.GET);
     JSONObject rootJson = new JSONObject(client.getResponse());
     JSONArray jsonArray = rootJson.getJSONArray("GetLocationsResult");

     String[] names = null;
     if (jsonArray != null) {
         names = new String[jsonArray.length()];
         for (int i = 0; i < jsonArray.length(); i++) {
             JSONObject json = jsonArray.getJSONObject(i);
             names[i] = json.getString("Name");
         }
     }

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, names));
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

如果在AsyncTask的doInBackground()中收集了ListAdapter的数据,则Activity onCreate()不会等待AsyncTask完成。

所以我们有: 1.活动开始 2. AsyncTask启动 3.试图设置ListAdapter的活动 - 它是空的,因为AsyncTask没有完成 4. AsyncTask已完成 - 但ListAdapter填充了空数据

我的解决方案:AsyncTask应在数据加载完成后通知ActivityAdapter:

Public class MyActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.myActivity);
    ExpandableListView listView = (ExpandableListView) findViewById(R.id.expList);

    //MyArrayList - data source for your list. For example, I use static ArrayList from other class. Replace with your data object.

    final MyExpandableListAdapter adapter = new MyrExpandableListAdapter(getApplicationContext(), myArrayList);

    listView.setAdapter(adapter);

    int taskId=1; // type of task should be executed
    MyAsyncTask myTask = new MyAsyncTask(taskId); 
    myTask.execute(adapter);





@Override
protected Dialog onCreateDialog(int id) {

    ProgressDialog progress = null;
    progress = new ProgressDialog(this);

//数据加载时用户是否应该使用app?在我的情况下,不,所以setCancelable = false         progress.setCancelable(假);

    if (id==1) {
        progress.setMessage("Getting locations, please wait...");
    }

    if (id==2) {
        progress.setMessage("Task type #2 performing...");
    }

    return progress;

} // end onCreateDialog



class MyAsyncTask extends AsyncTask<Void, Void, Void> {


    MyExpandableListAdapter adapter; //ExpandableListAdapter you wanna notify about data load completion

    int taskId; //Type of tasks in your Activity. You have only one, however - getLocations().


    MyAsyncTask(int taskId) {

        //save task ID to use in doInBackground, showDialog and dismissDialog
        this.taskId=taskId;

    }


    @Override
    protected Void doInBackground(Void... params) {


            if (taskId==1) {

                // YOUR LONG-TIME TASK #1 THAT UPDATES DATA SOURCE FOR LIST ADAPTER  GOES HERE;

            }


            if (taskId==2) {
                // YOUR LONG-TIME TASK #2 THAT UPDATES DATA SOURCE FOR LIST ADAPTER  GOES HERE;
                // create more task types if needed


            }


            //this adapter will be used in onPostExecute() to notify Activity about data changes
            adapter = (MyExpandableListAdapter) params[0];




        return null;

    }

    @Override
    protected void onPreExecute() {

        //Show dialog for this task type. if need more than one - us switch here and in onCreateDialog, 
        // different dialog types for different tasks etc.

        showDialog(taskId);

    }

    @Override
    protected void onPostExecute(Void result) {

        // IMPORTANT! Update data in Activity
        adapter.notifyDataSetChanged();

        dismissDialog(taskId);

    }


} //end of AsyncTask

} //活动类的结尾