Zahary
Zahary

Reputation: 329

Android Spinner get selected value OnSelectedItemListener();

I'm having a problem to get value selected for spinner. here is my code and adapter.

Spinner event listener

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

for my Spinner, in the adapter, I inflate Image + Text, my spinner is working find, herewith I illustrate my spinner

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

|Icon1 Text1 |

|Icon2 Text2 |

|Icon3 Text3 |

but once i try to use onitemselectedlistener and use the getSelectedItem().toString() method and toast it, the value return on the toast is the combination of icon and the text, I don't want the value of icon will be return but I just want the string or text only. I've try using setTag(Object value) method using ((TextView) convertView.findViewById(R.id.category)).setTag((Object) data.get("Category")); in the adapter, and when use getTag() method in the listener nullpointer exception was thrown. Instead of using getItemPosition(position) or getSelectedItem() method is there any other solution to get and manipulate string value for selected Item? Please help.

Upvotes: 1

Views: 4782

Answers (1)

svpino
svpino

Reputation: 1994

One solution you can easily use is by using the Spinner#getSelectedItemPosition() method. This method will return the position of the selected item, and you can retrieve the item itself from the list you are passing to the adapter.

So basically, replace the following line in your onItemSelected method:

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

with:

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

Upvotes: 2

Related Questions