Reputation: 14950
I want to update my database on click of my checkbox
But I have no idea how I can capture the item no. of my item list by clicking the checkbox.
Here is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bucket_item);
int value = getIntent().getExtras().getInt("bucketno");
DatabaseHandler myDbHelper = new DatabaseHandler(this);
BucketItem[] bucketItems = myDbHelper.getAllContacts(value);
BucketItemAdapter adapter = new BucketItemAdapter(this, R.layout.row_item, bucketItems);
listView2 = getListView();
View header = (View) getLayoutInflater().inflate(R.layout.row_header, null);
listView2.addHeaderView(header);
listView2.setAdapter(adapter);
}
activity_bucket_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
</LinearLayout>
row_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/lightgray"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/completed"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/bucketNo"
android:layout_width="29dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" />
<TextView
android:id="@+id/bucketItemName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="10dp"
android:layout_weight="0.97" />
<TextView
android:id="@+id/bucketItemNo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone" />
</LinearLayout>
How can I click the checkbox, then call the update method of my database? any idea?
Note:
I have researched that I can override the
onListItemClick(ListView listView, View v, int position,
long id)
method to make this work, but I can't because I plan to use this method when the user click the item list, the description and image will pop-up.
Thank you
Upvotes: 0
Views: 671
Reputation: 5593
You can put some Id of row to your checkbox when creating view in adapter by setTag() method. Then extract it when you handle check state change. Something like this:
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = (Integer)buttonView.getTag();
}
Upvotes: 1