java - adding listener to custom listview containing CheckedTextView and button -
this row layout(rowlayout.xml)
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <checkedtextview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/checkedtextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="8dp" android:layout_margintop="8dp" android:drawableleft="?android:attr/listchoiceindicatormultiple" android:gravity="center_vertical" android:paddingright="16dp" android:text="checkedtextview" /> <button android:id="@+id/telque" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentend="true" android:layout_torightof="@id/checkedtextview" android:text="telque" /> </relativelayout> and main layout (activity_work_condition.xml)
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.doctorbeingwell.doctorbeingwell.createfilepack.workconditions"> <button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginbottom="8dp" android:layout_marginend="8dp" android:layout_marginleft="8dp" android:layout_marginright="8dp" android:layout_marginstart="8dp" android:layout_margintop="8dp" android:layout_weight="1" android:onclick="passconditions" android:text="button" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_torightof="parent" app:layout_constrainttop_tobottomof="@+id/linearlayout" /> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" tools:layout_editor_absolutex="8dp" tools:layout_editor_absolutey="8dp" android:id="@+id/linearlayout"> <listview android:id="@+id/checkabalelist" android:layout_width="match_parent" android:layout_height="491dp" app:layout_constraintleft_toleftof="parent" app:layout_constrainttop_totopof="parent" android:layout_weight="0.2"/> </linearlayout> </android.support.constraint.constraintlayout> and class (workconditions.java):
public class workconditions extends activity { arraylist<string> selecteditems = new arraylist<>(); listview checkablelist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_work_condition); checkablelist = findviewbyid(r.id.checkabalelist); checkablelist.setchoicemode(listview.choice_mode_multiple); string[] items = {"étudiant", "chomeur", "retraité", "travail à domicile", "travail au bureau"}; arrayadapter<string> adapter = new arrayadapter<string>(this, r.layout.rowlayout, r.id.checkedtextview, items); checkablelist.setadapter(adapter); checkedtextview checkedtextview= findviewbyid(r.id.checkedtextview); checkablelist.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { string selecteditem = ((checkedtextview) view).gettext().tostring(); if (selecteditems.contains(selecteditem)) { selecteditems.remove(selecteditem); } else selecteditems.add(selecteditem); } }); and result ,
but items not clickable , cant retreive data code
string selecteditem = ((checkedtextview) view).gettext().tostring();
in rowlayout.xml, add android:descendantfocusability="blocksdescendants" root node(relativelayout in case).
checkedtextviewview , button in rowlayout.xml both focusable, prevent listview item getting focus.
for more, refer android:descendantfocusability

Comments
Post a Comment