Android 在listview中设置图像
我是android新手,我在“ListAdapter”行遇到了一个问题。下面有人能帮我吗?我提到了我的代码,其中图像由于map.put图像值无法从字符串转换回int MainActivity.javaAndroid 在listview中设置图像,android,Android,我是android新手,我在“ListAdapter”行遇到了一个问题。下面有人能帮我吗?我提到了我的代码,其中图像由于map.put图像值无法从字符串转换回int MainActivity.java public class MainActivity extends Activity { private int id; private Time today = new Time(Time.getCurrentTimezone()); private ImageView aImage; pri
public class MainActivity extends Activity {
private int id;
private Time today = new Time(Time.getCurrentTimezone());
private ImageView aImage;
private Bitmap bMap;
private ListView listView;
private ArrayList<HashMap<String, String>> values = new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//aImage = (ImageView)findViewById(R.id.AImage);
listView = (ListView) findViewById(R.id.list);
java.lang.reflect.Field[] list = R.drawable.class.getFields();
//for(int i=0;i<list.length;i++){
for (int i=0;i<9;i++){
try {
id = list[i].getInt(null);
today.setToNow();
//System.out.println("------------------"+id+"---------------");
bMap = BitmapFactory.decodeResource(getResources(), id);
bMap = Bitmap.createScaledBitmap(bMap, 50, 50, true);
//aImage.setImageBitmap(bMap);
HashMap<String, String> map = new HashMap<String, String>();
map.put("img", String.valueOf(bMap));
map.put("time", today.format("%k:%M"));
values.add(map);
new CheckinDetail().execute();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
class CheckinDetail extends AsyncTask<String, String, String> {
@SuppressWarnings("deprecation")
protected String doInBackground(String... args) {
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
//int in = Integer.valueOf(et.getText().toString());
ListAdapter adapter = new SimpleAdapter(MainActivity.this, values, R.layout.rowlayout, new String[] { "img","time"},new int[] { R.id.AImage, R.id.time});
listView.setAdapter(adapter);
listView.setCacheColorHint(Color.TRANSPARENT);
}
});
}
}
}
公共类MainActivity扩展活动{
私有int-id;
今天的私人时间=新时间(Time.getCurrentTimezone());
私家影像浏览目标;
私有位图bMap;
私有列表视图列表视图;
私有ArrayList值=新ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//aImage=(ImageView)findViewById(R.id.aImage);
listView=(listView)findViewById(R.id.list);
java.lang.reflect.Field[]list=R.drawable.class.getFields();
//对于(int i=0;i使用List
)。
在这种情况下,可以将不同类型的对象作为值
在您的情况下,它将是:
// definition
private List<Map<String, Object>> values = new ArrayList<HashMap<String, Object>>();
// ...
// initialization
HashMap<String, String> map = new HashMap<String, String>();
map.put("img", bMap);
map.put("timeString", today.format("%k:%M"));
values.add(map);
//定义
私有列表值=新的ArrayList();
// ...
//初始化
HashMap=newHashMap();
地图放置(“img”,bMap);
map.put(“timeString”,today.format(“%k:%M”);
添加(映射);
然后,要使用SimpleAdapter
进行此操作,您需要给他一个自定义的SimpleAdapter.ViewBinder
,您应该从映射中提取位图并将其设置为相应的UI元素(ImageView)。使用HashMap map=new HashMap();在这种情况下,您可以将位图实例放入哈希集中。