0

我已经在这个网站上搜索了这个问题,但没有任何帮助。很多人问同样的问题,但没有人回答我的问题。我有一个列表视图,列表的每一行都有一个按钮删除。我已经完成了所有事情,除了更新我的列表视图。此列表视图由存储在数据库中的信息填充。对于每一行,我都有一个自定义 ListAdapter,并且我在这个类中处理我的删除按钮的 onClick 事件。

所以这是我设置自定义适配器的主要活动:

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 中,我输入了以下代码(如您在上面的代码中所见):

this.changeCursor(DB.listEmployees(context));
this.notifyDataSetChanged();

在哪里

DB.listEmployees(context) 

是我用来从 DB 中检索值以填充 listView 的方法。

因此,如您所见,我需要将旧光标更改为更新数据的新光标。

4

1 回答 1

0

您重新查询 mCursor 然后调用 notifyDataSetChanged()

于 2013-05-14T03:12:33.000 回答