Android 如何动态刷新ListView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10354692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to refresh ListView dynamically?
提问by Ankit
I am making a ListView with TextView and 1 delete button for each row.
我正在制作一个带有 TextView 的 ListView 和每行 1 个删除按钮。
To populate the list I am using my custom adaptor (extends base adapter) and sqlite db to map into list.
为了填充列表,我使用我的自定义适配器(扩展基本适配器)和 sqlite db 来映射到列表中。
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.
我可以从 db 中删除记录,但在我旋转设备或从活动分配我的适配器的新实例之前,我的列表不会刷新。
I have tried following answerbut didn't work in my case. the difference between this answer and my case is I am using baseAdapter and he is using cursorAdapter.
我试过以下答案,但在我的情况下不起作用。这个答案和我的情况之间的区别是我使用的是 baseAdapter 而他使用的是 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
列表项.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 deleteto deleted
在这里,我从我的数据库中删除记录并将删除按钮的文本从删除更改为已删除
回答by Shankar Agarwal
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.
您可以随时调用上述方法刷新列表视图。在您的情况下,从数据库中删除记录后调用它。
回答by mobile app Beginner
I update listview by calling:
listview.invalidateViews();
我通过调用更新列表视图:
listview.invalidateViews();
回答by Ankit
notifyDataSetChanged();
is the only solution for me also now my deleteBookmark method looks like
对我来说也是唯一的解决方案,现在我的 deleteBookmark 方法看起来像
void deleteBookmark(int wantedChild, String bookmarkItem) {
openDatabase();
int check = datasource.deleteBookmark(bookmarkItem);
if(check==1)
Toast.makeText(context, R.string.msg_bookmark_delete, ReaderConstants.TOAST_SHOWTIME).show();
else
Toast.makeText(context, R.string.msg_bookmark_delete_failed, ReaderConstants.TOAST_SHOWTIME).show();
notifyDataSetChanged();
closeDatabase();
}
回答by maddy
inside adapter set the custome method for update dataset and notify changes
内部适配器设置更新数据集的自定义方法并通知更改
public void updateData(ArrayList<DataSet> d) {
this.data = d;
notifyDataSetChanged();
}
Then call method inside Activity, (for my case ,I am calling inside onResume Method)
然后在 Activity 中调用方法,(就我而言,我在 onResume 方法中调用)
@Override
public void onResume(){
super.onResume();
adapter.updateData(dataList);
}