簡體   English   中英

將JSON數據解析為listView

[英]parse JSON data into listView

  1. 如何將JSON數據放入列表視圖? 我要做的就是遍歷JSON數據並獲取前兩個元素,並將它們添加到列表視圖的兩個文本字段中。

但是,當我執行下面的代碼時,它只是將整個數組元素放入兩個列表視圖括號中。 它增加,但僅是主數組而不是子項。 (如果說得通)?

以下是我的json數據,魔術發生在“ //循環數組 ”之后:

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

到目前為止,這是我的腳本:

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

您應該使用getStringgetInt或getWhatever

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM