Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

android - How to remove a selected item from ListView using CursorAdapter

I am using CursorAdapter and below is my adapter class. My list consists of two text views and one button on each row. Now, on click of the button I want to delete the selected item from the list as well as from the database. How can I get the id of the selected item from the database so that I can delete it and then notify the adapter (refresh the list).

public class MyAdapter extends CursorAdapter {

    Cursor c;
    LayoutInflater inflater;
    Context context;
    private String TAG = getClass().getSimpleName();

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

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

        TextView txtName = (TextView) view.findViewById(R.id.txt_name);
        txtName.setText(cursor.getString(cursor.getColumnIndex(Helper.tbl_col_username)));
        TextView txtPassword = (TextView) view.findViewById(R.id.txt_password);
        txtPassword.setText(cursor.getString(cursor.getColumnIndex(Helper.tbl_col_password)));

        Button button = (Button) view.findViewById(R.id.btn_delete);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Log.d(TAG, "Button Click ");
            }
        });
    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = inflater.inflate(R.layout.row, null); 
        return v;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try some thing like this :

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

    TextView txtName = (TextView) view.findViewById(R.id.txt_name);
    txtName.setText(cursor.getString(cursor.getColumnIndex(Helper
                                                           .tbl_col_username)));
    TextView txtPassword = (TextView) view.findViewById(R.id.txt_password);
    txtPassword.setText(cursor.getString(cursor.getColumnIndex(Helper
                                                           .tbl_col_password)));

    final String itemId = cursor.getString(cursor.getColumnIndex("id"));

    Button button = (Button) view.findViewById(R.id.btn_delete);
    button.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            Log.d(TAG, "Button Click ");
            deleteRecordWithId(itemId);
            cursor.requery();
            notifyDataSetChanged();
        }
    });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...