0

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.

3
  • Are you using custom listview or simple listview? Commented Oct 25, 2012 at 5:38
  • 1
    @ankit you have to make custome adapter and you have to put delete buttnon in row file.and make the onclick item in adapter class.if u have tried any thing then post the code Commented Oct 25, 2012 at 5:42
  • @Google I am already using a custom adaptor. So please tell me is having a single delete button for all items is not right and should I change the design ? Commented Oct 25, 2012 at 5:53

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.