0

我正在创建一个包含两个 TextView 和一个 ImageView 的 ListView。

我使用 Hashmap 将数据传递给 SimpleAdapter,因此我将图像的 id 转换为字符串,然后在 SimpleAdapter 中将它们转换回整数以设置图像资源。

但是,这似乎不起作用。

相关代码为:

我的活动的创建

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);

简单适配器类

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;
    }
}

我的 logcat 中的错误

E/AndroidRuntime(19406): java.lang.NumberFormatException: unable to parse 'res/drawable-hdpi/pic1.png' as integer
4

2 回答 2

2

您不能R.drawable.pic1作为 a传递String,然后将其解析为Integer. 这将不起作用,而是Integer直接在以下内容中传递初始可绘制 id 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);
    }

我希望我明白你想要什么。您可以创建一个类来保存要传递给适配器的数据,例如:

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.

于 2012-04-07T16:10:31.990 回答
0

here is an good example that may help you

enter image description here

于 2012-04-07T21:14:36.663 回答