如何使用SimpleCursorAdapter使用按钮删除listview中的行

时间:2014-07-08 05:31:34

标签: android listview simplecursoradapter

我在ListView行中添加了一个删除按钮,并试图找到一种方法使删除按钮在我的ListView中工作。我可以让我的代码从数据库中删除项目,我只需要帮助查找将click事件附加到行的删除按钮的位置和方法。

我尝试创建自定义游标适配器,但在使用我的应用程序时遇到了大量错误。非常感谢任何帮助!

这是我的代码:

ListRecipients.java

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListRecipients extends Activity
{
    ListView list;
    Cursor cursor;
    SimpleCursorAdapter adapter;
    //CursorAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);

        DbHelper dbhelper = new DbHelper(getApplicationContext());
        SQLiteDatabase db = dbhelper.getWritableDatabase();

        String[] from = new String[] { DbHelper.NAME, DbHelper.PHONE, DbHelper.MESSAGE };
        String[] column = new String[] {DbHelper.ID, DbHelper.NAME, DbHelper.PHONE, DbHelper.MESSAGE };
        int[] toViewIDs = new int[] { R.id.text_list_name, R.id.text_list_phoneNum, R.id.text_list_message };

        cursor = db.query(DbHelper.TABLE_NAME, column, null, null, null, null, null);
        adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, toViewIDs, 0);
        //adapter = new CursorAdapter(this, cursor);

        list = (ListView)findViewById(R.id.listview_list);
        list.setAdapter(adapter);
        cursor.moveToFirst();

        while (cursor.moveToNext())
        {
            String name = cursor.getString(cursor.getColumnIndex(DbHelper.NAME));
            String phone = cursor.getString(cursor.getColumnIndex(DbHelper.PHONE));
            String message = cursor.getString(cursor.getColumnIndex(DbHelper.MESSAGE));

            Toast.makeText(getApplicationContext(), "Name = " + name + "\nPhone = " + phone + "\nMessage = " + message, Toast.LENGTH_SHORT).show();
        }
    }
}

CustomCursorAdapter.java

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.Toast;

public class UserCustomAdapter extends CursorAdapter {
    Cursor cursor;
    LayoutInflater inflater;
    Context context;

    public UserCustomAdapter(Context context, Cursor cursor) {
        super(context, cursor);
        this.cursor = cursor;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view, Context context, final Cursor cursor) {

        final String itemId = cursor.getString(cursor.getColumnIndex("ID"));
        Button btnDeleteRecipient = (Button) view.findViewById(R.id.btn_list_delete);

        btnDeleteRecipient.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Log.i("ListRecipient", "Delete clicked");
                //deleteRecordWithId(itemId);
                cursor.requery();
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = inflater.inflate(R.layout.list_item, null); 
        return v;
    }
}

1 个答案:

答案 0 :(得分:0)

这个想法很旧,

尝试使用复选框实现Listview,并在任意位置放置一个删除按钮。

当用户选中多个复选框,然后获取所有选中的项目,然后仅删除那些行。

在这里你可以找到complete example