Android 在自定义适配器内单击“删除”按钮时刷新列表视图
我在这个网站上搜索过这个问题,但没有任何帮助。很多人问同样的问题,但没有人回答我的问题。我有一个列表视图,列表中的每一行都有一个删除按钮。我已经完成了所有的事情,除了更新我的列表视图。此列表视图由存储在数据库中的信息填充。对于每一行,我都有一个自定义的ListAdapter,我在这个类中处理我的delete按钮的onClick事件 这是我设置自定义适配器的主要活动:Android 在自定义适配器内单击“删除”按钮时刷新列表视图,android,android-listview,android-adapter,Android,Android Listview,Android Adapter,我在这个网站上搜索过这个问题,但没有任何帮助。很多人问同样的问题,但没有人回答我的问题。我有一个列表视图,列表中的每一行都有一个删除按钮。我已经完成了所有的事情,除了更新我的列表视图。此列表视图由存储在数据库中的信息填充。对于每一行,我都有一个自定义的ListAdapter,我在这个类中处理我的delete按钮的onClick事件 这是我设置自定义适配器的主要活动: public class ClientsActivity extends ListFragment { private Curs
public class ClientsActivity extends ListFragment {
private Cursor mCursor;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_clients, container, false);
listEmployees(view);
...
}
private void listEmployees(View view){
//Get the list from DB and set other variables..
...
ListView lvEmployees = (ListView) view.findViewById (android.R.id.list);
ListClientCursorAdapter notes = new ListClientCursorAdapter(context, R.layout.activity_row_client, mCursor, from, to, 0);
lvEmployees.setAdapter(notes);
}
}
这是我的自定义适配器类:
public class ListClientsCursorAdapter extends SimpleCursorAdapter{
//Set the constructor and stuff
...
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
//Things that populate my rows
...
ImageButton buttonDelete = (ImageButton) v.findViewById(R.id.imageButtonDelete);
//I set in a bundle, the id of the employee that I want to delete.
final Bundle mArguments = new Bundle();
mArguments.putString("id", id);
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());
alert.setMessage("are you sure?");
alert.setTitle("Deleting..");
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}});
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//I get from the bundle the Id of my employee
String id = mArguments.getString("id");
deleteEmployee(id);
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}});
return v;
}
private void deleteEmployee(String id) {
//Here I delete the item in the DB
//and everything works fine ..
...
//SOLUTION:
//HERE I HAVE TO REFRESH THE LIST. THIS IS HOW
this.changeCursor(DB.listCompanies(context));
this.notifyDataSetChanged();
}
}
据我所知,如果我从数组中获取列表的数据,notifyDataSetChanged()就可以工作。但这不是我的情况
任何帮助都将被感激
解决方案:
在deleteEmployee方法中,我放置了以下代码(如您在上面的代码中所看到的):
在哪里
DB.listEmployees(context)
是我用来从DB中检索值以填充listView的方法
因此,如您所见,我需要在数据更新后将旧游标更改为新游标。您重新查询mCursor,然后调用notifyDataSetChanged()您忘了告诉我调用changeCursor()方法。但你是对的,我需要重新查询并调用notifyDataSetChanged()。所以你的答案被接受了。我通常将mCursor传递给我的适配器,这样我就不需要调用changeCursor(),只需重新查询,然后通知datasetChanged()。
DB.listEmployees(context)