简单的适配器问题,微调器中的文本+图像。 Java,Android
问候一切。 我有一个小问题......那么,让我先说明我正在努力完成什么。 我有一个微调从一个存储阵列拉出字符串。 像这样,你不需要虽然阅读:简单的适配器问题,微调器中的文本+图像。 Java,Android
ArrayAdapter healthadapter = ArrayAdapter.createFromResource( this, R.array.health, android.R.layout.simple_spinner_item); mHealthSpin = (Spinner) findViewById(R.id.health_spin); healthadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mHealthSpin.setAdapter(healthadapter);
简单,几乎足够了。我想添加一个图像到微调。单选按钮是没有必要的。所以微调应弹出并有一个清单:
TEXT *IMAGE* TEXT2 *IMAGE* TEXT3 *IMAGE*
到目前为止,我有一个自定义SimpleAdapter。 这是问题!:文字出现,但不是图像。下面是代码:
public class stageadapter extends SimpleAdapter { private Context localContext; private ArrayList> localList; public stageadapter(Context context, ArrayList> list, int resource, String[] from, int[] to) { super(context, list, resource, from, to); localContext = context; localList = list; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (null == convertView) { LayoutInflater inflater = (LayoutInflater) localContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.stagerow, null); } TextView name = (TextView) convertView.findViewById(R.id.stage_name); name.setText((String)localList.get(position).get("Name")); ImageView icon = (ImageView) convertView.findViewById(R.id.stage_icon); icon.setImageResource(R.drawable.icon); return convertView; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { if (null == convertView) { LayoutInflater inflater = (LayoutInflater) localContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.stagerow, null); } TextView name = (TextView) convertView.findViewById(R.id.stage_name); name.setText((String)localList.get(position).get("Name")); ImageView icon = (ImageView) convertView.findViewById(R.id.stage_icon); icon.setImageResource(R.drawable.icon); return convertView; } }
我打算使用switch语句为每个名称设置不同的图像。然而我停在这里,直到我可以得到任何图像显示。
如何我打电话
ArrayList> list = new ArrayList>(); HashMap map = new HashMap(); map.put("Name", "One"); map.put("Icon", R.drawable.icon); list.add(map); map = new HashMap(); map.put("Name", "Two"); map.put("Icon", R.drawable.icon); list.add(map); mStageSpin = (Spinner) findViewById(R.id.stage_spin); stageadapter adapter = new stageadapter(getApplicationContext(), list, R.layout.stagerow, new String[] { "Name", "Icon"}, new int[] { R.id.stage_name, R.id.stage_icon }); mStageSpin.setAdapter(adapter);
对我来说,答案就在评论
删除以下行 - 它混淆你的适配器:
healthadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("Name", "One");
map.put("Icon", R.drawable.icon);
list.add(map);
map = new HashMap<String, Object>();
map.put("Name", "Two");
map.put("Icon", R.drawable.icon);
list.add(map);
Spinner spin = (Spinner) findViewById(R.id.spin);
myAdapter adapter = new myAdapter(getApplicationContext(), list,
R.layout.list_layout, new String[] { "Name", "Icon" },
new int[] { R.id.name, R.id.icon });
spin.setAdapter(adapter);
}
private class myAdapter extends SimpleAdapter {
public myAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_layout,
null);
}
HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);
((TextView) convertView.findViewById(R.id.name))
.setText((String) data.get("Name"));
((ImageView) convertView.findViewById(R.id.icon))
.setImageResource(R.drawable.icon);
return convertView;
}
}
问题是在getView中已经分配了对应于po的文本使用
((TextView) convertView.findViewById(R.id.name)).setText((String) data.get("Name"));
但对于图像u必须使用相同的资源,即
((ImageView) convertView.findViewById(R.id.icon)).setImageResource(R.drawable.icon);
你需要使用 “数据” HashMap的名单,并指定在此图像
是sition @Vicky你是对的。 对于像它应该基于上述答案是
((ImageView) convertView.findViewById(R.id.icon)) .setBackgroundResource((Integer) data.get("Icon"));
,但有一些调整,以简化代码,并使其成为下拉正常工作。首先为你想要的行布局定义xml。在这种情况下,一个TextView和一排图像:
text_image_spinner_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView android:id="@+id/text_image_spinner_text_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView android:id="@+id/text_image_spinner_image_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/icon" />
</LinearLayout>
然后,根据需要创建,将建立各行类:
TextImageAdapter.java
public class ImageTextAdapter extends SimpleAdapter {
public final static String keyForTextInAdapter = "text";
public final static String keyForImageInAdapter = "image";
private final static String fromKeys[] = {keyForTextInAdapter, keyForImageInAdapter};
private final static int toIds[] = {MyR.id.text_image_spinner_text_tv, MyR.id.text_image_spinner_image_imageview};
private final static int TO_ID_TEXT_INDEX = 0;
private final static int TO_ID_IMAGE_INDEX = 1;
private final static int layoutId = MyR.layout.text_image_spinner_layout;
private final static int TYPE_SHOWN_SELECTION = 0;
private final static int TYPE_IN_DROPDOWN = 1;
LayoutInflater inflater;
private List<? extends Map<String, ?>> dataIn;
public ImageTextAdapter(Context context, List<? extends Map<String, ?>> data) {
super(context, data, layoutId, fromKeys, toIds);
inflater = LayoutInflater.from(context);
dataIn = data;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return makeView(position, convertView, parent,TYPE_IN_DROPDOWN);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return makeView(position, convertView, parent, TYPE_SHOWN_SELECTION);
}
private View makeView(int position, View convertView, ViewGroup parent, int type) {
// Use the int type if you want to determine different actions if you have
// differing requirements for the dropdown and the shown selection.
if (convertView == null) convertView = inflater.inflate(layoutId, null);
HashMap<String, Object> data = (HashMap<String, Object>)dataIn.get(position);
((TextView) convertView.findViewById(toIds[TO_ID_TEXT_INDEX])).setText((String) data.get(keyForTextInAdapter));
((ImageView) convertView.findViewById(toIds[TO_ID_IMAGE_INDEX])).setImageBitmap((Bitmap) data.get(keyForImageInAdapter));
return convertView;
}
}
现在,您只需在实例化时创建微调适配器。您提供的“东西”...:
private void setUpAppOptions() {
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map;
for (int i=0; i<things.length; i++) {
map = new HashMap<String, Object>();
map.put(ImageTextAdapter.keyForTextInAdapter, things[i].name);
map.put(ImageTextAdapter.keyForImageInAdapter, things[i].image);
list.add(map);
}
ImageTextAdapter adapter = new ImageTextAdapter(myContext, list);
appOptionSpinner.setAdapter(adapter);
}
我删除了setDropDownViewResource。没有图像。我修改了你的代码来和我一起工作。没有图像。我添加了Overide getDropDownView。没有图像:(我不知道什么是错的,因为文本工作 – Brian 2010-09-11 18:09:51
检查你的行布局的xml定义常见的错误:在一个水平的LinearLayout你应该使用wrap_content的TextView和ImageView(不fill_parent) – 2010-09-11 18:52:51
这是它!我的TextView被设置为fill_parent谢谢 – Brian 2010-09-11 19:14:10