user1252191
user1252191

Reputation: 49

how to remove selected item from listview using CursorAdapter

buttononclicklistener is not working,how to delete the selected item from list

 public void onClick(View arg0) {
    //TestDBAdapter.deleteEntry(itemId);
    TestDBAdapter.delete("TABLE_NAME", "_id="+itemId, null);                    
    Toast.makeText(DisplayActivity.this, "you want delete this item", Toast.LENGTH_SHORT).show();
    cursor.requery();
    updateList();   
    notifyDataSetChanged();
}

Upvotes: 0

Views: 2156

Answers (2)

Stellit
Stellit

Reputation: 21

Other possibility of refresh data in a CursorAdapter backed ListView is to create a new cursor and call swapCursor on CursorAdapter.

  cursor = newCursor();
  ((CursorAdapter) getListAdapter()).swapCursor(cursor);

Hope this can help.

Upvotes: 0

Aerrow
Aerrow

Reputation: 12134

Try 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();
        }
    });
}

also refer this link, How to remove a selected item from ListView using CursorAdapter

Upvotes: 1

Related Questions