0

I have a custom component acting as a table (header + body). In my header i am trying to add "filterViews" depending on data types filled in each column of table. So i have a column with boolean types, represented as non clickable checkboxes.

I am trying to create a spinner in header cell with three posible choices:

  • Clear filter (String)
  • Checked CheckBox
  • Unchecked CheckBox

I am struggling to get this code right. Here is my code for 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);
    }
}

I create spinner in code:

Spinner spinner = new Spinner(getActivity());
BooleanAdapter adapter = new BooleanAdapter(getActivity());
spinner.setAdapter(adapter);

I get this error:

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)

I appreciate any help. I'm still newbie in android (3 weeks) so i am probably missing something basic. Thank you.

vaske
  • 398
  • 1
  • 3
  • 13

1 Answers1

1

Had to set LayoutParameters to each of item in adapter.

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);
}
vaske
  • 398
  • 1
  • 3
  • 13