我有一个自定义组件充当表(标题+正文)。在我的标题中,我正在尝试添加" filterViews"取决于填写在表的每列中的数据类型。 所以我有一个布尔类型的列,表示为不可点击的复选框。
我正在尝试在标题单元格中创建一个带有三个可选选项的微调器:
我正在努力让这段代码正确。这是我的BaseAdapter代码:
public class BooleanAdapter extends BaseAdapter
{
private List<Object> values = new LinkedList<Object>();
public BooleanAdapter(Context context)
{
TextView txt = new TextView(context);
txt.setText("Clear filter");
values.add(txt);
CheckBox cb1 = new CheckBox(context);
cb1.setClickable(false);
cb1.setChecked(true);
values.add(cb1);
CheckBox cb2 = new CheckBox(context);
cb2.setClickable(false);
cb2.setChecked(false);
values.add(cb2);
}
@Override
public int getCount()
{
return values.size();
}
@Override
public Object getItem(int position)
{
return values.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
return (View) values.get(position);
}
}
我在代码中创建微调器:
Spinner spinner = new Spinner(getActivity());
BooleanAdapter adapter = new BooleanAdapter(getActivity());
spinner.setAdapter(adapter);
我收到此错误:
java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
at android.widget.ListView.measureScrapChild(ListView.java:1181)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1264)
at android.widget.ListView.onMeasure(ListView.java:1173)
at android.view.View.measure(View.java:10853)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4353)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:613)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
at android.view.View.measure(View.java:10853)
我感谢任何帮助。我还是新手(三周),所以我可能会遗漏一些基本的东西。谢谢。
答案 0 :(得分:1)
必须将LayoutParameters设置为适配器中的每个项目。
public BooleanAdapter(Context context)
{
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT);
TextView txt = new TextView(context);
txt.setText("Clear filter");
txt.setLayoutParams(lp);
values.add(txt);
CheckBox cb1 = new CheckBox(context);
cb1.setClickable(false);
cb1.setChecked(true);
cb1.setLayoutParams(lp);
values.add(cb1);
CheckBox cb2 = new CheckBox(context);
cb2.setClickable(false);
cb2.setChecked(false);
cb2.setLayoutParams(lp);
values.add(cb2);
}