Android : Using a checkbox in GridView and saving the checkbox's state

on Saturday, March 21, 2015


I have: A GridView that displays accounts from a database.


I want: To add another column at the end of each row that contains just a checkbox to denote the account status. There are only two statuses to choose from Active/Inactive so I thought a checkbox would suffice for that.


I need help with:



  1. Adding a column with just checkboxes to the grid.

  2. Saving the checkbox's state so that even after its dismissed when I call it again it will still show the appropriate state of the checkbox for each account in the grid.


Some Concerns:



  1. Since the checkbox is going to be directly associated with an account am I correct in assuming I have to declare a new value in my database. Something to denote the checkbox value so I can save it for future reference? Or is there another way?


Code Snippets ( if any other pieces of code are needed just ask )


How I post on my gridview



public void ShowGrid() {
dbHelper = new DatabaseHelper(this);
try {

Cursor c = dbHelper.getAllAccounts();
startManagingCursor(c);

String[] from = new String[]{DatabaseHelper.colName, DatabaseHelper.colAmount, DatabaseHelper.colTermsClass, DatabaseHelper.colStatClass};
int[] to = new int[]{R.id.colName, R.id.colAmount, R.id.colTerms, R.id.colStat};


SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.gridrow, c, from, to);
grid.setAdapter(sca);


} catch (Exception ex) {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setMessage(ex.toString());
b.show();
}
}


GridView



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tab1"

>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/status"
android:id="@+id/textView2"
android:layout_column="4" />

<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinStat"
android:layout_column="4" />


<TableRow android:showDividers="beginning|end">
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/name"
android:layout_weight="1"
/>
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/amount"
android:layout_weight="1"
/>
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/terms"
android:layout_weight="1"
/>
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/status"
android:layout_weight="1"
/>

</TableRow>
</TableLayout>
<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="336dp"
android:numColumns="1"
android:stretchMode="columnWidth"
/>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add_account"
android:id="@+id/addbtn"
/>

<TextView

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/showallacc"
android:id="@+id/allacc"
android:layout_alignParentRight="true"
/>


gridrow



<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow>
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="@+id/colName"
android:padding="5dp"
android:layout_weight="1"


/>
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="@+id/colAmount"
android:padding="5dp"
android:layout_weight="1"

/>

<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="@+id/colTerms"
android:padding="5dp"
android:layout_weight="1"

/>


<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="@+id/colStat"
android:padding="5dp"
android:layout_weight="1"

/>

</TableRow>

0 comments:

Post a Comment