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
Related
I was learning custom adapter concepts recently.
Problem is during onlongClick in a row,
I want to show checkboxes checked in the longclicked row, which is in android:visibility="gone" initially. And also to show checkboxes in other rows which are not clicked in unchecked state.
I changed some parts of this code here.
http://windrealm.org/tutorials/android/listview-with-checkboxes.php
In simplerow.xml I made
android:visibility="gone" initially.
Now I made a onLongClickListener inside getView(...) method
textView.setOnLongClickListener (new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
CheckBox cb = (CheckBox) v;
Planet planet = (Planet) cb.getTag();
planet.setVisibility(View.VISIBLE);
planet.setChecked(true);
}
});
Now the above code will affect only the longClicked row. How do I make changes in the non-clicked rows?
Calling notifyDataSetChanged() on long click did not work because the other rows have checkbox initially in android:visibility="gone".
Please help. Is any other work around possible?
ListView accompanies Adapter. All your model items would be inflated into the view items through the implemented method named getView from Adapter. If you want to update view without changing data, call notifyDataSetChanged afterwards.
I have a scrollable listview that has 2 textViews, 3 imageButtons and 1 checkBox in each row.
In the Header of the listView, I have a delete button just above the column containing all the checkboxes of the listView.
I have to perform the delete operation of objects in the listView when 2 or more checkBoxes are checked randomly by scrolling the listView and thereafter the delete button at the top is clicked.
But the problem is that i am not getting the correct poition of the checkbox that was selected. Moreover, sometimes i get the correct position but still the object to be deleted passed is wrong. Hence the entire functionality is affected maybe due to the scrolling nature of the list.
Should i take the position in the holder of the adapter class and also bind the state of the checked or unchecked checkbox with my object.
And should I use checkBox.setOnCheckedChangeListener() or deleteButton.setOnClickListener().
If i use the latter one, then how to get all the corresponding objects of the list whose checkboxes were checked before pressing the delete button?
And where should all the related code be placed..in the listAdapter class or in the activity?
Please help me find a solution to this problem..
first of all in your getView() method you should set a specific Tag to each checkBox. For example: check1.setTag(position) then you should implement both OnCheckBoxChangeListener for your checkBoxes and OnClickListener for your delete button. As you know setting of onCheckBoxChangeListner have to be in getView() method. Then you add positions of list that their checkBox is checked to the a ArrayList with the help of getTag() method of chechBoxes in onCheckedChanged() method.
Try getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {}
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.
Im having a listview with items in a custom relativelayout which implements Checkable. It contains a checkbox which gets checked/uncheked via the checkable interface.
setFocusable is set to false for the Checkbox, so that I can use onItemClicked for the listview.
Now when an item is clicked the checkbox is also selected. I am getting crazy about this.
In the getView Method for my Adapter I assign an onCheckChangeListener to the checkbox.
This listener is called everytime a listitem is clicked and checks the Checkbox.
I saw questions how to select a checkable listitem onitemclick, and im getting this behaviour by default....
The Problem with this behaviour is:
The checkbox should get checked by clicking the checkbox not clicking the listitem. I start an Actionmode for the current visible Fragment when a checkbox is clicked, and i replace the current fragment when a listitem is clicked. BOTH is happening right now and that means, wrong Actionmode for wrong fragment and force close on backpress...
best regards vino
I think you have to make all other item in custom listview to setFocusable="false"
I have a ListView with checkboxes in it.
When I click on a element of the ListView, it does not check/uncheck the checkbox. I want to toggle the checkbox if the user clicks on the checkbox or on the listview.
How can I do that ?
Thank you
Listen for the click event and adjust the state of the CheckBox. The onClick gives you the View that was clicked so you should be able to get the CheckBox easily.
In order to get the click listener of CheckBox in each row of list view, you can add the setOnCheckedChangeListener of checkbox in getView() method of your custom adapter.