嗨,我擴大了前一個問題,我問只是爲了說清楚。我試圖從一個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);
粘貼您的JSON響應字符串太 – waqaslam 2012-03-08 20:39:33
嘿看起來像你使用我的回答你的問題在這裏:http://stackoverflow.com/questions/9605913/如何解析-json-in-android/9606629#9606629,你如何接受它,如果它對你有幫助 – bbedward 2012-03-08 20:43:21
可能的重複[Android setlistAdapter error](http://stackoverflow.com/questions/9623565/android -setlistadapter-error) – Luksprog 2012-03-08 20:44:20