1

I am trying to make a custom style for android spinner, however there are several problems. I have noticed in many examples, they pass array to getCustomView method. I am wondering whether I can pass a list instead of an array.

Second problem is why they have declared variable and initialized in class variable scope? They have declared arrays like this.

String []data1={"Brainbitz:java","Brainbitz:Android","Brainbitz:Embedded Systems","Brainbitz:PHP"};

in class variable scope. But when I try to do for the list I get an error. why?

Third is,

If we can pass list to getCustomView how do I do that? Here is the link to the tutorial tutorial

I am considering this source code.

 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.get(position));

//            TextView sub=(TextView)row.findViewById(R.id.textView2);
//            sub.setText(data2[position]);
//
//            ImageView icon=(ImageView)row.findViewById(R.id.imageView1);
//            icon.setImageResource(images[position]);

            return row;
            }

In above code I don't know the syntax to pass position to list3 list type reference.

Please be kind enough to explain this.

Jim
  • 3,254
  • 1
  • 19
  • 26
newday
  • 3,842
  • 10
  • 54
  • 79

2 Answers2

2

First of All,

You are using default ArrayAdapter Class..

new ArrayAdapter<String>();

Which Uses String class for data bind. If you want to make an ArrayAdapter with a ArrayList or List you have to make a Custom Adapter by extending ArrayAdapter<Custom_Class> or BaseAdapter.

The ArrayAdapter class can handle any Java object as input. It maps the data of this input to a TextView in the layout.

ArrayAdapter uses the toString() method of the data input object to determine the String which should be displayed.

Look at How to use ArrayAdapter<myClass> and this Tutorial

Update:

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);
  // TODO Auto-generated constructor stub
  listString  = objects;
  }

  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)); // How to use listString
   .
   .
   .
Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151
  • I am using the tutorial's class which has extended as followes, `public class MyAdapter extends ArrayAdapter` – newday Jan 04 '13 at 09:49
  • Then make a **Constructor** of that **Custom Adapter** class with argument `List` and use that object in `getView()` of your custom adapter class.. – user370305 Jan 04 '13 at 09:51
  • 1
    Also before using any tutorial just check whether its followed standard android programming rules or not.. – user370305 Jan 04 '13 at 09:54
  • but I am learning, How can I know whether it is standard rule then :) – newday Jan 04 '13 at 09:59
  • 1
    As if you know basic java programming rule, You know how to use `Class`, `Constructor` and `Member Variables`. As Most of Tutorials are for just workaround behind the Programming rules.. – user370305 Jan 04 '13 at 10:01
  • another comment, what do you think of initializing arrays in class variable scope? Can't I do this for list. Sorry for asking this kind of questions – newday Jan 04 '13 at 10:12
  • I had to create a custom style for spinner becase default spinner doesnt behave as I want – newday Jan 04 '13 at 10:26
  • My last comment, If I have several spinners such as let's say 3 spinners do I have to create 3 Adaptor classes? I have to passe list to `label.setText(list3.get(position));` . isn't there more easy way to handel? – newday Jan 04 '13 at 10:48
  • 1
    That's why we use `Adapter Class`.. If your 3 spinners have the **Same UI** only display data will be changed then the follow my answer. You don't need to create a 3 different Adapters. Just pass 3 different **ArrayList** to Adapter class. – user370305 Jan 04 '13 at 10:51
0

Here is the corrected solution,

public class MyAdapter extends ArrayAdapter<String>
    {

        public MyAdapter(Context context, int textViewResourceId,List<String> objects) {
              super(context, textViewResourceId, objects);
              // TODO Auto-generated constructor stub
        }
            @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.get(position));

//            TextView sub=(TextView)row.findViewById(R.id.textView2);
//            sub.setText(data2[position]);
//
//            ImageView icon=(ImageView)row.findViewById(R.id.imageView1);
//            icon.setImageResource(images[position]);

            return row;
            }

   }

Basiclly, I had done 1 mistake, one is I hadnt initalised constructor correctly.

newday
  • 3,842
  • 10
  • 54
  • 79
  • One more thing your programing code is wrong. As you are not using List objects in your `MyAdapter` then whats the need of using it in Constructor... :-) – user370305 Jan 04 '13 at 10:25
  • If you are using static `List objects` Class level variables then no need to pass it to `MyAdapter` Constructor. – user370305 Jan 04 '13 at 10:29