我在为微调器选择值时遇到问题。这是我的代码和适配器。

微调事件监听器

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;
    }
}


对于我的微调器,在适配器中,我给Image + Text充气,找到了微调器,并在此说明了微调器

===========================

| Icon1 Text1 |

| Icon2 Text2 |

| Icon3 Text3 |

但是一旦我尝试使用onitemselectedlistener并使用getSelectedItem()。toString()方法并进行烤面包,则烤面包上的返回值是图标和文本的组合,我不希望icon的值返回,但是我只想要字符串或文本。我尝试使用((TextView)convertView.findViewById(R.id.category))。setTag((Object)data.get(“ Category”));的setTag(Object value)方法。在适配器中,以及在侦听器中使用getTag()方法时,将引发nullpointer异常。除了使用getItemPosition(position)或getSelectedItem()方法,还有其他解决方案来获取和操作所选Item的字符串值吗?
请帮忙。

最佳答案

您可以轻松使用的一种解决方案是使用Spinner#getSelectedItemPosition()方法。此方法将返回所选项目的位置,您可以从传递给适配器的列表中检索项目本身。

因此,基本上,在您的onItemSelected方法中替换以下行:

String selected = category.getSelectedItem().toString();


与:

String selected = list.get(category.getSelectedItemItemPosition()).get("Category");

08-18 10:54