0
  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(); */

    }
}
5
  • Where do you define map variable? Commented Aug 23, 2011 at 1:18
  • Sorry left it out when i was coping it over. Updated. Commented Aug 23, 2011 at 1:21
  • jsonShopArray is actually jsonArray? :) Commented Aug 23, 2011 at 1:24
  • Are you using any library like jackson or gson ? Commented Aug 23, 2011 at 1:25
  • Updated to show the libraries you are correct Nikita. Commented Aug 23, 2011 at 1:29

1 Answer 1

3

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

You should use getString or getInt or getWhatever

Sign up to request clarification or add additional context in comments.

1 Comment

Get that was my issues. Thanks +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.