Android Spinner 获取选定值 OnSelectedItemListener();

标签 android event-handling android-widget android-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,在适配器中,我膨胀图像 + 文本,我的 Spinner 正在工作,我在此说明我的 Spinner

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

|图标1 文本1 |

|图标2 文本2 |

|图标3 文本3 |

但是一旦我尝试使用 onitemselectedlistener 并使用 getSelectedItem().toString() 方法并烤它, toast 上返回的值是图标和文本的组合,我不希望图标的值会返回但我只想要字符串或文本。我尝试使用 setTag(Object value) 方法使用 ((TextView) convertView.findViewById(R.id.category)).setTag((Object) data.get("Category"));在适配器中,当在监听器中使用 getTag() 方法时抛出空指针异常。除了使用 getItemPosition(position) 或 getSelectedItem() 方法之外,还有其他解决方案来获取和操作所选项目的字符串值吗? 请帮忙。

最佳答案

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

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

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

与:

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

关于Android Spinner 获取选定值 OnSelectedItemListener();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7989109/

相关文章:

java - Android 如何追加 SQLite 数据库

android - 在 Android 3.1 中隐藏软键、通知区域和时钟

image - iTextSharp - 文本重叠图像

android - mdpi hdpi xhdpi 2x2 4x4 的小部件图标大小

android - 删除 Android PopupWindow 中的阴影

java - Android 线程 Activity 生命周期

java - 安卓/ Gradle : how to find the good mix of Google dependencies versions?

jsf-2 - 以编程方式添加 <f :event. ..

.net - .NET 中由于事件处理程序导致内存泄漏的示例?

android - 如何在单个 Activity 中显示第一个 fragment 的第二个 fragment