Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using getAssets outside an activity

Tags:

android

I try to parse a file in my DatabaseHandler class but Eclipse say:

The method getAssets() is undefined for the type DatabaseHandler

This is the code:

 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();
    }

}

Update for Tim

This is how eclipse dont make any errors

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();
    }
like image 947
Laire Avatar asked Jun 26 '12 02:06

Laire


1 Answers

Store a reference to the Context that you get from the constructor then call getAssets() on that reference.

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();
    }

}
like image 158
FoamyGuy Avatar answered Oct 15 '22 00:10

FoamyGuy