3 Replies - 323 Views - Last Post: 07 January 2013 - 05:32 PM

#1 kiriappa  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-November 12

selected item doesnt show on the spinner android

Posted 05 January 2013 - 06:47 AM

I have a spinner and I have added a custom style to spinner. Problem is when I select an item it doesnt show up on the spinner but when I use android spinner style it shows what I selected on the spinner. Is there any more coding to add to make it work? Otherwise everthing of the spinner works. I have written the app when an item is selected in spinner to show a text. These things work. But it doesnt show what I selected.

Here is my code
MyAdapter  dataAdapter3 = new MyAdapter(this, R.layout.spinner, list3);
spinner1.setAdapter(dataAdapter3);

List<String> list3 = new ArrayList<String>();


Here is the class for custom spinner style
public class MyAdapter extends ArrayAdapter<String>
    {
      private List<String> listString = new ArrayList<String>();

      public MyAdapter(Context context, int textViewResourceId, List<String> objects) {
        super(context, textViewResourceId, objects);

      this.listString  = objects;
      }

      @Override
      public View getDropDownView(int position, View convertView,ViewGroup parent) {
            return getCustomView(position, convertView, parent);
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
      }

      public View getCustomView(int position, View convertView, ViewGroup parent) {

       LayoutInflater inflater=getLayoutInflater();
       View row=inflater.inflate(R.layout.spinner, parent, false);
       TextView label=(TextView)row.findViewById(R.id.textView1);
       label.setText(listString.get(position)); 

       return row;

      }
    }


please can anybody tell whether I have done any mistake here?

Is This A Good Question/Topic? 0
  • +

Replies To: selected item doesnt show on the spinner android

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,422
  • Joined: 29-July 11

Re: selected item doesnt show on the spinner android

Posted 06 January 2013 - 08:45 AM

This worked just fine for me:

public class MainActivity extends Activity
{
    private String[] list3 = {"Mommy", "Daddy", "Mia"};
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        
        Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
        
        spinner1.setAdapter(new MyAdapter(this, R.layout.spinner, list3));
     }
    
    private class MyAdapter extends ArrayAdapter<String>
    {

      public MyAdapter(Context context, int textViewResourceId, String[] objects) {
        super(context, textViewResourceId, objects);
      }

      @Override
      public View getDropDownView(int position, View convertView,ViewGroup parent) {
            return getCustomView(position, convertView, parent);
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
      }

      public View getCustomView(int position, View convertView, ViewGroup parent) {

       LayoutInflater inflater=getLayoutInflater();
       View row=inflater.inflate(R.layout.spinner, parent, false);
       TextView label=(TextView)row.findViewById(R.id.textView1);
       label.setText(list3[position]); 

       return row;

      }
    }
}


Was This Post Helpful? 1
  • +
  • -

#3 kiriappa  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-November 12

Re: selected item doesnt show on the spinner android

Posted 06 January 2013 - 10:50 PM

thanks for the reply. But there is a problem in your code. IF we have several spinners then we have to create several classes for custom styles. In my code I set values to list in the constructuor of custom class. Therefore, I can passe any list anywhere. In your case only list3 array can be passed.

Second is we can use list. No need to be limited to arrays as many tutorials show. I have use list in my app. Therefore, I like to continue with list

Third and most important thing is my code is correct. problem was in spinner.xml file. I had set large padding to textview. Therefore, no way to be visible on the spinner.
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,422
  • Joined: 29-July 11

Re: selected item doesnt show on the spinner android

Posted 07 January 2013 - 05:32 PM

Use whatever list you like. It's irrelevant. Ah, never mind... I am glad you found YOUR problem. :)

This post has been edited by farrell2k: 07 January 2013 - 05:32 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1