问题描述
我尝试搜索将JSONObject
转换为HashMap
,但是大多数结果是针对Java而不是Android.因此,希望您之前有经验的人可以分享.
I try to search converting JSONObject
to HashMap
but most of the results are for Java not Android. Hence, I hope someone can share if you have experience in doing this before.
listview_with_simpleAdapter_and_hashmap.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
String[] food_id= new String[]{"1", "2", "3"};
String[] food_name = new String[]{"apple", "orange", "banana"};
List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 3; i++) {
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("ID", food_id[i]);
hm.put("Name", food_name[i]);
aList.add(hm);
}
String[] from = {"ID", "Name"};
int[] to = {R.id.text_id, R.id.text_name};
SimpleAdapter adapter = new SimpleAdapter(this, aList, R.layout.list_item, from, to);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
此文件运行正常,只需在每行中显示2列;
json.java
TextView mTxtDisplay;
String url = "http://192.168.1.103/web_service/omg.php/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTxtDisplay = (TextView) findViewById(R.id.tv);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
mTxtDisplay.setText(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsObjRequest);
192.168.1.103/web_service/omg.php/
{
"32":"Western Food",
"35":"Japanese Food",
"37":"Italian Food"
}
JSON也可以正常工作.格式与ListView
数据-> ID和名称完全相同.
JSON is working fine as well. The format is exactly the same as the ListView
data -> ID and Name.
所以我的问题是如何将 omg.php 中的JSONObject
转换为listview_with_simpleAdapter_and_hashmap.java?我只需要一个简单的例子.
So my question is how to convert the JSONObject
in omg.php to listview_with_simpleAdapter_and_hashmap.java ? I just need a simple example.
推荐答案
您可以执行以下操作:
ListView listView = (ListView) findViewById(R.id.listView);
// ...
@Override
public void onResponse(JSONObject response) {
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
try {
Iterator<String> iterator = response.keys();
while (iterator.hasNext()) {
String key = iterator.next();
String value = response.getString(key);
HashMap<String, String> map = new HashMap<>();
map.put(KEY_ID, key);
map.put(KEY_NAME, value);
list.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
if(list.size() > 0) {
String[] from = {KEY_ID, KEY_NAME};
int[] to = {R.id.text_id, R.id.text_name};
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list,
R.layout.list_item, from, to);
listView.setAdapter(adapter);
}
}
这篇关于Android将JSONObject转换为HashMap并使用SimpleAdapter在ListView中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!