I have created a custom listview with checkbox, the items on the listview are not clickable. what am i doing wrong here, i want to be able to click the items on the list not just the checkbox.
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
data = store.getText().toString();
list.add(data);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_view, R.id.textView1, list);
ls.setAdapter(adapter);
}
});
ls.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String click = list.get(arg2).toString();
Toast.makeText(getBaseContext(), "You Clicked " + click, Toast.LENGTH_SHORT).show();
}
});
my code for list_view.xml
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_toRightOf="@+id/checkBox1"
android:text="" />
my activity_main.xml
file
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1" >
</ListView>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10" />
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/editText1"
android:text="Save" />
It doesnt work because adding CheckBox
to ListView
steals the focus from ListView
and you are not able click the list item . There is a Workaround , do not use checkbox but instead of it you can use for example Drawable , ImageView or TextView with parameter setClickable(true)
or XML android:clickable=”true”
. It'll do the thing because you will not lose the focus of the ListView
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.