0

Now i am going on with the Listview in that i should delete the particular row which i selected here i select the row with the help of button in each row i have a button if i click the 2nd row button 2nd row should be deleted.

public class Adapter extends ArrayAdapter<Data> {

 private final View.OnClickListener deleteButton = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          selected = (Data)v.getTag();

}}


public class DetailsFragment extends Fragment implements Adapter
        .Listener {


  @Override
    public void Deleted(Data list) {

        int itemCount = adapter.getPosition(list);
        for(int i=0;i<adapter.getCount();i++) {
         Data present=     adapter.getItem(i);
            if(itemCount==i) {
                adapter.remove(present);

            }

        }
}

other approach:

@Override
    public void Deleted(Data list) {


        for(int i=0;i<adapter.getCount();i++) {
         Data present=     adapter.getItem(i);
            if(Adapter.selected ==present) {
                adapter.remove(present);

            }

        }
}

Tried with many link few below:

http://wptrafficanalyzer.in/blog/deleting-selected-items-from-listview-in-android/

http://www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/

My problem was when ever i try to delete the selected row it deletes from bottom of the list.

Here my position of the data ,id everything is assigned correct but it default removes from bottom

How can i solve this is there any other apporach to solve this problem.

Manoj
  • 3,947
  • 9
  • 46
  • 84
  • Check this.`http://stackoverflow.com/questions/27498226/how-to-perform-update-and-delete-operation-in-listview-item-row-while-click-the` I hope it will be helpful to you. – Stephen Jan 05 '15 at 14:34

2 Answers2

1

Basicly there are two steps that you need to follow;

  1. Delete the row data from the list.

    There supposed to be a list which contains the data that you show in your listview. First you need to delete the data from this list.

  2. Notify the adapter.

    Your adapter is connected with the data list. After you implement changes on your data list, you need to inform your adapter about these changes which means;

    adapter.notifydatasetchanged()

    after you call this method adapter will reload the data to listview without the deleted items.

Yiğit Doğuş Özçelik
  • 2,763
  • 2
  • 16
  • 19
0

See here according to one of the tutorials you posted here is how the data is being deleted. After deleting the date you have to notify the adapter just like the tutorial and you are not doing that. and also take a look at you for loop

/** Defining a click event listener for the button "Delete" */
    OnClickListener listenerDel = new OnClickListener() {
        @Override
        public void onClick(View v) {
            /** Getting the checked items from the listview */
            SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
            int itemCount = getListView().getCount();

            for(int i=itemCount-1; i >= 0; i--){
                if(checkedItemPositions.get(i)){
                    adapter.remove(list.get(i));
                }
            }
            checkedItemPositions.clear();
            adapter.notifyDataSetChanged();
        }
    };

Hope it helps.... :)

Umair
  • 6,366
  • 15
  • 42
  • 50
  • no bro actually i did this adapter.notifyDataSetChanged(); i didn't mentioned in code but my problem was when i delete it deletes from bottom not the particular position which is selected – Manoj Jan 05 '15 at 13:38
  • @Manoj ok did you take a look or tried to make changes in your for loop ? – Umair Jan 05 '15 at 13:41
  • above is my code please have a look at it if i wrong plz guide me bro – Manoj Jan 05 '15 at 13:43
  • @Manoj follow this tutorial it will surely help you as there are different scenarios given. http://www.vogella.com/tutorials/AndroidListView/article.html – Umair Jan 05 '15 at 13:46
  • This answer will help you to understand how to proceed using the listener on the adapter's constructor https://stackoverflow.com/a/50923045/1568663 – tryp Jun 19 '18 at 07:42