【发布时间】:2015-08-07 20:09:31
【问题描述】:
我正在做我的 BlogApp。它从 Internet 获取所有数据。我添加了三个 TextView,但从 JSON 获取图像时遇到问题。我尝试了不同的方法,但我仍然不明白如何做到这一点。这是我的代码的一部分:
private void handleBlogResponse() {
mProgressBar.setVisibility(View.INVISIBLE);
if (mBlogData==null){
updateDisplayForError();
}else {
try {
JSONArray jsonPosts = mBlogData.getJSONArray("posts");
ArrayList<HashMap<String, String>> blogPosts = new ArrayList<HashMap<String, String>>();
for (int i=0; i<jsonPosts.length(); i++){
JSONObject posts = jsonPosts.getJSONObject(i);
String title = posts.getString(KEY_TITLE); //"title"
title = Html.fromHtml(title).toString();
String author = posts.getString(KEY_AUTHOR); //"author"
author = Html.fromHtml(author).toString();
String time = posts.getString((KEY_TIME)); //"time"
time = Html.fromHtml(time).toString();
String icon = posts.getString(KEY_ICON); //"icon" (icons url)
icon = Html.fromHtml(icon).toString();
我停在这里,不知道下一步该做什么!也许我应该为我的图像添加一个适配器来转换它?
HashMap<String, String> blogPost = new HashMap<String, String>();
blogPost.put(KEY_TITLE, title);
blogPost.put(KEY_AUTHOR, author);
blogPost.put(KEY_TIME, time);
blogPosts.add(blogPost);
}
String[] keys = {KEY_TITLE, KEY_AUTHOR, KEY_TIME};
int[] ids = {R.id.textView1, R.id.textView2, R.id.textView3};
SimpleAdapter adapter = new SimpleAdapter(this, blogPosts, R.layout.row_layout,
keys, ids);
setListAdapter(adapter);
} catch (JSONException e) {
Log.e(TAG, "Exception caught" + e);
}
}
}
感谢观看!
【问题讨论】:
标签: android json android-listview android-arrayadapter listadapter