简体   繁体   English

将JSON数据解析为listView

[英]parse JSON data into listView

  1. How do I get my JSON data into my listview? 如何将JSON数据放入列表视图? 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. 我要做的就是遍历JSON数据并获取前两个元素,并将它们添加到列表视图的两个文本字段中。

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 ": 以下是我的json数据,魔术发生在“ //循环数组 ”之后:

[["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 ) e.toString不正确http://developer.android.com/reference/org/json/JSONArray.html#toString(int

You should use getString or getInt or getWhatever 您应该使用getStringgetInt或getWhatever

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM