How to get position of checkbox in listview
android, android-listview, checkbox
Solution
You can set tag to the checkbox, and when you get the callback for the event, you can extract the tag from view provided in callback. Set the position in the tag (setTag() is the method for setting tag). So your stuff should look something like following:
public View getView(int position, View convertView, ViewGroup parent)
{
//...
CheckBox cbx;
//...
cbx.setTag(position);
cbx.setOnCheckedChangeListener(checkListener);
//...
}
OnCheckedChangeListener checkListener = new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
int position = buttonView.getTag();
}
};
Problem
how to get id or position of a checkbox inside listview? I need to update database when isChecked = true or false, but I need position... maybe I could just do same things as in OnItemClick method? ``` @Override public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id){ CheckBox cbx = (CheckBox)itemClicked.findViewById(R.id.cbxList); cbx.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { } }); if(cbx.getVisibility() == View.VISIBLE){ ContentValues cv = new ContentValues(); if(cbx.isChecked()){ cbx.setChecked(false); int cbxID = 0; cv.put(DB.CBX, cbxID); mDB.updateCbx(cv, id); Log.d(LOG, " updated with id = " + id + " and cbxID is " + cbxID); }else{ cbx.setChecked(true); int cbxID = 1; cv.put(DB.CBX, cbxID); mDB.updateCbx(cv, id); Log.d(LOG, " updated with id = " + id + " and cbxID is " + cbxID); } }else{ Intent intentContactView = new Intent(this, ContactView.class); intentContactView.putExtra("id", id); startActivity(intentContactView); } } ``` The method getView() from class with SimpleCursorAdapter... same question, how to get the position or id of chosen checkbox? ``` @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = mLayoutInflater.inflate(R.layout.item, parent, false); view = super.getView(position, convertView, parent); CheckBox cbx = (CheckBox)convertView.findViewById(R.id.cbxList); } return view; } ``` and my Xml file with listview's items. Here said checkboxes are invisible, but they appears, when I call them (just for understanding. ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/margin" android:orientation="horizontal" > <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="@dimen/margin" android:layout_weight="0.5" android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/margin" android:orientation="vertical" > <ImageView android:id="@+id/ivPhoto" android:layout_width="@dimen/imageWidth" android:layout_height="@dimen/imageHeight" android:layout_gravity="center_vertical" android:contentDescription="@string/photoDescription" android:src="@drawable/default_contact" /> </LinearLayout> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="@dimen/margin" android:text="" android:textSize="@dimen/textSize" /> <TextView android:id="@+id/tvSurname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="@dimen/margin" android:text="" android:textSize="@dimen/textSize" /> </LinearLayout> <CheckBox android:id="@+id/cbxList" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="@dimen/margin" android:layout_weight="0.2" android:focusable="false" android:visibility="invisible" /> </LinearLayout> ```