2011-11-18 47 views
1

這是從我的問題thread exiting error in android異步任務在android系統不工作

我創建了一個異步任務,但該值不會在列表視圖中顯示了一個跟進的問題....

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

問題是,在適配器中的值沒有得到修改...

回答

2

您在AsyncTask中填充listItem後,用空listItems初始化適配器。 onPostExecute(),您的適配器沒有得到更新,試試這個:

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

希望有所幫助。

1

listItems = result;將無法​​正常工作,你需要使用:

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

如果您創建另一個列表,您的適配器將不會知道它,因爲它保留對舊列表的引用(保持不變)。