2011-11-18 5 views
1

Ceci est une question de suivi de ma question thread exiting error in androidAsync Tâche dans Android ne fonctionne pas

J'ai créé une tâche async mais les valeurs ne figurent pas dans la vue de la liste ....

public class History extends Activity implements OnItemClickListener 
{ 
/** Called when the activity is first created. */ 
ListView list; 

//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS 
ArrayList<String> listItems; 

//DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW 
ArrayAdapter<String> adapter; 
private String resDriver,resPassenger,ID; 
private ProgressDialog dialog; 
ArrayList<HashMap<String, Object>> listInfo = new ArrayList<HashMap<String, Object>>(); 
HashMap<String, Object> item; 
JSONObject jDriver; 
//JSONObject jPassenger; 

// Make strings for logging 
private final String TAG = this.getClass().getSimpleName(); 
private final String RESTORE = ", can restore state"; 
private final String state = "Home Screen taking care of all the tabs"; 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    Intent loginIntent = getIntent(); 
    ID = loginIntent.getStringExtra("ID"); 
    listItems = new ArrayList<String>(); 
    Log.i(TAG, "Started view active rides"); 
    setContentView(R.layout.searchresults); 
    list = (ListView)findViewById(R.id.ListView01); 
    list.setOnItemClickListener(this); 
    adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listItems); 

    list.setAdapter(adapter); 
    getInfo(); 
} 
@Override 
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) 
{ 
// TODO Auto-generated method stub 
    Toast.makeText(this, "u clicked " + listItems.get(position) ,Toast.LENGTH_LONG).show(); 
} 

public void getInfo(){ 

    DownloadInfo task = new DownloadInfo(); 
    task.execute(new String[] { "http://www.vogella.de" }); 

} 

private class DownloadInfo extends AsyncTask<String, Void , ArrayList<String>>{ 

    @Override 
    protected ArrayList<String> doInBackground(String ... strings) { 
     ArrayList<String> listItems1; 
     jDriver = new JSONObject(); 
     try { 
      jDriver.put("ID", ID); 
      jDriver.put("task", "GET DATES"); 

     } catch (JSONException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     listItems1 = new ArrayList<String>(); 
     Log.i(TAG,"Sending data for the driver rides"); 
     resDriver = HTTPPoster.sendJson(jDriver,"URL"); // Any Server URL 

     JSONObject driver; 
     try { 
      driver = new JSONObject(resDriver); 
      Log.i(TAG,"Recieved Driver details"); 
      if (driver.getString("DATABASE ERROR").equals("False")){ 
       int length = Integer.parseInt(driver.getString("length")); 
       Log.i(TAG,"length is " + length); 
       for(int i =0 ; i< length ; i++){ 
        String info = driver.getString(Integer.toString(i)); 
        String[] array = info.split(","); 
        Log.i(TAG,"array is " + Arrays.toString(array)); 
        Log.i(TAG,"Date "+ array.length); 
        Log.i(TAG,"DONE WITH THE LOOP"); 
        //listInfo.add(item); 
        Log.i(TAG,"Date is"+array[0]); 
        listItems1.add(array[0]);       
       } 
      } 
     } catch (JSONException e) { 
     // TODO Auto-generated catch block 
      listItems1.add("No driver rides created"); 
     } 
     return listItems1; 
    } 

    @Override 
    protected void onPostExecute(ArrayList<String> result) { 

     listItems = result; 
     adapter.notifyDataSetChanged(); 
    } 
} 
} 

le problème est que les valeurs de l'adaptateur ne sont modifiés ...

Répondre

2

Vous initialisez votre adaptateur avec un élément ListItem vide, après avoir rempli vos éléments ListItems dans AsyncTask. onPostExecute(), votre adaptateur n'est pas mis à jour, essayez ceci:

protected void onPostExecute(ArrayList<String> result) { 
    listItems = result; 
    adapter.addAll(ListItems); 
    adapter.notifyDataSetChanged(); 
} 

Espérons que cette aide.

1

listItems = result; ne fonctionnera pas non, vous devez utiliser:

listItems.clear(); 
listItems.addAll(result); 

Si vous créez une autre liste, votre adaptateur ne le connaîtra pas, car il conserve une référence à l'ancienne liste (qui reste la même).

Questions connexes