I am making a ListView with TextView and 1 delete button for each row.
To populate the list I am using my custom adaptor (extends base adapter) and sqlite db to map into list.
My requirement is onclick of delete button in a row that record should be deleted and list should refresh.
I am able to delete record from db but my list is not refreshing until I rotate the device or assign a new instance of my adapter from activity.
I have tried following answer but didn't work in my case. the difference between this answer and my case is I am using baseAdapter and he is using cursorAdapter.
 public class BookmarksPDFAdapter extends BaseAdapter { 
            public View getView(int position, View convertView, ViewGroup parent) {
            openDatabase();
            btnDelete.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    deleteBookmark(getLocation(v));//getlocation(View) method returns which delete button clicked
                    notifyDataSetChanged();
                }
            });
        }
        closeDatabase();
        return convertView;
    }
my activity looks like
public class BookmarkActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bookmarks);
    btnEdit = (Button) findViewById(R.id.edit_bookmarks);
    btnAdd = (Button) findViewById(R.id.add_bookmarks);
    list = (ListView) findViewById(android.R.id.list);
    adapter = new BookmarksPDFAdapter(this);
    list.setAdapter(adapter);
}
bookmark.xml
<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10.0" 
android:paddingTop="5dp">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="3dip"
    android:layout_alignParentLeft="true"
    android:weightSum="1.0" 
    android:layout_marginRight="5dip">
    <ImageView
        android:id="@+id/iconShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/delete_icon"
        android:visibility="invisible" 
        android:layout_weight="1.0"/>
</LinearLayout>
<TextView
    android:id="@+id/bookmark_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="6dp"
    android:layout_weight="7.0"
    android:gravity="center_horizontal|center_horizontal"
    android:lines="1"
    android:text="@+id/TextView01"
    android:textSize="24dp" />
<Button
    android:id="@+id/btnDelete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_weight="2.0"
    android:text="@string/btn_txt_delete"
    android:visibility="invisible" >
</Button>
listitem.xml
<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10.0" 
android:paddingTop="5dp">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="3dip"
    android:layout_alignParentLeft="true"
    android:weightSum="1.0" 
    android:layout_marginRight="5dip">
    <ImageView
        android:id="@+id/iconShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/delete_icon"
        android:visibility="invisible" 
        android:layout_weight="1.0"/>
</LinearLayout>
<TextView
    android:id="@+id/bookmark_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="6dp"
    android:layout_weight="7.0"
    android:gravity="center_horizontal|center_horizontal"
    android:lines="1"
    android:text="@+id/TextView01"
    android:textSize="24dp" />
<Button
    android:id="@+id/btnDelete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_weight="2.0"
    android:text="@string/btn_txt_delete"
    android:visibility="invisible" >
</Button>
deleteBookmark method
void deleteBookmark(int wantedChild) {
    String bookmarkItem = getBookmarkItemText(wantedChild, true);
    datasource.open();
    int check = datasource.deleteBookmark(bookmarkItem);
    if (check == 1) {
        btnDelete = (Button) (viewList.get(wantedChild)
                .findViewById(R.id.btnDelete));
        btnDelete.setText(R.string.btn_txt_deleted);
        btnDelete.setEnabled(false);
    }
    datasource.close();
}
Here I am deleting record from my database and changing text of delete button from delete to deleted
adapter.notifyDataSetChanged();
You can call the above method to refresh list view any time. In your case call it after you delete a record from database.
I update listview by calling:
listview.invalidateViews();
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With