2012-05-15 58 views
0

嗨,朋友,我只是希望數據顯示在列表視圖我使用異步任務,我完成獲取數據在JSON和篩選它的ID和標題現在我顯示ID和標題在列表視圖可以幫助我提前感謝在AsyncTask我想列表視圖中的數據

public class runActivity extends Activity implements OnClickListener { 
    String returnString=""; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    findViewById(R.id.my_button).setOnClickListener(this); 
} 

@Override 
public void onClick(View arg0) { 
    Button b = (Button)findViewById(R.id.my_button); 
    b.setClickable(false); 
    new LongRunningGetIO().execute(); 
} 

private class LongRunningGetIO extends AsyncTask <Void, Void, String> { 

    protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException { 
     InputStream in = entity.getContent(); 
     StringBuffer out = new StringBuffer(); 
     int n = 1; 
     while (n>0) { 
      byte[] b = new byte[4096]; 
      n = in.read(b); 
      if (n>0) out.append(new String(b, 0, n)); 
     } 
     return out.toString(); 
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpGet httpGet = new HttpGet("http://192.168.1.156/recess/document/document.json"); 
     HttpClient client = new DefaultHttpClient(); 
     HttpResponse response=null; 
     try{ 
      response = client.execute(httpGet);} 
     catch(Exception e){} 
     System.out.println(response.getStatusLine()); 
     String text = null; 
     try { 
       response = httpClient.execute(httpGet, localContext); 
       HttpEntity entity = response.getEntity(); 
       text = getASCIIContentFromEntity(entity); 
     } catch (Exception e) { 
      return e.getLocalizedMessage(); 
     } 
     String var =text;    
     try{ 
      JSONObject jObj = new JSONObject(var); 
      JSONArray jArray = jObj.getJSONArray("document"); 
      for(int i=0;i<jArray.length();i++){ 

        JSONObject json_data = jArray.getJSONObject(i); 
        Log.i("log_tag","id: "+json_data.getString("id")+ 
          ", title: "+json_data.getString("title") 
        ); 
        returnString += "\n" +"id:"+ json_data.getString("id")+" "+"Title:"+    json_data.getString("title"); 

        } 


    } 
    catch(JSONException e){ 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 
     return returnString; 
    } 

    protected void onPostExecute(String results) { 
     if (results!=null) { 
      ListView listView = (ListView) findViewById(R.id.mylist); 
      listView.setFilterText(results); 
     } 
     Button b = (Button)findViewById(R.id.my_button); 
     b.setClickable(true); 
    } 
} 
} 

回答

0

我認爲最好的解決方案是在您的活動中創建一個Handler。然後,您可以向處理程序發送消息並獲取數據並將其放入ListView中。

0

在doInBackground「for」循環只是既可以創建數據的數組或數據放在對象的數組列表(這時就需要編寫自定義適配器)

對於 1-選項 http://www.java-samples.com/showtutorial.php?tutorialid=1516 http://www.ezzylearning.com/tutorial.aspx?tid=1659127 & q =綁定Android的列表視圖與 - 串陣列使用-arrayadapter

對於 2 - 選項

http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter

+0

我的數據轉換陣列dthanks,但我不能在列表視圖中顯示 – Mohit

+0

setListAdapter(新ArrayAdapter ( 此, 機器人.R.layout.simple_expandable_list_item_1, items)); 其中item是你的字符串數組。 –

+0

setListAdapter(new ArrayAdapter (this,android.R.layout.showalbooks,list1));它向我showal錯誤showalbooks它的XML文件名和錯誤是showalbooks無法解析或不是一個字段 – Mohit

相關問題