我遇到问题,需要为微调器选取值。这里是我的代码和适配器。Android微调器获取选定的值OnSelectedItemListener();
微调事件侦听器
category = (Spinner) findViewById(R.id.Spinner);
category.setPrompt("Select Category..");
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("Category", "Choose a Category");
list.add(map);
map = new HashMap<String, Object>();
map.put("Icon", R.drawable.icon1);
map.put("Category", "Category1");
list.add(map);
map = new HashMap<String, Object>();
map.put("Icon", R.drawable.icon2);
map.put("Category", "Category2");
list.add(map);
map = new HashMap<String,Object>();
map.put("Icon", R.drawable.icon3);
map.put("Category", "Category3");
list.add(map);
CategoryAdapter adapter = new CategoryAdapter(v.getContext(), list,R.layout.list_layout, new String[] { "Category", "Icon" }, new int[] { R.id.category, R.id.icon });
category.setAdapter(adapter);
category.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0,View v, int arg2,long arg3) {
// TODO Auto-generated method stub
String selected = category.getSelectedItem().toString();
//String selected = category.getTag().toString();
try{
//String selected = category.getSelectedItem().toString();
//Object selected = arg0.getTag();
//Toast.makeText(getApplicationContext(), String.valueOf(selected), Toast.LENGTH_SHORT).show();
if(selected.toString().equals("Category1")){
String msg1 = "Category1 type selected";
Toast.makeText(getApplicationContext(), msg1 + " " + selected, Toast.LENGTH_SHORT).show();
}else if(selected.toString().equals("Category2")){
String msg2 = "Category2 type selected";
Toast.makeText(getApplicationContext(), msg2 + " " + selected, Toast.LENGTH_SHORT).show();
}else if(selected.toString().equals("Category3")){
String msg3 = "Category3 type selected";
Toast.makeText(getApplicationContext(), msg3 + " " + selected, Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
Toast.makeText(getApplicationContext(),"Error occured " + e.getName().getClass(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(
AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "nothing selected", Toast.LENGTH_SHORT).show();
}
});
CategoryAdapter
public class ReminderCategoryAdapter extends SimpleAdapter{
Context c;
public ReminderCategoryAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
c = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater li = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.list_layout,null);
}
HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);
((TextView) convertView.findViewById(R.id.category)).setText((String) data.get("Category"));
((TextView) convertView.findViewById(R.id.category)).setTag((Object) data.get("Category"));
return convertView;
}
}
我的微调,在适配器,我吹图片+文字,我的微调工作发现,特此我说明我的微调
============================
| Icon1 Text1 |
| Icon2 Text2 |
| Icon3 Text3 |
但是,一旦我尝试使用onitemselectedlistener并使用getSelectedItem()。toString()方法并对其进行烘烤,则敬酒时的值返回值就是图标和文本的组合,我不希望图标的值将会返回,但我只想要字符串或文本。我尝试使用setTag(Object value)方法使用((TextView)convertView.findViewById(R.id.category))。setTag((Object)data.get(“Category”));在适配器中,并且在侦听器中使用getTag()方法时,抛出nullpointer异常。除了使用getItemPosition(position)或getSelectedItem()方法之外,还有其他解决方案来获取和操作所选项目的字符串值吗? 请帮忙。
只是为了解释这个有点多,'category.getSelectedItem()'返回一个Map实例。这就是为什么'toString()'不起作用。如果你做'category.getSelectedItem()。get(“Category”)'',你将在地图中获得该键的值。 – dmon
谢谢,我的问题解决了。 – Zahary