I like to ask, how I can read data from my databast “test_database.db” and list them on my screen?
Database: test_database.db
Table: tbl_homework
In table homework are those entries:
ID HW
1 Mathematics
2 Physics
3 Sports
So i know how I can send ONE entry to a toast or alert. (With cursor)
I read that tutorial here but I can’t build that example for my app.
http://coenraets.org/blog/android-samples/androidtutorial/
What do I have?
Java: CustomDatabaseHelper.java, homework.java, main.java
Layout: homework.xml, main.xml
homework.java is a normal activity I tried to change it to ListActivity. But then it crashes.
Here the code: (It works fine)
public class homework extends Activity {
SQLiteDatabase db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homework);
//Create Database
db = openOrCreateDatabase("test_database.db",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
//Create table tbl_homework
final String CREATE_TABLE_HW =
"CREATE TABLE IF NOT EXISTS tbl_homework ("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "hw TEXT);";
db.execSQL(CREATE_TABLE_HW);
press_cmd_back();
press_cmd_test_data();
}
//What will happen if cmd_back gets pressed
private void press_cmd_back(){
Button cmd_back = (Button)findViewById(R.id.cmd_back);
cmd_back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent Intent_main = new Intent(homework.this, Noten_HausaufgabenActivity.class);
startActivity(Intent_main);
}
});
}
//What will happen if cmd_test_data gets pressed
private void press_cmd_test_data(){
Button cmd_test_data = (Button)findViewById(R.id.cmd_test_data);
cmd_test_data.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
open_database_rw();
final String INSERT_DATA_1 = "INSERT INTO tbl_homework ('hw') VALUES ('Mathematics')";
final String INSERT_DATA_2 = "INSERT INTO tbl_homework ('hw') VALUES ('Physics')";
final String INSERT_DATA_3 = "INSERT INTO tbl_homework ('hw') VALUES ('Sports')";
db.execSQL(INSERT_DATA_1);
db.execSQL(INSERT_DATA_2);
db.execSQL(INSERT_DATA_3);
}
});
}
//Open Database RW
private void open_database_rw() {
db = SQLiteDatabase.openDatabase("/data/data/test.marco.notenha/databases/test_database.db",
null, SQLiteDatabase.OPEN_READWRITE);
}
}
You really haven’t given us a lot of information… I’m not sure why your code is crashing, what the file
R.layout.homeworkcontains, etc.You need two things to bind data to a ListView in your ListActivity. 1) You need a Cursor to read the columns of interest, and 2) you need an Adapter to bind the database rows to a ListView. Therefore, you need to implement a method that queries the database and returns a Cursor over the database, and you also need to create a
SimpleCursorAdapter mAdapter(or some other subclass ofBaseAdapterif you wish), bind the adapter to the columns of interest, and end youronCreate()method with something likesetListAdapter(mAdapter). You will also have to be careful that you don’t accidentally open multiple instances of aCursororSQLiteDatabasewithout closing them, or else the system will throw an exception.Your best bet is to check out the notepad tutorial on the Android Developers site. Really try to understand how everything works and you’ll learn a lot. It’s a great tutorial and it’s very similar to what you need to get done (that is, it queries a database and binds “todo notes” to a list on the main screen). You’ll also learn how to manage the
Activitylife-cycle and how to create aSQLiteDatabasecorrectly from scratch.Hope that made sense. If it didn’t, read through the notepad tutorial and then come back and read my answer and hopefully it’ll make a lot more sense. I was in the same exact position as you a few months ago… it gets easier but you really have to sit down and try to understand how this stuff works first, and the notepad tutorial is a great place to start!
Good luck!