解析JSON数据转换成的ListView [英] parse JSON data into listView

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

问题描述


  1. 如何让我的JSON数据到我的列表视图?所有我想要做的就是通过JSON数据环和抢前两个元素,并将它们在我的列表视图添加到我的两个文本字段。

但是当我做了code低于它只是把整个数组元素到这两个列表视图支架和所有。它增加只是主阵列不是子项目。 (如果是有道理的)?

下面是我的JSON数据后,奇迹发生了 //循环数组

  [皇牌纹身有限公司,80260,(303)427-3522,461W¯¯第84大街,],[智囊团纹身 ,80209,(720)932-0124,172百老汇,]]

这是我的脚本到目前为止:

 进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;公共类苏州秀工场扩展ListActivity {    私有静态最后弦乐TAG =MyApp的;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.ziplist_layout);         束束= getIntent()getExtras()。
         串shop_data = bundle.getString(shopData);
         Log.v(TAG,shop_data);         ArrayList的<&HashMap的LT;字符串,字符串>> MYLIST =新的ArrayList<&HashMap的LT;字符串,字符串>>();        //获取数据(见上文)
               尝试{
            //获取保存地震元素(JSONArray),其中
                   JSONArray jsonShopArray =新JSONArray(shop_data);
                        ** //循环数组**
                的for(int i = 0; I< jsonArray.length();我++){HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
                JSONArray E = jsonShopArray.getJSONArray(I)                Log.v(TAG,e.toString(I));
                map.put(ID,将String.valueOf(1));
                map.put(名,店铺名称:+ e.toString(2));
                map.put(ZIP code,邮编code:+ e.toString(3));
                mylist.add(地图);
        }
               }赶上(JSONException E){
                 Log.e(log_tag,错误分析数据+ e.toString());
               }               ListAdapter适配器=新SimpleAdapter(这一点,MYLIST,R.layout.shop_list,
                       新的String [] {姓名,拉链code},
                       新的INT [] {R.id.item_title,R.id.item_subtitle});       setListAdapter(适配器);
        /**Toast.makeText(ShowShop.this,子preturn,Toast.LENGTH_LONG).show(); * /    }
}


解决方案

e.toString 是不正确的 http://developer.android.com/reference/org/json/JSONArray.html#toString(INT

您应该使用的getString 调用getInt 或getWhatever

  1. How do I get my JSON data into my listview? All I am trying to do is loop through the JSON data and grab the first two elements and add them to my two text fields in my listview.

BUT when I do the code below it just puts the whole array element into both list views brackets and all. It increments but just the main array not the sub items. (if that makes sense)?

Below is my json data the magic happens after " //Loop the Array":

[["Ace Tattooing Co","80260","(303) 427-3522 ","461 W 84th Ave",""],["Think Tank Tattoo","80209","(720) 932-0124","172 S Broadway",""]]

This is my script so far:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ShowShop extends ListActivity {

    private static final String TAG = "MyApp";  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ziplist_layout);

         Bundle bundle = getIntent().getExtras();
         String shop_data = bundle.getString("shopData");
         Log.v(TAG, shop_data);

         ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

        //Get the data (see above)
               try{
            //Get the element that holds the earthquakes ( JSONArray )
                   JSONArray jsonShopArray = new JSONArray(shop_data);


                        **//Loop the Array**
                for(int i=0;i < jsonArray.length();i++){                        

HashMap<String, String> map = new HashMap<String, String>();
                JSONArray e = jsonShopArray.getJSONArray(i);

                Log.v(TAG, e.toString(i));
                map.put("id",  String.valueOf(1));
                map.put("name", "Store name:" + e.toString(2));
                map.put("zipcode", "Zipcode: " +  e.toString(3));
                mylist.add(map);
        }
               }catch(JSONException e)        {
                 Log.e("log_tag", "Error parsing data "+e.toString());
               }

               ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.shop_list,
                       new String[] { "name", "zipcode" },
                       new int[] { R.id.item_title, R.id.item_subtitle });

       setListAdapter(adapter);




        /**Toast.makeText(ShowShop.this, zipReturn, Toast.LENGTH_LONG).show(); */

    }
}

解决方案

e.toString is incorrect http://developer.android.com/reference/org/json/JSONArray.html#toString(int)

You should use getString or getInt or getWhatever

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

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