1

I have some values in my database and populating the values in my custom Listview with a custom Cursor Adapter.So when the person clicks on the listView a new Activity is opened and when he selects the delete button the that value or View should disappear and the list should be refreshed.

I had referred to many answers on SO but all of them suggested to removing the value from the database(which is not what I want) and some were using ArrayList and suggested me to use it with custom Array Adapters.But I am using a Cursor adapter.

So my whole question is how do I remove a value or remove a view from the listview?

The Listview item onClick Listener opens a new Activity

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//  Constants.ROW_NUMBER=position;
        TextView getRecordID = (TextView) view.findViewById(R.id.recordID);
        Constants.ROW_NUMBER = Integer.parseInt(getRecordID.getText().toString());

        Intent editIntent = new Intent(this,NoteDisplay.class);
        editIntent.putExtra(Constants.ROW_NAME,Constants.ROW_NUMBER);
        startActivity(editIntent);

    }

This is the new Activity and on the press of a Menu button the record will marked as deleted but won't be deleted from the database.

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId())
        {

            case R.id.deletebutton:

                DataBaseHelper deletedb = new DataBaseHelper(this);
                int deletedata = deletedb.deleteAction(position,System.currentTimeMillis());
                Intent dintent = new Intent(this,MainActivity.class);
                dintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(dintent);

                return true;

            default:
                return super.onOptionsItemSelected(item);

        }
    }

This is my custom Cursor adapter class,

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



    @Override
        public void bindView(View view, Context context, Cursor cursor) {
    //int getDeleteFlag is initialized above
              getDeleteFlag = cursor.getInt(cursor.getColumnIndex(DELETE_FLAG));

        if(getDeleteFlag==0)
        {
            ll = (LinearLayout)view.findViewById(R.id.listViewLayout);
            setListViewHeight(ll,context);

            if (cursor != null) {
                getText = cursor.getString(cursor.getColumnIndex(NOTE_TITLE));
                existsRecordID = cursor.getString(cursor.getColumnIndex(RECORD_ID));
                datevalue = cursor.getLong(cursor.getColumnIndex(RECORD_DATE));
}
            Date newdate = new Date(datevalue);
            tv = (TextView) view.findViewById(R.id.content);
            recordID = (TextView) view.findViewById(R.id.recordID);
            dateET = (TextView) view.findViewById(R.id.date);
            tv.setText(getText.trim());
            recordID.setText(existsRecordID);
            dateET.setText(dateFormatter.format(newdate));
        }else{
            Toast.makeText(context, "FALSE VALUE", Toast.LENGTH_SHORT).show();
        }

        }

I am confused whether to remove the view or the value at this point.

I have a row named "deleteflag" in my DB which stores 1=> Deleted 0=>Not deleted.

So only the values which are having 1 will be marked as deleted.

EDIT 1:

The above code in my bindView() method does not work

oldcode
  • 1,669
  • 3
  • 22
  • 41

2 Answers2

0

You have to store that deleteFlag values in database with 0 and 1 values.

So when you try to delete row/view at that time you have to update that flag value from 0 to 1.

Also, after updating old data you have to perform query of delete that row from DB.

Then do not forget to call yourAdapterName.notifyDataSetChanged(); just after delete data from DB.

Feel free to comment if this not solve your problem

You can see this link will help you.

and you are writing wrong code inside adapter.

You cannot filter inside adapter by checking if condition bcz in your cursor you are holding that data also which has deleteFlag=1; so if condition will not work

You have to perform this steps from link i have shared.

You have to update a Cursor, not a CursorAdapter.

newCursor = db.rawQuery( "SELECT * FROM mytable where deleteflag ==0 ", null); 
myCursorAdapter.changeCursor(newCursor);

So finally what it will do is it will hold only those records which has deleteFlag ==0

So you it will solve your problem as well as will not show extra rows also which does not contain deleteFlag=1;

Hope this will solve your problem.

Please mark as accepted answer if this helps you.

Community
  • 1
  • 1
Anant Shah
  • 3,744
  • 1
  • 35
  • 48
0

as you said dont delete item from database just put deleteflag = 1 it is ok

but when you come back to your list or coming to your list you should do folowing

Cursor yCursor = db.rawQuery("SELECT  * FROM table where deleteflag!=1", null);
yAdapter.changeCursor(yCursor);

if you are using activity than you can do it in your onactivityresult or in onResume of activity

it will just refresh your adapter with latest cursor who is not deleted

Miral Bhalani
  • 274
  • 2
  • 9
  • Yeah all that I tried bro but inspite of using the delete flag the item still appears in the list View. – oldcode Oct 25 '16 at 06:16
  • as i looking to your code you are pulling all data from database and checking flag inside bindview. but i am saying not to take all data write where in query.can you please share your database query ? – Miral Bhalani Oct 25 '16 at 06:20