Question

ListView is populating with the items from the Cursor wich I pass in the SimpleCursorAdapter, but each time I open the application these items are re-added to the listview increasing it continously. When I use SimpleAdapter, i do something like this:

static final ArrayList<HashMap<String, String>> foobar = new 
                                          ArrayList<HashMap<String,String>>();
   SimpleAdapter adapter = new SimpleAdapter(this, foobar, R.layout.list_item, String[] from, int[] to);
   setListAdapter(adapter);

Doing next, solve my problem:

    @Override
    public void onDestroy(){
    super.onDestroy();
       foobar.removeAll(foobar);
    }

But now, I don't want to delete the database content,so how to solve it if I have a SimpleCursorAdapter? like this one:

> SimpleCursorAdapter myadapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, String[] from, int[] to);

I have tried setListAdapter(null) or cursor.close(),and many others, but no efect...

Now, these hapen when I exit the application using "back" button of the device. If I press "home" button, when I came back I have the same number of items.So the list is duplicating every time I exit with "back" button.

Solved thanks to Kaediil's answer.The commented lines is what I have improved. The hole class:

public class DataBaseActivity extends ListActivity {

 DataBaseMethods dbmet; //is the class that handle database and auxiliar methods working on it

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    dbmet = new DataBaseMethods(this);
    Cursor mycursor= dbmet.getItems(); // 1. add new cursor


    try{
            if(!mycursor.moveToFirst()){   //2.check if there are items in the database
        dbmet.addItems("Daniel","Son","Karate-kid");
        dbmet.addItems("Silv", "Stalone", "Terminator");
        dbmet.addItems("foo", "bar", "buz");
            } //
            showDatabaseContent();
    }

    finally{
        dbmet.close();
    }
}


public void showDatabaseContent(){

     DataBaseMethods dbmet = new DataBaseMethods(this);
    try{

        Cursor cursor = dbmet.getItems();
        SimpleCursorAdapter myadapter = new SimpleCursorAdapter(this, R.layout.item_list, cursor, dbmet.FROM, TO);
        setListAdapter(myadapter);
    }
    finally{
        dbmet.close();
    }

}
Was it helpful?

Solution

Umm, this line is suspect:

If does it matter, I'm populating the database in this activity's onCreate(), but I have tried to populate it also from other activity and I get the same behaviour for the listview.

In the populating of the database call do you check to see if the database is already populated?

It sounds like you just keep adding more copies to the DB that the cursor is pointing to.

OTHER TIPS

public void showDatabaseContent(){

     DataBaseMethods dbmet = new DataBaseMethods(this);
    try{

        Cursor cursor = dbmet.getItems();
        SimpleCursorAdapter myadapter = new SimpleCursorAdapter(this, R.layout.item_list, cursor, dbmet.FROM, TO);
        setListAdapter(myadapter);
       -->> myadapter.notifyDataSetChanged(); <<----
    }
    finally{
        dbmet.close();
    }


}

You need to call notifyDataSetChanged() to signal the list to invalidate itself ONLY with the new data.

NOTE: If the issue is from the Database class. Make sure you are not reinserting items in the database every time you start the application. I might mixed your problem, wither the items are duplicate in the listview or in the database. I'm deeply sure that you are reinserting the data every time you run the app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top