从SQLiteAssetHelper调用AsyncTask的问题

时间:2015-06-16 11:53:32

标签: android android-studio autocomplete android-asynctask

我当前的Android项目使用的是SQLite数据库。根据有关该主题的文档,数据库操作可以长时间运行,具体取决于数据库的大小,因此建议使用AsyncTask,特别是对于大型数据集。因此,我在我的数据库类中实现了AsyncTasks:

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:

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

Android Studio似乎意识到GetDevice作为db的一种方法存在,因为它将它作为自动完成示例提供给我。但是,一旦自动完成,它会以红色突出显示它并给出错误&#34;无法解析符号GetDevice&#34;。我试过清理项目,但没有用。

有人可以指出我正确的方向吗?谢谢!

1 个答案:

答案 0 :(得分:1)

你的班级结构并不完全清楚,但这应该有效:

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