0

Hey. I have the following code:

final String text = (String) lt.getItemAtPosition(position); 
                    db.removeCategory(text);

What I want to do is to remove a item from a ListView. The problem I have is that I only can remove the item that is in the first position of the list.

Is like the getItemAtPosition(0);

Why's that? Can somebody help?

Thanks

AMadmanTriumphs
  • 4,888
  • 3
  • 28
  • 44
thathashd
  • 1,022
  • 4
  • 17
  • 49
  • Could you collaborate a little more on the problem? I guess you have a ListView which has elements from a database? Do you want to remove the item just from the list or from the database as well? And please provide more code. – pecka85 Apr 13 '11 at 13:23
  • Try this : remove final from final String text=.... – Kartik Domadiya Apr 13 '11 at 13:24
  • text =lt.getItemAtPosition(position).toString() I got it. Now it's Working. Thanks – thathashd Apr 13 '11 at 13:44
  • what is db.removeCategory()? is that your custom function? If yes, post that code as well. Elaborate your code and situation to help you more. – Priyank Apr 13 '11 at 16:13
  • the remove category is a method in a class DBAdapter that removes data from the database. – thathashd Apr 13 '11 at 16:48
  • Post your code where you set Position variable. It is likely being set incorrectly to first node every time – Priyank Apr 13 '11 at 18:40
  • {adb.setPositiveButton("Yes",new DialogInterface.OnClickListener() { public void onClick(DialogInterface di, int arg) { db.open(); text= lt.getItemAtPosition(position).toString(); db.removeCategory(text); aa.notifyDataSetChanged(); Toast.makeText( getApplicationContext(), "Dados Removidos com Sucesso!!! " + text, Toast.LENGTH_SHORT) .show(); Intent myIntent = new Intent(category.this, MenuPrincipal.class); startActivity(myIntent); }});} – thathashd Apr 13 '11 at 23:17

2 Answers2

1

Bind your array to ArrayAdapter and use its remove method to remove particular object. Refer this link to get some understanding of how it works.

Remove ListView items in Android

Community
  • 1
  • 1
Priyank
  • 10,503
  • 2
  • 27
  • 25
  • my problem is that it only removes the first position of the list. Whatever item i click, the only item that is removed is the first. – thathashd Apr 13 '11 at 16:01
  • If it only removes the first one, your `position`'variable is likely wrong. Please post how you get it.. – pecka85 Apr 13 '11 at 18:28
  • `public void onClick(DialogInterface di, int arg) { db.open(); text= lt.getItemAtPosition(position).toString(); db.removeCategory(text); aa.notifyDataSetChanged();` – thathashd Apr 14 '11 at 08:44
0

Why don't you add items into a collection or data datable and then bind it to ListView.

In ste removal phase you delete the item from the collection/data table instead direct one from the ListView.

Sebastjan
  • 142
  • 1
  • 5
  • 13