Android 自定义旋转适配器显示空列表
我已经创建了一个自定义的ArrayAdapter,以我想要的方式在微调器中显示文本。但是,在运行程序时,微调器列表显示为空 这是我的密码:Android 自定义旋转适配器显示空列表,android,android-adapter,Android,Android Adapter,我已经创建了一个自定义的ArrayAdapter,以我想要的方式在微调器中显示文本。但是,在运行程序时,微调器列表显示为空 这是我的密码: public static class CustomAdapter extends ArrayAdapter<String> { private int fontsize; private int color; private Typeface typeface; private int bgcolor = Co
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;
}
}
公共静态类CustomAdapter扩展了ArrayAdapter{
私用字号;
私人内特色;
私人字体;
private int bgcolor=Color.rgb(50,50,50);
private int selectedColor=Color.rgb(50,50,50);
选择的私有int=-1;
私人名单数据;
公共CustomAdapter(上下文上下文、int资源、字体tf、int颜色、int fsize、列表标签){
超级(上下文、资源);
fontsize=fsize;
颜色=颜色;
字体=tf;
数据=标签;
}
公共空间背景色(内部背景色){
bgcolor=bg;
}
public void setSelectedColor(int-color){selectedColor=color;}
公共无效设置选定位置(int p){
选择=p;
notifyDataSetChanged();
}
公共视图getView(int位置、视图转换视图、视图组父视图){
TextView v=(TextView)super.getView(position,convertView,parent);
SpannableStringBuilder texter=新的SpannableStringBuilder();
辅助添加文本(文本输入、数据获取(位置)、颜色、字体大小、字体);
如果(v!=null){
v、 setLayoutParams(新视图组.LayoutParams(100100));
v、 setText(texter,TextView.BufferType.Spanable);
//v、 setLayoutParams(新建ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容));
如果(位置==所选)v.setBackgroundColor(所选颜色);
else v.setBackgroundColor(bgcolor);
}
返回v;
}
公共视图getDropDownView(int位置、视图转换视图、视图组父视图){
TextView v=(TextView)super.getDropDownView(位置、转换视图、父级);
SpannableStringBuilder texter=新的SpannableStringBuilder();
辅助添加文本(文本输入、数据获取(位置)、颜色、字体大小、字体);
如果(v!=null){
v、 setLayoutParams(新视图组.LayoutParams(100100));
v、 setText(texter,TextView.BufferType.Spanable);
如果(位置==所选)v.setBackgroundColor(所选颜色);
else v.setBackgroundColor(bgcolor);
}
返回v;
}
}
我发现有人有完全相同的问题,但我无法理解解决方案中的代码以便自己实现它(此处:)根据我所了解的,我似乎有布局问题,但我不理解代码中make、v和row变量之间的关系
另外,我想在不使用xml布局文件引用的情况下执行此操作,这可能吗?如果没有,请提供所需布局文件的示例 基本上,在getView方法中,您以错误的方式膨胀视图。您希望通过创建一个LayoutFlater对象并使用该对象对其进行充气,如其他示例所示 将“labels”参数传递给超级构造函数您不会将标签传递给超级构造函数,非常感谢!!!你是对的。在我将标签传递给构造器之后,它工作了。不过,我不得不删除setLayoutParams行。你应该把它作为答案贴出来,这样我就可以检查它了。