我很熟悉REST API。这是我得到的JSON响应, 任何人都可以告诉我如何循环这个JSON阵列打印关键字& listView中的Creation_date?
{"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":""
}
这就是我想要做的事情。
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();
}
}
答案 0 :(得分:0)
您几乎就在那里,只需将此行JSONObject keywordsObject = jarray.getJSONObject(i);
添加到您的for循环中,以便您可以从JSONArray中获取每个单独的对象。我已经更新了下面的完整代码段代码。
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();
}
}
答案 1 :(得分:0)
如果你想要的只是在列表视图上显示值,你可以使用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);
并在布局文件夹中创建yourlistviewlayout,其中包含两个textview,textViewKeyword和textViewCreationDate
答案 2 :(得分:0)
将android.R.layout.simple_list_item_2
用作ListView
中的行项目。您需要创建自定义适配器,以便在ListView
中显示这两个文本。在getView()
方法中设置两个文本。另外,使用ViewHolder
模式在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;
}
}