Android: binding data from database to CheckBox in ListView?

I am trying to bind data from my SQLiteDatabase to a ListView . I am currently using SimpleCursorAdapter to populate my ListView . Unfortunately, this does not work with setting the CheckBox checked attribute.

This is how I do it now; instead of changing the check state of the CheckBox, the adapter fills the value with a text argument, therefore the value is displayed to the right of the CheckBox as text.

Java:

 setListAdapter( new SimpleCursorAdapter( this, R.layout.mylist, data, new String[] { Datenbank.DB_STATE, Datenbank.DB_NAME }, new int[] { R.id.list_checkbox, R.id.list_text } ) ); 

mylist.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <CheckBox android:text="" android:id="@+id/list_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" ></CheckBox> <TextView android:text="" android:id="@+id/list_text" android:layout_width="wrap_content" android:layout_height="wrap_content" ></TextView> </LinearLayout> 

Edit: The field in the database, of course, is of type boolean, and I also tried to assign an identifier to the field being checked to fill in the value.

+43
android checkbox listview cursor
01 Oct '09 at 18:46
source share
3 answers

I'm not sure how you will do this, other than creating a custom adapter that will override newView / bindView or getView, depending on what you override (ResourceCursorAdapter is good).

So here is an example. I did not check if it will compile because I am at work, but this should definitely point you in the right direction:

 public class MyActivity extends ListActivity { MyAdapter mListAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Cursor myCur = null; myCur = do_stuff_here_to_obtain_a_cursor_of_query_results(); mListAdapter = new MyAdapter(MyActivity.this, myCur); setListAdapter(mListAdapter); } private class MyAdapter extends ResourceCursorAdapter { public MyAdapter(Context context, Cursor cur) { super(context, R.layout.mylist, cur); } @Override public View newView(Context context, Cursor cur, ViewGroup parent) { LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); return li.inflate(R.layout.mylist, parent, false); } @Override public void bindView(View view, Context context, Cursor cur) { TextView tvListText = (TextView)view.findViewById(R.id.list_text); CheckBox cbListCheck = (CheckBox)view.findViewById(R.id.list_checkbox); tvListText.setText(cur.getString(cur.getColumnIndex(Datenbank.DB_NAME))); cbListCheck.setChecked((cur.getInt(cur.getColumnIndex(Datenbank.DB_STATE))==0? false:true)))); } } } 
+33
01 Oct '09 at 20:55
source share

You can set custom SimpleCursorAdapter.ViewBinder :

 SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(/* ur stuff */); cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { public boolean setViewValue(View view, Cursor cursor, int columnIndex) { if(columnIndex == 1) { CheckBox cb = (CheckBox) view; cb.setChecked(cursor.getInt(1) > 0); return true; } return false; } }); 

The setViewValue method setViewValue called for each column specified in the SimpleCursorAdapter constructor, and gives you a good place to manipulate some (or all) of the views.

+38
Nov 28 '09 at 13:53
source share

You can solve this problem by creating your own CheckBox widget as follows:

 package com.example.CustomCheckBox; public class CustomCheckBox extends CheckBox { public CustomCheckBox(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public CustomCheckBox(Context context, AttributeSet attrs) { super(context, attrs); } public CustomCheckBox(Context context) { super(context); } protected void onTextChanged(CharSequence text, int start, int before, int after) { if (text.toString().compareTo("") != 0) { setChecked(text.toString().compareTo("1") == 0 ? true : false); setText(""); } } } 

The onTextChanged function will be called when the ListView associates data with the CheckBox (that is, it adds either "0" or "1"). This will force this change and add to your logical processing. The 1st if statement is necessary so as not to create infinite recursion.

Then specify your own class in the layout file as such:

 <com.example.CustomCheckBox android:id="@+id/rowCheckBox" android:layout_height="fill_parent" android:layout_width="wrap_content" /> 

That should do it!

0
Jan 26
source share



All Articles