我要创建一个包含两个TextViews和ImageView的一个ListView
我传递数据使用一个HashMap我SimpleAdapter,所以我图像的ID转换为字符串,然后在SimpleAdapter后,我将它们转换回整数设置图像资源。
不过这似乎并没有工作。
相关code是:
我的活动的OnCreate
clubImages =新的整数[] { R.drawable.pic1,R.drawable.pic2, R.drawable.pic3,R.drawable.pic4, R.drawable.pic5};LV的ListView =(ListView控件)findViewById(android.R.id.list);//创建网格项映射的String [] =由新的String [] {形象};INT []为= INT新[] {} R.id.club_image;// prepare的所有记录列表清单<&HashMap的LT;字符串,字符串>> fillMaps =新的ArrayList<&HashMap的LT;字符串,字符串>>();的for(int i = 0; I< eventTitles.length;我++){ HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>(); map.put(形象,的getString(clubImages [I])); fillMaps.add(地图);}//填写grid_item布局SimpleAdapter适配器=新MySimpleAdapter(这一点,fillMaps,R.layout.eventview_row,从,到);lv.setAdapter(适配器);
在simpleadapter类
公共类MySimpleAdapter扩展SimpleAdapter { 私人列表<&HashMap的LT;字符串,字符串>>结果; 公共MySimpleAdapter(上下文的背景下,列表与LT; HashMap的<字符串,字符串>>的数据,INT资源的String []从,INT []到){ 超(背景下,数据,资源,从,到); this.results =数据; } 公共查看getView(INT位置,查看视图的ViewGroup父){ 字体类型= Typeface.createFromAsset(getAssets(),字体/ aircruiser.ttf); 视图V =视图。 如果(V == NULL){ LayoutInflater VI =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); V = vi.inflate(R.layout.eventview_row,NULL); } mClubImageImageView =(ImageView的)v.findViewById(R.id.club_image); INT ClubImageHelper =的Integer.parseInt(results.get(位置)获得(图像)的toString()); mClubImageImageView.setImageResource(ClubImageHelper); 返回伏; }}
在我的logcat误差
E / AndroidRuntime(19406):java.lang.NumberFormatException:无法解析RES /绘,华电国际/ pic1.png'作为整数
解决方案
您无法通过 R.drawable.pic1
为字符串
,然后解析回为整数
。这是行不通的,而不是简单地直接通过最初的整数
绘制IDS在的HashMap
:
列表<&HashMap的LT;字符串,整数>> fillMaps =新的ArrayList<&HashMap的LT;字符串,整数>>(); 的for(int i = 0; I< eventTitles.length;我++){ HashMap的<字符串,整数>地图=新的HashMap<字符串,整数>(); map.put(形象,clubImages [I]); fillMaps.add(地图); }
我希望我知道你想要什么。你可以做一个类来保存您要在传递给适配器的数据,例如:
类数据对象{ 诠释imageId; 串firstString; 串secondString; //添加在这里,你希望把你的列表中的任何字符串 公开数据对象(字符串firstString,字符串secondString,诠释imageId){ this.imageId = imageId; this.firstString = firstString; this.secondString = secondString; }}
然后构造列表的适配器:
列表<&HashMap的LT;弦乐,数据对象>> fillMaps =新的ArrayList<&HashMap的LT;弦乐,数据对象>>(); 的for(int i = 0; I< eventTitles.length;我++){ HashMap的<弦乐,数据对象>地图=新的HashMap<弦乐,数据对象>(); map.put(形象,新的数据对象(第一个,第二个字符串,clubImages [I])); fillMaps.add(地图); }
在 getView()
方法检索数据对象
并使用其内容的数据,请你。
I'm creating a ListView that contains two TextViews and an ImageView.
I'm passing data to my SimpleAdapter using a Hashmap, so I convert the ids of the images to a string and then later in the SimpleAdapter I convert them back to integers to set the image resource.
However that doesn't seem to work.
The relevant code is:
The oncreate of my activity
clubImages = new Integer[] {
R.drawable.pic1, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5
};
ListView lv = (ListView)findViewById(android.R.id.list);
// create the grid item mapping
String[] from = new String[] {"image"};
int[] to = new int[] {R.id.club_image};
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < eventTitles.length; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("image", getString(clubImages[i]));
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new MySimpleAdapter(this, fillMaps, R.layout.eventview_row, from, to);
lv.setAdapter(adapter);
the simpleadapter class
public class MySimpleAdapter extends SimpleAdapter {
private List<HashMap<String, String>> results;
public MySimpleAdapter(Context context, List<HashMap<String, String>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
this.results = data;
}
public View getView(int position, View view, ViewGroup parent){
Typeface type = Typeface.createFromAsset(getAssets(), "fonts/aircruiser.ttf");
View v = view;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.eventview_row, null);
}
mClubImageImageView = (ImageView) v.findViewById(R.id.club_image);
int ClubImageHelper = Integer.parseInt(results.get(position).get("image").toString());
mClubImageImageView.setImageResource(ClubImageHelper);
return v;
}
}
The error in my logcat
E/AndroidRuntime(19406): java.lang.NumberFormatException: unable to parse 'res/drawable-hdpi/pic1.png' as integer
解决方案
You can't pass R.drawable.pic1
as a String
and then parse it back as an Integer
. This will not work, instead simply pass the initial Integer
drawable ids directly in the HashMap
:
List<HashMap<String, Integer>> fillMaps = new ArrayList<HashMap<String, Integer>>();
for(int i = 0; i < eventTitles.length; i++){
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("image", clubImages[i]);
fillMaps.add(map);
}
I hope I understand what you want. You could make a class to hold the data that you want to pass in to the adapter, for example:
class DataObject {
int imageId;
String firstString;
String secondString; // add here any string that you want to put in your list
public DataObject(String firstString, String secondString, int imageId) {
this.imageId = imageId;
this.firstString = firstString;
this.secondString = secondString;
}
}
Then construct the list for the adapter:
List<HashMap<String, DataObject>> fillMaps = new ArrayList<HashMap<String, DataObject>>();
for(int i = 0; i < eventTitles.length; i++){
HashMap<String, DataObject> map = new HashMap<String, DataObject>();
map.put("image", new DataObject("First String", "second String", clubImages[i]));
fillMaps.add(map);
}
In the getView()
method retrieve the DataObject
and use its content data as you please.