我在为微调器选择值时遇到问题。这是我的代码和适配器。
微调器事件监听器
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();
}
});
类别适配器
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;
}
}
对于我的微调器,在适配器中,我膨胀图像 + 文本,我的微调器正在工作,我在此说明我的微调器
==============================
|图标1 文字1 |
|图标 2 文本 2 |
|图标3 文字3 |
但是一旦我尝试使用 onitemselectedlistener 并使用 getSelectedItem().toString() 方法并烤它,烤面包上的值返回是图标和文本的组合,我不希望图标的值将被返回但是我只想要字符串或文本。我尝试使用 setTag(Object value) 方法使用 ((TextView) convertView.findViewById(R.id.category)).setTag((Object) data.get("Category")); 在适配器中,并且在侦听器中使用 getTag() 方法时会引发空指针异常。除了使用 getItemPosition(position) 或 getSelectedItem() 方法,还有其他解决方案来获取和操作所选项目的字符串值吗?请帮忙。