在自定义适配器中单击按钮删除时刷新列表视图

时间:2013-05-14 03:05:29

标签: android android-listview android-adapter

我在这个网站上搜索了这个问题,但没有任何帮助我。很多人问同样的事,但没有人回答我的问题。我有一个listView,我的列表的每一行都有一个按钮删除。我做了所有事情,除了更新我的列表视图。此列表视图使用存储在数据库中的信息填充。对于每一行,我都有一个自定义的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的方法。

因此,正如您所看到的,我需要更新旧游标以更新数据。

1 个答案:

答案 0 :(得分:0)

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

相关问题