Question

I have a list view with two text view and a delete button on the right corner (hidden), each list item is scrollable and delete button appears when I scroll to the left. I was able to get the position in the list onclick of delete button but I am unable to remove that list row.

Layout looks like this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:scrollbars="none"
        android:layout_height="match_parent"
        android:focusable="false" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:orientation="horizontal" >

            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/linearLayoutTeamInvitesListItem"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:focusable="false"
                android:onClick="onClickTeamInvitesListText"
                android:orientation="vertical" >

                <com.headsup.customviews.TextViewPlus
                    android:id="@+id/textViewListNameTeamInvites"
                    style="@style/text_view_list_item_style"
                    android:layout_marginTop="10dp"
                    android:focusable="false" >
                </com.headsup.customviews.TextViewPlus>

                <com.headsup.customviews.TextViewPlus
                    android:id="@+id/textViewListEmailTeamInvites"
                    style="@style/text_view_list_item_style"
                    android:layout_marginBottom="10dp"
                    android:focusable="false" >
                </com.headsup.customviews.TextViewPlus>
            </LinearLayout>

            <Button
                android:id="@+id/buttonListViewTeamInvite"
                android:layout_width="@dimen/teams_invites_listview_button_width"
                android:layout_height="match_parent"
                android:background="#FFFF0000"
                android:onClick="onClickTeamInvitesListButton"
                android:text="@string/delete" >
            </Button>
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout> 

Now I have to remove the list row when I click on delete button so I have "onClickTeamInvitesListButton" and function calls the activity class where I am redirecting it to the fragment, so that I could access the list view.

The function in activity looks like this.

public void onClickTeamInvitesListButton(View view) {
        fragmentTeamInvites.onClickTeamInvitesListButton(view);
    }

Function in fragment is like this.

 private static ListView listView;
 private static MyListAdapter mListAdapter;
...
...
public void onClickDeleteButton(View view) {
        int position = listView.getPositionForView(view);
        listView.removeViewAt(position);
        mListAdapter.notifyDataSetChanged();
    }

Found the answer .. needed to change my list code to this.

public void onClickDeleteButton(View view) {
        int position = listView.getPositionForView(view);
        listDataModel.remove(position);
        mListAdapter.notifyDataSetChanged();
    }
Was it helpful?

Solution

The problem is this:

public void onClickDeleteButton(View view) {
    int position = listView.getPositionForView(view);
    listView.removeViewAt(position);
    mListAdapter.notifyDataSetChanged();
}

You are removing the item from the ListView, not from the datasource (the adapter). So, when you call notifyDataSetChanged() the adapter attempts to refresh the data, but since nothing has changed in the data, it displays all rows again.

You should remove the item from your adapter, not from the ListView!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top