通过 SimpleAdapter 在 imageview 中显示图像
Posted
技术标签:
【中文标题】通过 SimpleAdapter 在 imageview 中显示图像【英文标题】:Display image in imageview by SimpleAdapter 【发布时间】:2014-07-18 16:54:51 【问题描述】:我是新的 android 现在我想显示来自 url 的图像。我在列表视图中使用图像视图。我想将图像列表添加到列表项的每一行中。我使用了 SimpleAdapter,但 imageview 显示为空白。
这是主要活动:
package com.example.mysqltest;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ReadComments extends ListActivity
// Progress Dialog
private ProgressDialog pDialog;
// testing on Emulator:
private static final String READ_COMMENTS_URL = "http://192.168.30.198/test/webservice/comments.php";
// JSON IDS:
private static final String TAG_SUCCESS = "success";
private static final String TAG_TITLE = "title";
private static final String TAG_POSTS = "posts";
private static final String TAG_POST_ID = "post_id";
private static final String TAG_USERNAME = "username";
private static final String TAG_MESSAGE = "message";
private static final String TAG_IMAGE = "image";
// An array of all of our comments
private JSONArray mComments = null;
// manages all of our comments in a list.
private ArrayList<HashMap<String, String>> mCommentList;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
// note that use read_comments.xml instead of our single_post.xml
setContentView(R.layout.read_comments);
@Override
protected void onResume()
// TODO Auto-generated method stub
super.onResume();
// loading the comments via AsyncTask
new LoadComments().execute();
public void addComment(View v)
Intent i = new Intent(ReadComments.this, AddComment.class);
startActivity(i);
/**
* Retrieves recent post data from the server.
*/
public void updateJSONdata()
mCommentList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
try
mComments = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mComments.length(); i++)
JSONObject c = mComments.getJSONObject(i);
// gets the content of each tag
String title = c.getString(TAG_TITLE);
String content = c.getString(TAG_MESSAGE);
String username = c.getString(TAG_USERNAME);
String image = c.getString(TAG_IMAGE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_TITLE, title);
map.put(TAG_MESSAGE, content);
map.put(TAG_USERNAME, username);
map.put(TAG_IMAGE, image);
// adding HashList to ArrayList
mCommentList.add(map);
catch (JSONException e)
e.printStackTrace();
/**
* Inserts the parsed data into the listview.
*/
private void updateList()
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_post, new String[] TAG_TITLE, TAG_MESSAGE,
TAG_USERNAME,TAG_IMAGE , new int[] R.id.title, R.id.message,
R.id.username, R.id.imageView1 );
// I shouldn't have to comment on this one:
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
);
public class LoadComments extends AsyncTask<Void, Void, Boolean>
@Override
protected void onPreExecute()
super.onPreExecute();
pDialog = new ProgressDialog(ReadComments.this);
pDialog.setMessage("Loading Comments...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
@Override
protected Boolean doInBackground(Void... arg0)
updateJSONdata();
return null;
@Override
protected void onPostExecute(Boolean result)
super.onPostExecute(result);
pDialog.dismiss();
updateList();
【问题讨论】:
您在map.put(TAG_IMAGE, image);
中保存了什么?
单独使用SimpleAdapter
是不可能的。您需要创建一个自定义适配器扩展 SimpleAdapter
或任何其他适配器(BaseAdapter、ArrayAdapter 等)来实现这一点..
你有创建自定义 SimpleAdapter 的例子吗?
【参考方案1】:
在您的 MainActivity
上的setContentView(R.layout.read_comments);
之后添加此代码
if (android.os.Build.VERSION.SDK_INT > 9)
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
IDE 会自动要求您导入 android.os.StrictMode 库。
并考虑使用基本适配器
【讨论】:
以上是关于通过 SimpleAdapter 在 imageview 中显示图像的主要内容,如果未能解决你的问题,请参考以下文章
ListView / SimpleAdapter 不刷新 - Android
Android--Gridview使用SimpleAdapter加载bitmap图片
Android--Gridview使用SimpleAdapter加载bitmap图片