2012-03-08 39 views
0

嗨,我擴大了前一個問題,我問只是爲了說清楚。我試圖從一個url獲得一個json feed,然後使用列表適配器創建一個標題列表。我的json feed由一個叫做data的東西組成,裏面有一個新聞數組,裏面有一個叫做title的東西。Android從一個for循環獲取一個字符串到一個setlistAdapter

當我運行該應用程序它強制關閉。我被告知它是因爲我在for循環中有一個字符串,並且沒有外部實例進入適配器。我是新來這個所以任何幫助,將不勝感激我的繼承人代碼

try{ 
      // Create a new HTTP Client 
      DefaultHttpClient defaultClient = new DefaultHttpClient(); 
      // Setup the get request 
      HttpGet httpGetRequest = new HttpGet("jsonfeed"); 

      // Execute the request in the client 
      HttpResponse httpResponse = defaultClient.execute(httpGetRequest); 
      // Grab the response 
      BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8")); 
      String json = reader.readLine(); 


      // Instantiate a JSON object from the request response 
      JSONObject jsonObject = new JSONObject(json); 



      JSONArray jArray = jsonObject.getJSONArray("news"); 

      String oneObjectsItem1 = null; 
      for (int i=0; i < jArray.length(); i++) 
      { 
       JSONObject oneObject = jArray.getJSONObject(i); 
       // Pulling items from the array 

       String oneObjectsItem = oneObject.getString("title"); 



      } 


     } catch(Exception e){ 
      // In your production code handle any errors and catch the individual exceptions 
      e.printStackTrace(); 
     } 





     setListAdapter (new ArrayAdapter<String>(this, R.layout.single_item, oneObjectsItem)); 
     ListView list = getListView(); 
     list.setTextFilterEnabled(true); 
+0

粘貼您的JSON響應字符串太 – waqaslam 2012-03-08 20:39:33

+0

嘿看起來像你使用我的回答你的問題在這裏:http://stackoverflow.com/questions/9605913/如何解析-json-in-android/9606629#9606629,你如何接受它,如果它對你有幫助 – bbedward 2012-03-08 20:43:21

+0

可能的重複[Android setlistAdapter error](http://stackoverflow.com/questions/9623565/android -setlistadapter-error) – Luksprog 2012-03-08 20:44:20

回答

0

你的錯誤是不明確的,因爲我們不知道自己在做什麼,你得到JSON之後,這裏的正確方法來獲取和處理它,和可能的修復的字符串本身。另外,請考慮接受你使用的答案,就像你在這裏的問題一樣:How to parse JSON in Android

其次,你會遍歷整個數組,每次創建一個新的對象。

刪除該

String oneObjectsItem1 = null; 
for (int i=0; i < jArray.length(); i++) 
{ 
    JSONObject oneObject = jArray.getJSONObject(i); 
    // Pulling items from the array 
    String oneObjectsItem = oneObject.getString("title"); 
} 

使用此

JSONArray jArray = MYJSONOBJECT.getJSONArray("JSONARRAYNAME"); 
ArrayList<String> myTitlesFromMyArray = new ArrayList<String>(); 
for (int i=0; i < jArray.length(); i++) 
{ 
    JSONObject oneObject = jArray.getJSONObject(i); 
    // TITLE IS IN MY ARRAY I WANT ALL TITLES IN MY LIST OF TITLES 
    myTitlesFromMyArray.add(oneObject.getString("title")); 
} 

// Iterate through the ENTIRE list and do whatever you want with the items in order 
for (String p: myTitlesFromMyArray) 
{ 
    // Do something with p 
} 

另外,我覺得你的JSON字符串流可能會回來不完整的,以確保它是徹底的改變它

刪除該內容

String json = reader.readLine(); 

使用此

StringBuilder sb = new StringBuilder(); 

String input = null; 
while ((input = reader.readLine()) != null) 
{ 
    sb.append(input + "\n"); 
} 
String json = input.toString(); 
+0

嗨,謝謝你這麼多我會接受一切。這是很好用的在這裏得到一個錯誤String titleFour = myTitlesFromMyArray.get(3);類型不匹配:不能從CharSequence轉換爲字符串 – iamlukeyb 2012-03-08 21:06:37

+0

編輯我的答案,這完全正確你對ArrayList做什麼取決於你? – bbedward 2012-03-08 21:09:44

+0

感謝這幫助我如何獲得字符串p到列表適配器 – iamlukeyb 2012-03-08 22:03:39

0

你需要走出JSON收集您的字符串轉換成一個ArrayList,後來提供了列表對象適配器。見下面的例子:

List<String> items = new ArrayList<String>(); 

try{ 
. 
. 
. 
for (int i=0; i < jArray.length(); i++) 
{ 
    JSONObject oneObject = jArray.getJSONObject(i); 
    String oneObjectsItem = oneObject.getString("title"); 

    if(!TextUtils.isEmpty(oneObjectsItem)) 
    items.add(oneObjectsItem); 
} 
} 


//and set the list object to your adapter 
setListAdapter(new ArrayAdapter<String>(this, R.layout.single_item, items); 
+0

嗨,感謝您的幫助,我試過他的,但它仍然在紅色說,創建本地變量一ObjectsItem – iamlukeyb 2012-03-08 20:48:58

+0

你是什麼意思?詳細說明你的問題 – waqaslam 2012-03-08 20:51:57

+0

這很好,謝謝它不再崩潰,但它沒有在屏幕上顯示任何東西 – iamlukeyb 2012-03-08 20:54:40