0

I'm not sure why but each item in my database is duplicated about 30 times even though i only insert each item once. I thought perhaps it might be as a result of a walkdir() method that inserts into the database for each file with a .mp3 extension within a specified root folder or perhaps as a result of my naivety regarding ListView recycling.

public class SQLHelper extends SQLiteOpenHelper{
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "MusicDatabase.db";
    private static final String SQL_CREATE_TABLE =
            "CREATE TABLE "+
                    FeedEntry.TABLE_NAME+" ("+
                    FeedEntry._ID+" INTEGER PRIMARY KEY,"+
                    FeedEntry.KEY_FILENAME+" TEXT UNIQUE,"+
                    FeedEntry.KEY_SONGNAME+" TEXT )";
    private static final String SQL_DELETE_TABLE = "DROP TABLE IF EXISTS "+ SQLContract.FeedEntry.TABLE_NAME;

    public SQLHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_TABLE);
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DELETE_TABLE);
        onCreate(db);
    }
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }/*
    public List<Song> getAllSongs(){
        List<Song> songs = new LinkedList<Song>();
        String query = "SELECT * FROM "+FeedEntry.TABLE_NAME;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query,null);
        Song song = null;
        if(cursor.moveToFirst()){
            do {
                song = new Song();
                song.setId(Integer.parseInt(cursor.getString(0)));
                song.setFileName(cursor.getString(1));
                song.setSongName(cursor.getString(2));
                songs.add(song);
            }while(cursor.moveToNext());
        }
        return songs;
    }*/
    public void addSong(File file){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(FeedEntry.KEY_FILENAME, file.getPath());
        values.put(FeedEntry.KEY_SONGNAME, file.getName());
        db.insert(FeedEntry.TABLE_NAME,null,values);
    }
    public Cursor read(String sort, String direction){
        SQLiteDatabase db = this.getReadableDatabase();
        String[] projection = {
            FeedEntry._ID,
            FeedEntry.KEY_FILENAME,
            FeedEntry.KEY_SONGNAME,
        };
        String sortOrder = sort+direction;
        Cursor c = db.query(
                FeedEntry.TABLE_NAME,
                projection,null,null,null,null,
                sortOrder
        );
        return c;
    }
}
spintron
  • 107
  • 10
  • you´re assuming, somehow, we all know what you´re talking about or did I miss the last episode or something. – eduyayo Nov 01 '14 at 16:08
  • the code for insertion seems to be ok. what´s the content of the root dir and what´s repeated in the list? – eduyayo Nov 01 '14 at 16:09
  • And when is `walkdir` called? – CL. Nov 01 '14 at 20:46
  • @eduyayo i am not assuming anyhow. You don't need to be rude to get your point across. it really is a pretty straight-forward question the reason i did not show the rest of the code is because it has no relevance. root is a file and walkdir is called in the doInBackground method of my asynctask. – spintron Nov 02 '14 at 03:58

2 Answers2

0

I think the problem doesn't happen on your database code.

I feel, your problem is quite similar to How ListView's recycling mechanism works

Dealing with ListView correctly requires quite amount of work.

Community
  • 1
  • 1
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
0

After much trial and failure i discovered that the error in my code lies here:

public static final String DATABASE_NAME = "MusicDatabase.db";

Should be:

public static final String DATABASE_NAME = "MusicDatabase";

I have no idea why i thought the database extension was necessary.

spintron
  • 107
  • 10