Android从for循环获取字符串到setlistAdapter

时间:2012-03-08 20:37:37

标签: android json

嗨我扩展了上一个问题,我只是想让它更清楚。我正在尝试从URL获取json提要,然后使用列表适配器创建标题列表。我的json feed由一个叫做数据的东西组成,里面有一个新闻数组,里面叫做title。

当我运行应用程序时,它会强行关闭。我被告知它因为我在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);

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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();