Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7072147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:46:24+00:00 2026-05-28T05:46:24+00:00

I like to ask, how I can read data from my databast test_database.db and

  • 0

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:46:24+00:00Added an answer on May 28, 2026 at 5:46 am

    You really haven’t given us a lot of information… I’m not sure why your code is crashing, what the file R.layout.homework contains, 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 of BaseAdapter if you wish), bind the adapter to the columns of interest, and end your onCreate() method with something like setListAdapter(mAdapter). You will also have to be careful that you don’t accidentally open multiple instances of a Cursor or SQLiteDatabase without 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 Activity life-cycle and how to create a SQLiteDatabase correctly 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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask how can i make an html anchor (a element)
I would like ask if there's a way to download an android layout from
I'd like to know if there exists a program that can read DTD specifications,
How can i use linq to list all pixels from image and process on
What I would like ask is best illustrated by an example, so bear with
I would like to ask for some simple examples showing the uses of <div>
I would like to ask you which automated build environment you consider better, based
I would like to ask for a reccomended solution for this: We have a
I would like to ask how licensing works on multiple instances? Suppose that I
I would like to ask is there any way to achieve this functionality: I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.