如何在我的线程类中访问传递给startindexing方法的sqlitedatabase对象?
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();
}
答案 0 :(得分:0)
如果您创建参数final
,那么您将能够在线程的run()
方法中直接访问它。见How to pass parameters to anonymous class?
public void startindexing (final SQLiteDatabase db)
删除此行:
SQLiteDatabase db=;