如何JSONArray转换成的ListView? [英] How to convert JSONArray to ListView?

查看:133
本文介绍了如何JSONArray转换成的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code这不下面 -

I have a code which does following -

  1. 通过HttpClient的连接到Web服务到PHP文件
  2. 从一个SQL查询返回的结果
  3. 在返回格式是jArray(一JSONArray),其中

for(int i=0; i < jArray.length() ; i++) {
    json_data = jArray.getJSONObject(i);
    int id=json_data.getInt("id");
    String name=json_data.getString("name");
    Log.d(name,"Output");
}

当我看着LogCat中,我看到查询所有的名,每条记录都打印出来。我只需要插入这些结果变成一个ListView。我怎样才能做到这一点?

When I look at the LogCat, I see all the "names" of the query, Each record is printed. I just need to plug these results into a ListView. How can I accomplish this ?

PS - 我没有一个ArrayAdapter一个单独的类。难道这是什么原因呢?

PS - I do not have a separate class for an ArrayAdapter. Could this be the reason ?

推荐答案

如果你只是想显示textViews列表你不需要重写任何东西,你可以添加所有的项目到一个ArrayList和使用arrayAdapter。

If you just want to display a list of textViews you don't need to override anything, you can just add all of the items into an arrayList and use an arrayAdapter.

把你的XML列表视图名为Android的:清单,然后用您要使用的TextView的创建arrayAdapter

Put a list view in your xml that is named android:list and then create your arrayAdapter with the textView you want to use.

在所有你所要做的就是调用setListAdapter(mArrayAdapter),它应该填充您的列表中。

After that all you have to do is call setListAdapter(mArrayAdapter) and it should populate your list.

ArrayList<String> items = new ArrayList<String>();
for(int i=0; i < jArray.length() ; i++) {
    json_data = jArray.getJSONObject(i);
    int id=json_data.getInt("id");
    String name=json_data.getString("name");
    items.add(name);
    Log.d(name,"Output");
}

ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this,  
           android.R.layout.simple_expandable_list_item_1, items));
setListAdapter(mArrayAdapter)

希望这有助于!

hope this helps!

这篇关于如何JSONArray转换成的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆