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();
Related
I have a listview and a corresponding listview adapter.
The views displayed in the Listview have I written myself and it's a framelayout with one button and one imagebutton. The imagebutton is a red cross that deletes the entry and the regular button selects the entry.
When the user presses the regular button, that item is selected and I want to show this to the user by setting the background of that item to green. The application stores which item the user selected and the next time the listview is rendered, THAT item should be selected and green.
Notice that there should be one and EXACTLY one selected item in my listview at all times.
In the getView(.. method in my adaper, it's very easy to change color of the button when the user clicks it. But the button that was green before the user clicked is impossible for me to reference.
I tried storing a reference the previously selected button, but it never repainted
I tried removing and adding the data item from the list to trigger notifyDataSetChanged but it never repainted
I tried setChoiceMode(ListView.CHOICE_MODE_SINGLE), but that led nowhere because I don't know how to catch the choice in my getView method and paint differently depending on wheter it's selected or not.
All guides I see suggest using notifyDataSetChanged. However, the underlying data is NOT changed in this case and it is not correct (or possible) solution.
So I think that my problem boils down to: HOW can I reference another view in my listview??
(And Yes, I have seen this post: Highlight selected item in ListView on Android . It describes my problem and is answered with "and then change the color of previous selected item's background back to normal" but I still can't refer to the PREVIOUS selected item.
Thanks!
Why don't you use Radio Buttons ? This is exactly the design you need. You can add a "red cross" on each line to delete the line. The selected line is unique and you can iterate on RadioButton.isChecked(); of your RadioGroup to set the background color accordingly.
ABOUT
I am using the SwipeListView library: https://github.com/47deg/android-swipelistview
PROBLEM
I've discovered that dismissing a row, and calling .recycle(cell, position); in the adapter causes the next row to be in a clicked state.
This causes the next row to become highlighted because the layout's background uses a selector.
When I remove the state_pressed="true" from the selector, this problem goes away but of course the row no longer highlights when the user clicks the row - which is not good.
QUESTION
How is it possible to reset a layout's clicked state ?
My plan is to reset the clicked state in the listview's adapter during getView()
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.
I have an app that has a ListView and when I click on some item, it adds the item in an array.
But when I click on some item I don't want change the view, then in Android 4.0 the ListViewgoing to first, but in Android 4.2.2 the ListView doesn't change. Why?
I always want the same result, the second option.
Is there any property for this?
Try making use of the getFirstVisiblePosition() and setSelection() methods of the listView. The first gets the first listview item present on the screen and the second moves the listview to the proper index when you comeback to this activity.
Please read the documentation for some more details about these functions.
Currently i am having listview with multiple items (First time, i fetch data with the use of AsyncTask), now i want to implement the below functionalities:
I am showing Option Dialog(With options Edit, Delete, Read) whenever user do long-click on any item, now if user select "Delete" option, at that time i want to remove item from the listview, listview should be displayed without that item meanwhile the data should also be fetched(in background) from the web.
Anybody knows, how do i implement such?
on the action of delete, delete the data from the source array, and call notifyDataSetChanged() on your adapter. and from the place where you call notifyDataSetChanged, you can do all the web stuff.
Use notifyDataSetChanged() over your Adapter
For display a dialog box when log press of listview item use onContextMenu() method in that onContextmenu u use the listview object and in that contaxt menu u write the code for display a dialog box with 3 options and next one remove item from listview when u click on the delete button in that dialog box is in delete button click listener remove the item from the listview and again set that remaining elements to that listview.