2010-11-02 3 views
1

j'utilisais AsyncTask pour afficher les données sur une liste, mais le chargement est visible, mais il ne marche pas afficher la liste ..Android, synctask ne montre pas la liste après ProgressDialog

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(); 
    } 
} 
+0

Vous collé le code deux fois? – Cristian

+1

Eh bien la réponse évidente est que vous lancez une exception ou jsonArray.length() est 0 – Falmarri

Répondre

0

Si les données pour ListAdapter est collecté dans doInBackground() de AsyncTask, Activity onCreate() n'attend pas que AsyncTask se termine.

donc ce que nous avons: 1. Activité Début 2. AsyncTask démarrage 3. Activité essayant de mettre ListAdapter - il est vide, parce que AsyncTask n'a pas terminé 4. AsyncTask terminé - mais ListAdapter rempli de données vides

Ma solution: AsyncTask doit en informer ListAdapter de l'activité après le chargement de données complète:

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

// travail de l'utilisateur avec l'application en doit-chargement des données? Dans mon cas, non, setCancelable = false progress.setCancelable (false);

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 

} // Fin de l'activité classe

Questions connexes