Remove item from Listview

Asked

Viewed 1,138 times

1

Well I have a list, in which there are some items that the user himself creates. With this I am also wanting to give the user an option to remove the items not to get accumulated, so I searched and found a way using the onItemLongClickListener to remove the item. Follow the code:

  adapter.remove(adapter.getItem(position));
  adapter.notifyDataSetChanged();

Good so far so good when I gave the LongClick he removed the item, but after I went on another activity from the app and back to that activity of ListView the item I removed appeared again, what I do to fix it, to go to another activity back and the item remain removed ?

Note, I’m using Sqlitedatabase.

Thank you!

  • I imagine you are building your Adapter with an Arraylist/List, right? You are removing that item from that list as well?

  • 1

    I created my Arrayadapter, I didn’t quite understand your question but I’m only using these code up there to remove the item, otherwise I can remove the item, but it doesn’t save .. @siachester

  • um... so here’s the thing... did you check if the sqliter is removing the value? It may be persisting in insertion because of the value that did not exit the column.

  • because I can’t see the items the user created @Rafael

  • Yes. If you simulate a user. Performing all steps and reviewing your code on onItemLongClickListener, check if there is any line of code that removes, too, in sqliter.

  • has that option no...

  • The problem is that, in the life cycle of an Activity and depending on your code, your Adapter is redone after you return to the screen. If you do not persist deletion of the item in your internal database, it will not help to just delete from your Adapter

Show 2 more comments

1 answer

2

You performed the first step, which was to delete the Listview item. However, this item will persist as it was not deleted from the Database. When the Activity is recreated, the list is reorganized, because the query of that "deleted" item finds the item in the Database.

I recommend you review your code and insert something like this, to carry out the last remaining step to solve your problem:

 //---delete um valor em particular ---
public boolean deleteValor(String nome) 
{
    return db.delete(DATABASE_TABLE, KEY_NAME + "=" + nome, null) > 0;
}
  • Yes, but what I put in KEY_NAME ?

  • Static final String KEY_NAME = "myColuna"; // IF YOU ARE OUTSIDE THE SCOPE ------------------ Or String KEY_NAME = "myColuna"; // IF YOU ARE INSIDE THE SCOPE "db" is your database

  • and how I will use it in longclick ?

  • 1 - You have not submitted your code in full. 2 - This is a Boolean function with return, so it is simple to understand. 3 - You need to study more on how to put this in your logic. I don’t see your logic at all.

  • I can’t post my code...

  • I’m sorry, buddy. It’s not that I don’t want to, it’s that I don’t know where to start, since you’re not doing your part. However, if one day you understand and solve, come back here and vote positive for me :)

  • is because I can’t see logically the part of your code embedded in my code..

  • I know one day I’ll make it so I already gave you a positive and it was worth it anyway!

  • I give you a hint! Study more about: boolean functions with Java return.

  • Beauty I will study yes!

  • :) SHOW!!!!!!!!

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.