Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialryanjones6
13,797 PointsRead in file to SQLite database only once: not working!
I currently created a simple dictionary application that reads in a text document stored in the [raw] directory under the [res] directory. It is loaded in the onCreate method. The issue I need help with is.... how do I make it so it loads the data only once? -- It currently loads the data every time the activity is created, creating duplicated entries --
I've already got a class called DBhandler that extends the SQLiteOpenHelper My main class is called MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DBHandler db = new DBHandler(this);
readInFileToDatabase(db, R.raw.spanish,"^");
}//End onCreate
/**
* Uses a class called DBHandler which extends SQLiteOpenHelper
* To read a file from the raw directory. Replace rawFile with R.raw.your_file_name. Does not need file extension
* The default value for splitChar is a comma ,
* The default value for splitNumber is 2
* @param db DBHandler
* @param rawFile R.raw.file_name
* @param splitChar character that splits string into array
* */
public void readInFileToDatabase(DBHandler db, int rawFile, String splitChar){
Scanner inFile;
String foreignLang;
String nativeLang;
String cat;
try{
inFile = new Scanner(getResources().openRawResource(rawFile));
//temp fix delete all// no duplicates
//continue while file has nextLine
while(inFile.hasNextLine()){
//read line
String currentLine = inFile.nextLine();
//split current line: look for splitChar String character
//double escape required: just accept it
String[] value = currentLine.split("\\" + splitChar);
//assign values from value array to name and address
foreignLang = value[0];
nativeLang = value[1];
cat = value[2];
//add external file row to new database row
db.addWord(new Word(foreignLang, nativeLang, cat));
}
inFile.close();
} catch (Exception e){
Log.v("Message: ",e.getMessage());
Log.wtf("File not found: ", String.valueOf(rawFile));
}
} //END readInFileToDatabase
Review the Project on Github https://github.com/drjonesy/LangDictionary
1 Answer
Ben Deitch
Treehouse TeacherTry creating it in the Application.onCreate() method.
class App extends Application {
@Override
public void onCreate() {
super.onCreate();
DBHandler db = new DBHandler(this.getBaseContext());
readInFileToDatabase(db, R.raw.spanish,"^");
}
}
Then in your Android manifest, add your new 'Application' class:
<application
android:name=".App"
...
ryanjones6
13,797 Pointsryanjones6
13,797 PointsI tried to create a new java file as directed. I move the readInFileToData method to the new class. But it didn't work. I'm currently using a bad fix, by setting one of the columns UNIQUE. -- Downside, this outputs errors in the Android Monitor, but the app runs. -- In the future I will probably just pre-load a SQLite database. But I don't know how to do that currently.
IF anyone wants to play around with the code, here is the GitHub location: https://github.com/drjonesy/LangDictionary