问题
I am quite new working with REST API's.This is the JSON Response i am getting, can anybody please tell me how to loop this JSON Array printing keyword & Creation_date in a listView?
{"result": "ok",
"keywords":
[
{"keyword_id":"3",
"keyword":"keyword1",
"creation_date":"2015-03-12 15:45:12",
"description":"",
"tags":"",
"followers_count":0,
"feedbacks_count":0
},
{"keyword_id":"4",
"keyword":"keyword2",
"creation_date":"2015-03-12 15:45:34",
"description":"",
"tags":"",
"followers_count":0,
"feedbacks_count":0
}
],
"error":""
}
This is what i was trying to do.
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
keyWord = new ArrayList<FollowingKeywords>();
keywordlist = new ArrayList<String>();
try {
JSONObject json = new JSONObject(result);
String Result = json.getString("result");
if (Result.equals("ok")) {
JSONArray jarray = new JSONArray("keywords");
for (int i = 0; i < jarray.length(); i++) {
FollowingKeywords keywords = new FollowingKeywords();
keywords.setKeyword(JSONObject.optString("keyword"));
keywords.setTimestamp(JSONObject.optString("creation_date"));
keyWord.add(keywords);
}
count = jarray.length();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
回答1:
You're almost there, just add this line JSONObject keywordsObject = jarray.getJSONObject(i); into your for loop so that you can get each individual object out of the JSONArray. I've updated the full code snippet code below.
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
keyWord = new ArrayList<FollowingKeywords>();
keywordlist = new ArrayList<String>();
try {
JSONObject json = new JSONObject(result);
String Result = json.getString("result");
if (Result.equals("ok")) {
JSONArray jarray = new JSONArray("keywords");
for (int i = 0; i < jarray.length(); i++) {
JSONObject keywordsObject = jarray.getJSONObject(i);
FollowingKeywords keywords = new FollowingKeywords();
keywords.setKeyword(keywordsObject.optString("keyword"));
keywords.setTimestamp(keywordsObject.optString("creation_date"));
keyWord.add(keywords);
}
count = jarray.length();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
回答2:
if what you want is just to show the values on a listview, you can use SimpleAdapter
listview = (ListView) findViewById(R.id.listview);
ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> listItem;
for (int i = 0; i < jarray.length(); i++) {
listItem = new HashMap<String, Object>();
listItem.put("keyword", keywordsObject.optString("keyword"));
listItem.put("creation_date", keywordsObject.optString("creation_date"));
items.add(listItem);
}
adapter = new SimpleAdapter(getApplicationContext(), items,R.layout.yourlistviewlayout,
new String[] { "keyword", "creation_date" },
new int[] { R.id.textViewKeyword, R.id.textViewCreationDate });
listview.setAdapter(adapter);
and create yourlistviewlayout on your layout folder with two textviews, textViewKeyword and textViewCreationDate
回答3:
Use android.R.layout.simple_list_item_2 as a row item in your ListView. You need to create Custom Adapter so as to show both the text in the ListView. Set both text in getView() method. Also, use ViewHolder pattern for smooth scrolling on ListView.
protected void onPostExecute(String result) {
JSONObject json = null;
try {
json = new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
}
ArrayList<FollowingKeywords> keyWord = new ArrayList<FollowingKeywords>();
JSONArray keywordArray = json.optJSONArray("keywords");
if(keywordArray != null) {
for (int i = 0; i < keywordArray.length(); i++) {
JSONObject keywordObject = keywordArray.optJSONObject(i);
if(keywordObject != null) {
FollowingKeywords keywords = new FollowingKeywords();
keywords.setKeyword(keywordObject.optString("keyword"));
keywords.setTimestamp(keywordObject.optString("creation_date"));
keyWord.add(keywords);
}
}
}
MyAdapter myAdapter = new MyAdapter(this, android.R.layout.simple_list_item_2, keyWord);
ListView listView1 = (ListView) findViewById(R.id.listView1);
listView1.setAdapter(myAdapter);
}
MyAdapter.java
public class MyAdapter extends ArrayAdapter<FollowingKeywords>{
Context context;
List<FollowingKeywords> keywordList;
public MyAdapter(Context context, int resource, ArrayList<FollowingKeywords> keywordList) {
super(context, resource, keywordList);
this.context = context;
this.keywordList = keywordList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if(row == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = (View)inflater.inflate(android.R.layout.simple_list_item_2, null);
holder = new ViewHolder();
holder.keywordText = (TextView)row.findViewById(android.R.id.text1);
holder.creationText = (TextView)row.findViewById(android.R.id.text2);
row.setTag(holder);
} else{
holder = (ViewHolder) row.getTag();
}
holder.keywordText.setText(keywordList.get(position).getKeyword());
holder.creationText.setText(keywordList.get(position).getTimestamp());
return row;
}
static class ViewHolder {
TextView keywordText;
TextView creationText;
}
}
来源:https://stackoverflow.com/questions/28997142/how-to-loop-json-array-and-print-the-values-in-listview-android