Add custom checkbox to ArrayAdapter

2019-09-15 01:16发布

I am using SQLite database to save my records, on returning them I am using this code:

 mydb = new DBHelper(this);
 ArrayList array_list = mydb.getAllItems();
 ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
 mRecyclerview =(RecyclerView) findViewById(R.id.items_List);
 obj = (ListView)findViewById(R.id.listView1);
 obj.setAdapter(arrayAdapter);

But this is showing me a normal list of items and I want to display a list with CheckBox on every row. I tried using BaseAdapter and also tried RecyclerView but Its just not working. Is there a way to customize the simple_list_item_1?

2条回答
狗以群分
2楼-- · 2019-09-15 01:56

you must write your own adapter and in getView() method, inflate your own view and pass data to it:

MyAdapter.java

public class MyAdapter extends BaseAdapter {

private final ArrayList data;
private final TextView text;
private final CheckBox check;
private final LayoutInflater inflater;
private final Context context;

public MyAdapter(Context context, ArrayList data) {
    this.context = context;
    this.data = data;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return data.get(position).getId();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.my_item_layout, null, false);
    }

    text = (TextView) convertView.findViewById(R.id.text_view);
    text.setText(data.get(position));

    check = (CheckBox) convertView.findViewById(R.id.check_box);

    return convertView;

}

And my_item_layout will be something like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <CheckBox
        android:id="@+id/check_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

With this you just show check boxes....you can't save or change check boxes data

查看更多
Evening l夕情丶
3楼-- · 2019-09-15 02:14

Have you tried CheckedTextview,You can

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/checkedTextView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:gravity="center|left"
    android:textColor="@color/black_color"
    android:drawablePadding="5dp"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:textSize="12dp"

     />
查看更多
登录 后发表回答