Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6780513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:30:54+00:00 2026-05-26T16:30:54+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:30:54+00:00Added an answer on May 26, 2026 at 4:30 pm

    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");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.