In my app I have ListView with checkboxes. I have delete button too. When I press delete button I want to delete all items from ListView which are checked. Is there any method which checks all items in ListView and deletes all items from ListView.? If there is not then how can i get my result..?
Yes its easy.
You have list items, so you know how many check boxes are there in listview. position is associated with each listview row. So set position as tag for the checkboxes in listview's getview method. i.e. checkbox.setTag(position).
Now fire the checkbox change event and store all checkbox state into boolean array. when user press delete button, just delete items whose value is true in boolean array.
Related
I want to pre checked/ pre selected a specific item when a list being created. I use CheckedTextView for the text and checkbox.
For example, I have 3 items, after the list create, I will get a listview. and the second item is already check/tick.
I know how to perform it by using onclick, but what I need is before I click it. some item is already checked.
Thank you
Add a boolean isChecked variable in your ItemModel, set true/false as you wish. Check/uncheck item in your CustomAdapter.
I have an Activity with a ListView and an adapter. Each row in my ListView contains one EditText and one CheckBox. I have two problems with this ListView:
first: what is the best way to save checked checkboxes after changing device orientation or pressing home button?
second: I implemented functionality to delete selected rows. When I click on checkbox and press a "delete button" the I call notifyDataSetChanged on my Adapter and everything is ok but if selected row isn't last row on my list then after all operations first field after deleted field becomes checked. Why?
The listview is recycling the views all the time. That means that the checkbox in the field after the deleted field is the SAME checkbox as was in the deleted field. That is why it becomes checked.
The solution is for the checked state to always be stored outside the view, in the activity. The easiest way is probably a Map of some key to a Boolean.
Try this..
Create Model fro list item and store checked and unchecked value to one variable and also you have to use getTag() and setTag() method.
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() {}
In my android app, I have a listview with an option to delete items from it
on the same screen. The delete button lies at the bottom of the list view
and there's a function written for deleting items and refreshing the list view.
This function is being called in onClick() of the List View.
This is where the problem lies.
In delete button's onClick I have first made a check for knowing which item to delete -
if (ListView.INVALID_POSITION != mListView.getSelectedItemPosition()) {
//delete the selected item
}else{
//do nothing
}
So whenever user touches delete button the focus from list view is removed and we get invalid as the list view position and hence the item is not deleted.
I also tried to store the value of selected item in a constant and update it in list view's onItemSelected() method and then remove the condition from Delete Button's onClick
.This works but causes another problem - If the user taps into empty area then focus from list view is removed and it appears that nothing is selected, but if u press delete button and then it deletes the last selected item as it is coming from a constant.
This is the problem I am facing.
Please suggest what to do.
Selection is useful only in keyboard mode, its turned off in touch mode so, getSelectedItemPosition() is not always reliable.
Read do's and don't in this developer blog entry.
If you want to use a single button, Set ListView choice mode to Single/Mutliple, then, on button click, get the checked items and delete them, refresh ListView after that.
Try to implement the onitemclickListener() and get the item id and delete the item clicked in the arrayadapter by implementing the onclick() for the button and next call adapter.notifyDataSetChanged();
I have a ListView and I want to select multiple items in the ListView without using CheckedTextView.
Please help !!
You don't really need a CheckBox. All you need is a boolean flag on your list objects. When the user clicks an item you change the state of the flag, and probably the background or text color for that list item.
I never tried it, but I would use ListView with checkbox but non visible.
Perform the setChecked(True) when the user press on an item.
I would also try changing the item background when the checkbox is checked.