2013-09-27 456 views
2

我正在將我的HTTP調用轉換爲來自同步調用的異步調用。因爲連接在後臺運行,所以當我最初設置我的列表適配器時,數據不在那裏。我怎樣才能讓我的HTTP調用後更新列表適配器?我已經嘗試了一些東西,例如在數據發回並重新設置適配器之前不設置適配器,但沒有任何工作。這裏是我當前的OnCreate代碼在異步更新後更新Android Listview

protected void onCreate(Bundle savedInstanceState) { 
    overridePendingTransition(R.layout.anim_out,R.layout.anim_in); 
    setTheme(R.style.Theme_D_theme); 
    setContentView(R.layout.news_fragment); 
    super.onCreate(savedInstanceState); 

    text = (TextView) this.findViewById(R.id.nomore); 
    list = (ListView) this.findViewById(R.id.newslist); 
    list.setDividerHeight(2); 

    new MyAsyncTask().execute("THIS"); 



     list.setOnItemClickListener(this); 
     list.setCacheColorHint(Color.WHITE); 
     adapter = new SimpleAdapter(this, x, R.layout.drill1_listview, 
       new String[] {"title","content","distance", "image"}, 
       new int[] {R.id.title, R.id.content, R.id.distance, R.id.image}); 

} 

而且我AsyncTast

private class MyAsyncTask extends AsyncTask<String, Integer, Double>{ 

    @Override 
    protected Double doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     postData(params[0]); 
     return null; 
    } 

    protected void onPostExecute(Double result){ 
     //pb.setVisibility(View.GONE); 
     Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show(); 
    } 
    protected void onProgressUpdate(Integer... progress){ 
     //pb.setProgress(progress[0]); 
    } 

    public void postData(String valueIWantToSend) { 


     ThreadPolicy tp = ThreadPolicy.LAX; 
     StrictMode.setThreadPolicy(tp); 

     HttpClient httpclient = new DefaultHttpClient();   
     String type = getIntent().getExtras().getString("type"); 
     HttpGet httppost = new HttpGet("http://myip.../all?latitude=42.12345&longitude=-76.2154"); 



     InputStream inputStream = null; 
     try {    
      HttpResponse response = httpclient.execute(httppost);   
      HttpEntity entity = response.getEntity(); 
      String JSONstring = EntityUtils.toString(entity); 
      Log.w("HTTPRESPONSE", JSONstring); 

      //Toast.makeText(this, JSONstring, Toast.LENGTH_SHORT).show(); 

      if (!(JSONstring.equals(" 0"))) { 




      JSONArray array = new JSONArray(JSONstring); 
      for (int i = 0; i < array.length(); i++) { 
       JSONObject row = array.getJSONObject(i); 
       HashMap<String,String> temp = new HashMap<String,String>(); 




       String thisid = row.getString("id"); 
       ider.add(thisid); 

       String starthold = row.getString("start_time"); 
       start.add(starthold); 

       String endhold = row.getString("end_time"); 
       end.add(endhold); 

       String latitudehold = row.getString("latitude"); 
       latitude.add(latitudehold); 

       String longitudehold = row.getString("longitude"); 
       longitude.add(longitudehold); 

       String addresshold = row.getString("street"); 
       address.add(addresshold); 

       String title = row.getString("company"); 
       company.add(title); 
       temp.put("title", title); 

       String distance_hold = row.getString("distance"); 
       distance.add(distance_hold); 
       temp.put("distance", distance_hold); 


       int[] images = new int[] { R.drawable.local_eat,R.drawable.local_eat, 
         R.drawable.local_drink, R.drawable.local_shop, 
         R.drawable.local_do, R.drawable.local_chance, 
         R.drawable.local_all }; 

       String image = row.getString("promo_type"); 
       temp.put("image", Integer.toString(images[Integer.valueOf(image)])); 

       String description = row.getString("name"); 
       name.add(description); 
       temp.put("content", description); 

       x.add(temp); 

      } 


      } 
     } catch (Exception e) { 
      // Oops 
     } finally { 
      try{if(inputStream != null)inputStream.close();}catch(Exception squish){} 
     }   


    } 

} 
+0

發表您的AsyncTask以及 –

+0

@BirajZalavadia添加 – centreee

回答

7

在執行後加入這一行。到底。

x.add(temp); 
adapter.notifyDataSetChanged(); 
2

做這樣

private class MyAsyncTask extends AsyncTask<String, Integer, Double>{ 

    @Override 
    protected Double doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     postData(params[0]); 
     return null; 
    } 

    protected void onPostExecute(Double result){ 
     //pb.setVisibility(View.GONE); 
     Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show(); 
    //UPDATE HERE 
    adapter = new SimpleAdapter(this, x, R.layout.drill1_listview, 
       new String[] {"title","content","distance", "image"}, 
       new int[] {R.id.title, R.id.content, R.id.distance, R.id.image}); 
    list.setAdapter(adapter); 

    } 
    protected void onProgressUpdate(Integer... progress){ 
     //pb.setProgress(progress[0]); 
    } 

    public void postData(String valueIWantToSend) { 


     ThreadPolicy tp = ThreadPolicy.LAX; 
     StrictMode.setThreadPolicy(tp); 

     HttpClient httpclient = new DefaultHttpClient();   
     String type = getIntent().getExtras().getString("type"); 
     HttpGet httppost = new HttpGet("http://myip.../all?latitude=42.12345&longitude=-76.2154"); 



     InputStream inputStream = null; 
     try {    
      HttpResponse response = httpclient.execute(httppost);   
      HttpEntity entity = response.getEntity(); 
      String JSONstring = EntityUtils.toString(entity); 
      Log.w("HTTPRESPONSE", JSONstring); 

      //Toast.makeText(this, JSONstring, Toast.LENGTH_SHORT).show(); 

      if (!(JSONstring.equals(" 0"))) { 




      JSONArray array = new JSONArray(JSONstring); 
      for (int i = 0; i < array.length(); i++) { 
       JSONObject row = array.getJSONObject(i); 
       HashMap<String,String> temp = new HashMap<String,String>(); 




       String thisid = row.getString("id"); 
       ider.add(thisid); 

       String starthold = row.getString("start_time"); 
       start.add(starthold); 

       String endhold = row.getString("end_time"); 
       end.add(endhold); 

       String latitudehold = row.getString("latitude"); 
       latitude.add(latitudehold); 

       String longitudehold = row.getString("longitude"); 
       longitude.add(longitudehold); 

       String addresshold = row.getString("street"); 
       address.add(addresshold); 

       String title = row.getString("company"); 
       company.add(title); 
       temp.put("title", title); 

       String distance_hold = row.getString("distance"); 
       distance.add(distance_hold); 
       temp.put("distance", distance_hold); 


       int[] images = new int[] { R.drawable.local_eat,R.drawable.local_eat, 
         R.drawable.local_drink, R.drawable.local_shop, 
         R.drawable.local_do, R.drawable.local_chance, 
         R.drawable.local_all }; 

       String image = row.getString("promo_type"); 
       temp.put("image", Integer.toString(images[Integer.valueOf(image)])); 

       String description = row.getString("name"); 
       name.add(description); 
       temp.put("content", description); 

       x.add(temp); 

      } 


      } 
     } catch (Exception e) { 
      // Oops 
     } finally { 
      try{if(inputStream != null)inputStream.close();}catch(Exception squish){} 
     }   


    } 

} 
+0

當我這樣做,我得到的R.id.title,R.id.content,R.id.distance,R.id.image不要錯誤t認爲它出於某種原因認識到了R – centreee