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.

4

2 回答 2

2

首先,

您正在使用默认 ArrayAdapter类..

new ArrayAdapter<String>();

哪个使用String类进行数据绑定。如果你想ArrayAdapter用 a 制作一个ArrayListorList你必须通过扩展or制作一个自定义适配器ArrayAdapter<Custom_Class>BaseAdapter

该类ArrayAdapter可以处理任何 Java 对象作为输入。它将这个输入的数据映射到布局中的 TextView。

ArrayAdapter使用toString()数据输入对象的方法来确定应该显示的字符串。

查看如何使用 ArrayAdapter<myClass> 和本教程

更新:

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
   .
   .
   .
于 2013-01-04T09:42:17.143 回答
0

这是更正的解决方案,

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

   }

基本上,我犯了 1 个错误,一个是我没有正确初始化构造函数。

于 2013-01-04T10:23:40.567 回答