Android 如何将json数据设置为列表视图?
我试图将json数据添加到listview,但我不知道如何将json数据添加到列表适配器Android 如何将json数据设置为列表视图?,android,android-listview,Android,Android Listview,我试图将json数据添加到listview,但我不知道如何将json数据添加到列表适配器 try { mylist = new ArrayList<HashMap<String, String>>(); HashMap<String, String> map = new HashMap<String, String>(); JSONObject jsonResponse = new JSONObject(s
try {
mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jsonResponse = new JSONObject(strJson1);
JSONArray jsonMainNode = jsonResponse.optJSONArray("restaurants");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String restName = jsonChildNode.optString("name");
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
if(restName.equalsIgnoreCase(name)){//this name is predefined name.
String address = jsonChildNode.optString("address");
String mobile = jsonChildNode.optString("mobile");
String direction = "Direction: "+jsonChildNode.optString("direction");
String bestTime = "Best time to visite: "+jsonChildNode.optString("bestTime");
String food = jsonChildNode.optString("food");
String dress = jsonChildNode.optString("dress");
String priceRange = "Price Range: "+jsonChildNode.optString("priceRange");
String rate = jsonChildNode.optString("Rate");
String comment = "Price Range: "+jsonChildNode.optString("comment");
map.put("restName",restName);
map.put("address",address);
map.put("mobile",mobile);
map.put("direction",direction);
map.put("bestTime",bestTime);
map.put("food",food);
map.put("dress",dress);
map.put("priceRange",priceRange);
map.put("rate",rate);
map.put("comment",comment);
mylist = new ArrayList<HashMap<String, String>>();
mylist.add(map);
}else{
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
}
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error..." + e.toString(),
Toast.LENGTH_LONG).show();
}
// ListAdapter adapter = new SimpleAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,mylist);
// list.setAdapter(adapter);
}
有人能告诉我如何将此数据设置为列表视图吗?试试这个
你的做法是相反的。在for循环之前取消对arraylist的清除,并在for循环中取消对HashMap的清除
谷歌gson
我会用gson来做这个。查看有关如何操作的更多详细信息。这是对象序列化/反序列化的示例:
对象示例
class BagOfPrimitives {
private int value1 = 1;
private String value2 = "abc";
private transient int value3 = 3;
BagOfPrimitives() {
// no-args constructor
}
}
系列化
BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj);
BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);
==>json是{value1:1,value2:abc}
请注意,不能使用循环引用序列化对象,因为这将导致无限递归
反序列化
BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj);
BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);
==>obj2就像obj@anuruddhika,然后再次初始化内部if条件,这样每次循环运行时都会再次清除或重新初始化。@anuruddhika-hmm k您在哪里将mylist填充到listview代码的。你能把代码贴出来吗?我想这应该是问题所在。我没有在这里使用适配器类。我在代码中以注释的形式提到。@anuruddhika您应该使用自定义适配器。您需要在listview中显示哪些数据。让我们看看您可以在这里找到答案=>您应该使用自定义适配器。您解决了问题吗@阿努鲁迪卡