我在为微调器选择值时遇到问题。这是我的代码和适配器。
Spinner事件监听器
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;
}
}
对于我的Spinner,在适配器中,我充气Image + Text,我的微调器正在找工作,在此我说明了我的微调器
============================
但是一旦我尝试使用onitemselectedlistener并使用getSelectedItem()。toString()方法并烘烤它,吐司上的值返回就是图标和文本的组合,我不希望icon的值将是返回,但我只想要字符串或文本。我尝试使用setTag(Object value)方法使用((TextView)convertView.findViewById(R.id.category))。setTag((Object)data.get(“Category”));在适配器中,当在侦听器中使用getTag()方法时,引发了nullpointer异常。而不是使用getItemPosition(position)或getSelectedItem()方法是否有任何其他解决方案来获取和操作所选项的字符串值? 请帮忙。
答案 0 :(得分:2)
您可以轻松使用的一种解决方案是使用Spinner#getSelectedItemPosition()方法。此方法将返回所选项目的位置,您可以从传递给适配器的列表中检索项目本身。
所以基本上,在onItemSelected方法中替换以下行:
String selected = category.getSelectedItem().toString();
使用:
String selected = list.get(category.getSelectedItemItemPosition()).get("Category");