- 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
精彩评论