0

How would I access the sqlitedatabase object which is passed to startindexing method, inside my thread class?

public void startindexing (SQLiteDatabase db) {
    new Thread() {
        public void run() {

            ContentValues values = new ContentValues();
            for (int i = 0; i < filelist.length-1; i++) {
                String cat = "O", ty;   
                values.put(NAME, filelist[i].getName());
                if (!filelist[i].isDirectory()) {
                                        ty = filelist[i].getName().substring(filelist[i].getName().lastIndexOf("."));
                    if (ty.equalsIgnoreCase(".mp3") || ty.equalsIgnoreCase(".wav") || ty.equalsIgnoreCase(".ogg") || ty.equalsIgnoreCase(".acc"))
                        cat = "A";
                    else if (ty.equalsIgnoreCase(".mp4") || ty.equalsIgnoreCase(".wmv") || ty.equalsIgnoreCase(".3gp") || ty.equalsIgnoreCase(".mkv") || ty.equalsIgnoreCase(".divx"))
                        cat = "V";
                    else if (ty.equalsIgnoreCase(".jpg") || ty.equalsIgnoreCase(".gif") || ty.equalsIgnoreCase(".png") || ty.equalsIgnoreCase(".bmp") || ty.equalsIgnoreCase(".webp"))
                        cat = "P";
                    else if (ty.equalsIgnoreCase(".txt"))
                        cat = "T";
                    values.put(TYPE,ty);
                } else { values.put(TYPE, "dir"); cat = "dir"; }
                values.put(CATEGORY, cat);
                values.put(PATH, filelist[i].getAbsolutePath());
                SQLiteDatabase db = ;
                db.insert(TABLE, null, values);
        }
    }
}.start();
}
Dave Newton
  • 158,873
  • 26
  • 254
  • 302

1 Answers1

0

If you make the parameter final then you will be able to directly access it inside your thread's run() method. See How to pass parameters to anonymous class?

public void startindexing (final SQLiteDatabase db)

Remove this line:

SQLiteDatabase db=;
Community
  • 1
  • 1
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • i already tried that...wen i make the database object as final and run it, logcat says that my database object connection is not open...and i have already opened connection in another class from which i sent the object to this startidexing() method...plz help! – user1453438 Jun 13 '12 at 11:27
  • Make sure you aren't closing the DB connection somewhere else while your thread is running. – Graham Borland Jun 13 '12 at 12:48