0

I can't get this working. I want the item from list view to be deleted when I click on the button. But I really don't know how to implement this.

Here is my adapter

    public class PersonalRecordsAdapterDialog extends CursorAdapter {

    public PersonalRecordsAdapterDialog(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.list_view_personal_layout, parent, false);
    }

    @Override
    public void bindView(View view, Context context, final Cursor cursor) {
        final DatabaseAdapter db = new DatabaseAdapter(context);
        TextView weightTV = (TextView) view.findViewById(R.id.weight_tv);
        TextView dateTV = (TextView) view.findViewById(R.id.date_tv);
        final Button deleteRecord = (Button) view.findViewById(R.id.delete_record);
        final String id = cursor.getString(cursor.getColumnIndex(DatabaseAdapter.DatabaseHelper.COL_1));
        String weight = cursor.getString(cursor.getColumnIndex(DatabaseAdapter.DatabaseHelper.COL_3));
        String date = cursor.getString(cursor.getColumnIndex(DatabaseAdapter.DatabaseHelper.COL_4));
        weightTV.setText(weight);
        dateTV.setText(date);

        deleteRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db.remove(Integer.parseInt(id));
                notifyDataSetChanged();
            }
        });
    }
}

and here is my ListView

   AlertDialog.Builder builder = new AlertDialog.Builder(holder.mContext);
            View dialogView = View.inflate(holder.mContext,R.layout.dialog, null);
            ListView myList = (ListView) dialogView.findViewById(R.id.dialog_list_view);

            Cursor cursor = holder.myDb.getRows(exercise[position]);
            PersonalRecordsAdapterDialog adapter = new PersonalRecordsAdapterDialog(holder.mContext,cursor);
            myList.setAdapter(adapter);
            builder.setView(dialogView)
                    .setTitle("History of your " + exercise[position].toLowerCase() + " records")
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            // sign in the user ...
                        }
                    });
            AlertDialog b = builder.create();
            b.show();

Thank you for your help

curiousMind
  • 2,812
  • 1
  • 17
  • 38
Robert Vangor
  • 1,018
  • 1
  • 13
  • 24

3 Answers3

1

Remove the item from your model when onClick happens n call notifyDatasetChanged() for the same adapter

and in your case you are passing cursor directly to your custom adapter which is a very bad practice as in this case your connection with the db will stay open and can lead to Memory and DB issues

So you can create your own model (ArrayList) , get values from cursor, add them into your model,Close your db connection and pass that model to your adapter. and to remove a particular item remove that from your model and call notifyDatasetChanged().(Note: Removing from model will only remove the data from list but not from db. In case you want to delete that data from your db, you also have to execute Delete Query )

for this : I don't want to delete it when I click on ListView item. I want to delete it only when I click on button in ListView item

Go to your adapter class.. get the object instance over there in OnGetView(...) method and the the onClickListener for the same over there.

To delete row from DB you can use a unique id from db table like this

public void delete_byID(int id){
 sqLiteDatabase.delete(MYDATABASE_TABLE, KEY_ID+"="+id, null);
}

Where MYDATABASE_TABLE is your table to delete from.

KEY_ID is the name of the coloumn to put where condition.
id  is the unique id associated with the row you want to delete 

So in this case you will not need the cursor to delete a particular record

Nitesh
  • 3,868
  • 1
  • 20
  • 26
0

The reason that bindView method doesn't refresh by the db change is : The cursor is steel the old cursor . You need to requery the data base get new cursor and then Call adapter.change cursor(passHereTheNewCursor)

Rami Loiferman
  • 853
  • 1
  • 6
  • 22
0

Adding to above answer by Nitesh - To delete item from listview on button click, Either 1. first you need to delete item from listview's adapter like below -

deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              Object objToRemove = adapter.getItem([POSITION]);
              adapter.remove(objToRemove);
}
}

or 2. you can delete this item from your arrayList & call notifyDataSetChanged().

But this will not delete from database. you have to implement delete query for it.

and yes as above said - in your case you are passing cursor directly to your custom adapter which is a very bad practice as in this case your connection with the db will stay open and can lead to Memory and DB issues.

Vikram
  • 768
  • 2
  • 9
  • 23