Spininner中的SimpleAdapter,Text和Image [英] SimpleAdapter, Text and Image in spinner
问题描述
我有一点问题。好吧,让我先说明我想要完成的事情。
我有一个微调器,可以从存储的数组中拉出字符串。
喜欢这样,你不需要阅读它:
I've got a little problem. Well, let me first state what I'm trying to accomplish. I had a spinner that pulls strings out of a stored array. Like so, you don't need to read it though:
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);
简单且几乎足够。我想将图像添加到 Spinner。
RadioButton
不是必需的。所以 Spinner
应该弹出并有一个列表:
Simple and almost sufficient. I would like to add an image to the Spinner.
the RadioButton
is not necessary. So the Spinner
should pop up and have a list:
TEXT *IMAGE*
TEXT2 *IMAGE*
TEXT3 *IMAGE*
到目前为止,我有一个自定义 SimpleAdapter。
这是问题!! :
So far I've got a custom SimpleAdapter.
Here is the Problem!! :
文字出现但没有图片。
以下是代码:
public class stageadapter extends SimpleAdapter {
private Context localContext;
private ArrayList<HashMap<String, Object>> localList;
public stageadapter(Context context, ArrayList<HashMap<String, Object>> 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;
}
}
我打算使用切换
语句,为每个名称设置不同的图像。但是我停在这里直到我可以显示任何图像。
I plan to use a switch
statement to set different images to each name. however I stopped here until I can get any image to show.
我如何打电话:
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);
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);
我的答案在评论中。
推荐答案
删除以下行 - 它使您的适配器混淆:
Remove the following line -- its confusing your 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;
}
}
这篇关于Spininner中的SimpleAdapter,Text和Image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!