How to refresh ListView dynamically?
Solution 1
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.
Solution 2
I update listview by calling:
listview.invalidateViews();
Ankit
An android developer by profession and photographer by passion.
Updated on July 09, 2022Comments
-
Ankit almost 2 years
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
-
Shankar Agarwal about 12 yearswhere is your arraylist that you are setting to adapter?
-
ootinii about 12 yearsYeah, my guess would be that it's not yet removed from the array the adapter is using when he calls notifyDataSetChanged(). Would need to see the code for deleteBookmark
-
Mark D about 12 yearsI doubt he is doing anything asynchronously inside deleteBookmark. Still, it would be interesting to know if list.postDelayed( /* New Runnable that does notify data set changed() */, 200);
-
Ankit about 12 years@agrawal i am not using arrayList i have my database from where i am fetching bookmarks.
-
Ankit about 12 yearsthanks guys i got problem for this. I was intialising my cursor in the constructor of my adapter because of that my list was not refreshing but now i am facing some other issue. I am posting that also in separate question.