|
1 package org.hedgewars.mobile; |
|
2 import java.util.ArrayList; |
|
3 import java.util.HashMap; |
|
4 import java.util.Map; |
|
5 |
|
6 import org.hedgewars.mobile.R; |
|
7 |
|
8 import android.content.Context; |
|
9 import android.graphics.Bitmap; |
|
10 import android.graphics.BitmapFactory; |
|
11 import android.view.LayoutInflater; |
|
12 import android.view.View; |
|
13 import android.view.ViewGroup; |
|
14 import android.widget.ImageView; |
|
15 import android.widget.SimpleAdapter; |
|
16 import android.widget.TextView; |
|
17 |
|
18 |
|
19 public class TextImageAdapter extends SimpleAdapter { |
|
20 |
|
21 private Context context; |
|
22 private ArrayList<Map<String, ?>> data; |
|
23 |
|
24 public TextImageAdapter(Context _context, ArrayList<Map<String, ?>> _data, int resource, String[] from, int[] to) { |
|
25 super(_context, _data, resource, from, to); |
|
26 context = _context; |
|
27 data = _data; |
|
28 } |
|
29 |
|
30 public static TextImageAdapter createAdapter(Context c, String[] txt, String[] img, String[] from, int[] to){ |
|
31 if(txt.length != img.length) throw new IllegalArgumentException("txt and img parameters not equal"); |
|
32 |
|
33 ArrayList<Map<String, ?>> data = new ArrayList<Map<String, ?>>(txt.length); |
|
34 |
|
35 for(int i = 0; i < txt.length; i++){ |
|
36 HashMap<String, Object> map = new HashMap<String, Object>(); |
|
37 map.put("txt", txt[i]); |
|
38 map.put("img", BitmapFactory.decodeFile(img[i])); |
|
39 data.add(map); |
|
40 } |
|
41 return new TextImageAdapter(c, data, R.layout.spinner_textimg_entry, from, to); |
|
42 } |
|
43 |
|
44 public View getView(int position, View convertView, ViewGroup parent){ |
|
45 if(convertView == null){ |
|
46 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
|
47 convertView = inflater.inflate(R.layout.spinner_textimg_entry, parent); |
|
48 } |
|
49 TextView tv = (TextView) convertView.findViewById(R.id.spinner_txt); |
|
50 ImageView img = (ImageView) convertView.findViewById(R.id.spinner_img); |
|
51 |
|
52 tv.setText((String)data.get(position).get("txt")); |
|
53 img.setImageBitmap((Bitmap)data.get(position).get("img")); |
|
54 |
|
55 return convertView; |
|
56 } |
|
57 } |