获取name
我的Toast
中的值onItemClick
我的ListView
我可以做,但是如何获得code
值?如何在我的JSON对象中获取相应的JSON值?
我有一个JSON数组,看起来像这样:
[
{
"code": "123",
"name": "Jim"
},
{
"code": "456",
"name": "Sally"
},
{
"code": "789",
"name": "Bob"
},
{
"code": "012",
"name": "Jill"
},
etc...
]
我得到的name
值在我ListView
这样的:
public void onStart(){
super.onStart();
// Create json array request
JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Request.Method.POST,
"http://www.nameofsite.com/nameoffile.php", (JSONArray)null, new Response.Listener<JSONArray>(){
public void onResponse(JSONArray jsonArray){
// Successfully download json
// So parse it and populate the listview
for(int i=0;i<jsonArray.length();i++){
try {
JSONObject jsonObject=jsonArray.getJSONObject(i);
items.add(jsonObject.getString("name"));
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("Error", "Unable to parse json array");
}
});
// Create request queue
RequestQueue requestQueue= Volley.newRequestQueue(this);
// add json array request to the request queue
requestQueue.add(jsonArrayRequest);
}
在
onItemClick
和name
Toast
这样的:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView myTextView = (TextView) view.findViewById(R.id.txt);
String text = myTextView.getText().toString();
Toast.makeText(getApplicationContext(),text, Toast.LENGTH_LONG).show();
}
});
但我想要toast
中的code
值,而不是name
。我该怎么做?
检查异常,而你解析JSON。从代码它似乎是好的。 或添加日志以便更好地理解。 –