I've populated a ListActivity from a Cursor using SimpleCursorAdapter that starts another activity when one of the list items have been clicked. I'm also using ViewBinder to do some custom transformation of the data.
I want to add a CheckBox to each row in the list so I've changed the view and added a CheckBox with gravity right.
Adding the CheckBox has removed the ability to click on the items. The onListItemClick method I was overriding in ListActivity is no longer called when you press on a list item. Removing the CheckBox fixes this. Why is this?
Also, how can I set up the list so that it continues to perform my required functionality if the main part of the list item is clicked but have additional functionality when the CheckBox in the item is checked? Will setting a onCheckedChangedListener work or is the same view instance reused for each item in the list?
As explained here, the click listener only works if no other view is focusable. Setting your CheckBox to focusable="false" should do the trick:
<CheckBox android:focusable="false" />
Looks like SimpleCursorAdapter is too primitive for what I wanted to achieve.
I've switched to implementing CursorAdapter and returning a new view using the LayoutInflater in my implementation of the newView method.
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.alarm_row, parent, false);
}
In bindView I then set a custom OnClickListener to my main LinearLayout and then another OnCheckedChangeListener to the CheckBox.
For all this to look right I had to set the LinearLayout's background to android's menuitem drawable:
android:background="#android:drawable/menuitem_background"
Related
I got a listView with dynamically created items. Each item has a checkbox which can be checked/unchecked. When the checkbox is pressed this function is called:
public void updateClientList(View v) {
}
Now I want to change the specific item in my arraylist which is the base of the listview. But how do I know which checkbox was checked?
How can I do that? Thanks for your help!
If you only have a checkbox, you can use the OnItemClick listener that gives you position besides the view, but with this solution you have to update the checkbox manually so it doesnt consume the item click.
Another way is to use setTag(position) on the views you supply when binding.
You should consider moving on to RecyclerView.
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html
I have implemented an custom list view with base adapter that contains focusable item like button. so i have disabled the focusable and clickable in xml. My problem is , added context menu for list item in list view using register for context menu. how to make the list view respond for both long item click and also for button list item click?
Get reference to the button and then assign onclicklistener to it and also assign onlongitemclick listener to listview.hope this helps
The problem:
I have a ListView item with a relatively complex layout. One of the children in the list item view is a TextView with links. The TextView can handle the clicks on links found in it(by using Linkify), and has a OnClickListener that handles click on parts of the text that are not links. The listener will just get the position of the view in the ListView and perform a click:
#Override
public void onClick(View v) {
int position = mListView.getPositionForView(v);
mListView.performItemClick(v, position, mAdapter.getItemId(position));
}
Everything works fine, except the click doesn't trigger the ListView to update the list item's drawable state. It does trigger that when you click on other TextViews in the item that are not clickable.
Thanks!
Got it working.
Set the list item view clickable and set its background as a state-list drawable, also set addStatesFromChildren to true. This will make sure the state of the list view item is changed when any of its children is clicked(assume the child is clickable).
However, this will cause the ListView itself not receiving focus, so you need to handle the click by adding View.OnClickListener to the list item itself instead of adding a OnItemClickedListener to the ListView.
I have a custom cursor adapter I'm using to fill a ListView. The inflater has 2 CheckBoxes which both have onclick handlers. I want to be able to check the state of the sibling CheckBox when one is touched.
Does anyone have experience with something similar? I'm not sure how to grab the ListView row of the CheckBox and the cursor adapter is always in the state of the last row added.
You could probably call getParent() on the clicked view, and then findViewById on the parent to navigate back down to the other checkbox. Of course you might have to go up a couple levels if the checkboxes are wrapped in other views.
I'm trying to put a checkbox into ExpandableListView. How do I do that? I extend BaseExpandableListAdapter and put the following into getGroupView():
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
View view = convertView == null ?
context.getLayoutInflater().inflate(R.layout.onet_head, null) : convertView;
((TextView)view.findViewById(R.id.onetText)).setText(cats.get(groupPosition).value);
return view;
}
Notice that inflated layout? That's where I'm putting TextView and CheckBox.
I noticed that placing a checkbox into my group row layout disables default group row functionality when clicking on the row makes a secondary (child) list appear. CheckBox is functioning as expected but when I click outside of the it the
click is never detected by ether CheckBox or by OnGroupClickListener. I suspect that placing CheckBox into group row this way interferes with event detection/handling but thus far I'm not able to track it down
Can
someone help me to resolve this?
The CheckBox works fine though including detecting clicks when
clicking directly on the box
Anytime you place an item that is focusable in a list the list items no longer respond to clicks or anything like that. For every item you place in the list item that is focusable (buttons, checkboxes, etc), you need to set the android:focusable attribute to false.
I had a similar question and that was the answer for me.
Android custom ListView unable to click on items