2014-03-29 36 views
0

我已经创建了自定义ArrayAdapter以按照我想要的方式显示微调框中的文本。然而,在运行该程序时,微调列表显示为空。自定义自旋适配器显示空列表

这里是我的代码:

public static class CustomAdapter extends ArrayAdapter<String> { 

    private int fontsize; 
    private int color; 
    private Typeface typeface; 
    private int bgcolor = Color.rgb(50, 50, 50); 
    private int selectedColor = Color.rgb(50, 50, 50); 
    private int selected = -1; 
    private List<String> data; 

    public CustomAdapter(Context context, int resource, Typeface tf, int colour, int fsize, List<String> labels) { 
     super(context, resource); 
     fontsize = fsize; 
     color = colour; 
     typeface = tf; 
     data = labels; 
    } 

    public void setBackgroundColor(int bg){ 
     bgcolor = bg; 
    } 

    public void setSelectedColor(int color){selectedColor = color;} 

    public void setSelectedPos(int p){ 
     selected = p; 
     notifyDataSetChanged(); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     TextView v = (TextView)super.getView(position, convertView, parent); 
     SpannableStringBuilder texter = new SpannableStringBuilder(); 
     Aux.addText(texter,data.get(position),color,fontsize,typeface); 
     if (v != null) { 
      v.setLayoutParams(new ViewGroup.LayoutParams(100, 100)); 
      v.setText(texter, TextView.BufferType.SPANNABLE); 
      //v.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
      if (position == selected) v.setBackgroundColor(selectedColor); 
      else v.setBackgroundColor(bgcolor); 
     } 
     return v; 
    } 


    public View getDropDownView(int position, View convertView, ViewGroup parent) { 
     TextView v = (TextView)super.getDropDownView(position, convertView, parent); 
     SpannableStringBuilder texter = new SpannableStringBuilder(); 
     Aux.addText(texter, data.get(position), color, fontsize, typeface); 
     if (v != null) { 
      v.setLayoutParams(new ViewGroup.LayoutParams(100, 100)); 
      v.setText(texter, TextView.BufferType.SPANNABLE); 
      if (position == selected) v.setBackgroundColor(selectedColor); 
      else v.setBackgroundColor(bgcolor); 
     } 
     return v; 
    } 

} 

我发现有人用完全相同的问题,但我无法理解,为了解决方案的代码来实现它自己(这里:Custom Spinner Adapter)从我understad我似乎有一个布局问题,但我不明白代码中变量make,v和row之间的关系。

另外我想这样做,而不使用对XML布局文件的引用,这可能吗?如果不是,你能提供一个必要的布局文件的例子吗?

+0

您不会将标签传递给超级玩家 – pskink

+0

非常感谢!你是对的。将标签传递给构造函数后,它就可以工作了。不过,我不得不删除setLayoutParams行。你应该将它作为答案发布,以便我可以检查它。 – aarelovich

回答

0

将“标签”参数传递给超级构造函数

1

基本上,在你的getView方法中,你错误地夸大了视图。您想通过创建一个LayoutInflater对象并使用其他示例所示的方式对其进行膨胀来夸大它。