2015-06-16 25 views
0

我當前的Android項目使用SQLite數據庫。根據有關該主題的文檔,數據庫操作可能會長時間運行,具體取決於數據庫的大小,因此建議使用AsyncTask,特別是對於大型數據集。我就此實現我的數據庫類中操作爲AsyncTasks:從SQLiteAssetHelper調用AsyncTask的問題

package com.hadleyresearch.apptest; 

import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.AsyncTask; 
import android.support.v4.content.LocalBroadcastManager; 

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper; 

public class TscDatabase extends SQLiteAssetHelper{ 

    private static final String DATABASE_NAME = "database.db"; 
    private static final int DATABASE_VERSION = 1; 

    final private Context db_context; 

    //Action Strings 
    public static final String DEVICE_LIST_AVAILABLE = 
      "com.hadleyresearch.apptest.DEVICE_LIST_AVAILABLE"; 
    public static final String FUNCTION_LIST_AVAILABLE = 
      "com.hadleyresearch.apptest.FUNCTION_LIST_AVAILABLE"; 
    public static final String DEVICE_PROFILE_AVAILABLE = 
      "com.hadleyresearch.apptest.DEVICE_PROFILE_AVAILABLE"; 

    public TscDatabase(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     db_context = context; 
     // An alternate constructor can be used here to specify a 
     // different database location, such as an SD card. This folder 
     // must be available and permissions must be included to write 
     // to it. 
     //super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION); 
    } 

    private class ListDevices extends AsyncTask<Void, Void, Cursor> { 
     SQLiteDatabase db; 

     @Override 
     protected Cursor doInBackground(Void... params) { 
      db = getReadableDatabase(); 
      String sqlTable = "Devices"; 
      String[] sqlColumns = {"_id", "deviceAddr", "deviceName", "deviceType"}; 
      Cursor c = db.query(sqlTable, sqlColumns, null, null, null, null, null); 
      c.moveToFirst(); 
      return c; 
     } 

     @Override 
     protected void onPostExecute(Cursor result) { 
      broadcastUpdate(DEVICE_LIST_AVAILABLE, result); 
      db.close(); 
     } 

    } 

    public class GetDevice extends AsyncTask<String, Void, Cursor> { 
     SQLiteDatabase db; 

     protected Cursor doInBackground(String... device) { 
      db = getReadableDatabase(); 
      String sqlTable = "Devices"; 
      //String[] sqlColumns = {"_id", "deviceAddr", "deviceName", "deviceType"}; 
      String[] sqlColumns = null; 
      String sqlWhere = "deviceProfile = ?"; 
      String[] whereArgs = new String[] {device.toString()}; 
      Cursor c = db.query(sqlTable, sqlColumns, sqlWhere, whereArgs, null, null, null); 
      c.moveToFirst(); 
      return c; 
     } 

     protected void onPostExecute(Cursor result) { 
      broadcastUpdate(DEVICE_PROFILE_AVAILABLE, result); 
      db.close(); 
     } 
    } 

    //TODO - Implement listFunctions() command 
    // listFunctions() - list out mappable TactSense Functions 
    private class ListFunctions extends AsyncTask<Void, Void, Cursor> { 
     SQLiteDatabase db; 

     @Override 
     protected Cursor doInBackground(Void... params) { 
      db = getReadableDatabase(); 
      String sqlTable = "Functions"; 
      String[] sqlColumns = {"_id", "FunctionID", "FunctionName"}; 
      Cursor c = db.query(sqlTable, sqlColumns, null, null, null, null, null); 
      c.moveToFirst(); 
      return c; 
     } 

     @Override 
     protected void onPostExecute(Cursor result) { 
      broadcastUpdate(FUNCTION_LIST_AVAILABLE, result); 
      db.close(); 
     } 

    } 

    private void broadcastUpdate(final String action) { 
     final Intent intent = new Intent(action); 
     LocalBroadcastManager.getInstance(db_context).sendBroadcast(intent); 
    } 

    private void broadcastUpdate(final String action, 
           final Cursor cursor) { 
     final Intent intent = new Intent(action); 
     intent.putExtras(cursor.getExtras()); 
     LocalBroadcastManager.getInstance(db_context).sendBroadcast(intent); 
    } 
} 

在那之後,我稱之爲的AsyncTask從服務中:

@Override 
 
    public IBinder onBind(Intent intent) { 
 
     initialize(); 
 
     db = new TscDatabase(this); 
 
     new db.GetDevice().execute(device); 
 
     return mBinder; 
 
    }

Android Studio中似乎是知道的GetDevice作爲db的一個方法,因爲它作爲一個自動完成的例子提供給我。但是,一旦自動完成,它將以紅色突出顯示,並向我提供錯誤「無法解析符號GetDevice」。我試圖清理這個項目,無濟於事。

有人請指點我正確的方向?謝謝!

回答

1

類結構並不完全清楚,但這應該工作:

@Override 
public IBinder onBind(Intent intent) { 
    initialize(); 
    db = new TscDatabase(this); 
    TscDatabase.GetDevice asyncDev = new TscDatabase.GetDevice(); 
    asyncDev.execute(device); 
    return mBinder; 
} 
+0

這固定的「無法解析符號」的錯誤,但是執行上面的代碼引發以下錯誤: 「com.hadleyresearch .apptest.TscDatabase'不是封閉類 –

+0

然後請在問題中添加TscDatabase的代碼。 –

+0

@Gaurav:下班回家;將TscDatabase的完整代碼添加到問題中。任何幫助你或他人可以渲染將不勝感激,謝謝。 –