android - 在 Activity 外使用 getAssets

标签 android

我尝试在我的 DatabaseHandler 类中解析一个文件,但 Eclipse 说:

The method getAssets() is undefined for the type DatabaseHandler

这是代码:

 public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 15;

    public DatabaseHandler(Context context) {
        super(context, "rettinfo", null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("Create: ", "Creating antidotlist");
        String CREATE_ANTIDOT_TABLE = "CREATE TABLE antidots (id INTEGER PRIMARY KEY antidot TEXT, dos TEXT)";
        Log.d("Create: ", CREATE_ANTIDOT_TABLE);
        db.execSQL(CREATE_ANTIDOT_TABLE);

        InputStream antidots = getAssets().open("antidot/antidots");
        InputStreamReader input = new InputStreamReader(antidots);
        BufferedReader buffreader = new BufferedReader(input,2*1024);
        String line;
        while ((line = buffreader.readLine()) != null) {
            String[] point_t = line.split(",");
        }
        antidots.close();
    }

}

蒂姆更新

这就是eclipse不出错的原因

int i = 0;
InputStream antidots;
    try {
        antidots = mCtx.getAssets().open("antidot/antidots");
        InputStreamReader input = new InputStreamReader(antidots);
        BufferedReader buffreader = new BufferedReader(input,2*1024);
        String line;
        while ((line = buffreader.readLine()) != null) {
            i++;
            ContentValues values = new ContentValues();
            String[] antidot = line.split("#");
            int id = Integer.parseInt(antidot[0]);
            values.put("id", id);
            values.put("antidot", antidot[1]);
            values.put("dos", antidot[2]);  
            db.insert("antidots", null, values);                     
        }
        antidots.close();           
    } catch (IOException e) {
        e.printStackTrace();
    }

最佳答案

存储对从构造函数获取的 Context 的引用,然后对该引用调用 getAssets()。

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 15;
    private Context mCtx; //<-- declare a Context reference
    public DatabaseHandler(Context context) {
        super(context, "rettinfo", null, DATABASE_VERSION);
        mCtx = context; //<-- fill it with the Context you are passed
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("Create: ", "Creating antidotlist");
        String CREATE_ANTIDOT_TABLE = "CREATE TABLE antidots (id INTEGER PRIMARY KEY antidot TEXT, dos TEXT)";
        Log.d("Create: ", CREATE_ANTIDOT_TABLE);
        db.execSQL(CREATE_ANTIDOT_TABLE);

        InputStream antidots = mCtx.getAssets().open("antidot/antidots"); //<-- call getAssets on your Context object.
        InputStreamReader input = new InputStreamReader(antidots);
        BufferedReader buffreader = new BufferedReader(input,2*1024);
        String line;
        while ((line = buffreader.readLine()) != null) {
            String[] point_t = line.split(",");
        }
        antidots.close();
    }

}

关于android - 在 Activity 外使用 getAssets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11199900/

相关文章:

android - 如何防止或阻止使用旧版本应用程序的用户使用应用程序?

java - Android 使用 arraylist 在 ImageView 上加载图像

android - 如果安装了应用,请使用Deeplink和Google Play链接打开应用,否则打开商店

android - 如何检测 ProgressDialog 的状态

android - AppWidgetProvider 中的 updateAppWidget 不会导致更新

android - java.lang.VerifyError : Verifier rejected class: Constructor returning without calling superclass constructor 错误

android - 查询 packageManager 以处理 Intent 时 GET_INTENT_FILTERS 和 MATCH_DEFAULT_ONLY 之间的区别

android - 如何避免开机前黑屏?

android - 安装应用程序 android 时出现 INSTALL_PARSE_FAILED_MANIFEST_MALFORMED 错误

android - Android 2.3 SDK可以交叉编译到早期版本吗?