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 8437077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:20:16+00:00 2026-06-10T07:20:16+00:00

I am programming an android app that should use a database to store data

  • 0

I am programming an android app that should use a database to store data and read from it. Using this tutorial (on archive.org) I got the app to create a database and I’m able to create new entries, however, I don’t know, how to read the database to get the stored data in a ListView. I know there are many similar questions on this website but it seems none of them apply to the way, the database from the tutorial works.

Code:

import java.util.Calendar;

import maturarbeit.nicola_pfister.studenttools.database.DBAdapter;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;


public class Marks extends ListActivity {

DBAdapter db = new DBAdapter(this);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.marks);
}

@Override
protected void onPause() {
    super.onPause();
    db.close();
}

@Override
protected void onResume() {
    super.onResume();
    db.open();

    getData();
}

@SuppressWarnings("deprecation")
private void getData() {
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
            android.R.layout.simple_list_item_1, 
            db.getAllMarks(), 
            new String[] { "value" }, 
            new int[] { android.R.id.text1 });

    ListView listView = (ListView) findViewById(R.id.marks_list);
    listView.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.marks, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_add:
        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int month = cal.get(Calendar.MONTH);
        final String date = day + "." + month;
        Builder builder = new Builder(this);
        builder
            .setTitle(R.string.dialog_addmarks)
            .setItems(R.array.markslist, new OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    @SuppressWarnings("unused")
                    long id;
                    String selection = getResources().getStringArray(R.array.markslist)[which];
                    id = db.insertMark(date, "Default", selection);
                    }
            })
            .show();
        getData();
        break;
    case R.id.menu_delete:
            //Deleting function yet to be implemented.
        break;
    }
    return super.onOptionsItemSelected(item);
}
}

Edit: The ListView ID was wrong since it had to be android:list.

  • 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-06-10T07:20:18+00:00Added an answer on June 10, 2026 at 7:20 am

    Using the database format in the tutorial that you linked to, every row has an _id, isbn, title, and publisher. Now let’s assume that you want to display every title in a ListView:

    db = new DBAdapter(this);
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
            android.R.layout.simple_list_item_1, 
            db.getAllTitles(), 
            new String[] { "title" }, 
            new int[] { android.R.id.text1 });
    
    ListView listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(adapter);
    

    (You don’t need to loop through the Cursor yourself, an adapter does this work for you!)

    The last two parameters in your SimpleCursorAdapter constructor are what you are missing. They are the "from" and "to" parameters:

    • We want to get the name of each book which is stored in the column name title, this is where we get the information "from".
    • Next we need to tell it where "to" go: android.R.id.text1 is a TextView in the android.R.layout.simple_list_item_1 layout. (You can dig through your SDK and see the simple_list_item_1.xml file yourself or just trust me for the moment.)

    Now both the "from" and "to" parameters are arrays because we can pass more than one column of information, try this adapter as well:

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
            android.R.layout.simple_list_item_2, 
            db.getAllTitles(), 
            new String[] { "title", "publisher" }, 
            new int[] { android.R.id.text1, android.R.id.text2 });
    

    With this adapter the books in their database will displayed by title, then publisher. All we had to do is use a layout android.R.layout.simple_list_item_2 that takes two fields and define which columns go to which TextViews.

    There’s plenty more to learn but maybe this will give you some basics.


    Last Comment

    Off the top of my head, to refresh the ListView after adding new data try this:

    public void onClick(DialogInterface dialog, int which) {
        String selection = getResources().getStringArray(R.array.markslist)[which];
        db.insertMark(date, "Default", selection);
        cursor.requery();
        adapter.notifyDataSetChanged();
    }
    

    You’ll have to define adapter and create a variable for cursor but that’s simple:

    public class Marks extends ListActivity {
        SimpleCursorAdapter adapter;
        Cursor cursor;
        DBAdapter db = new DBAdapter(this);
        ...
    

    And change getData() accordingly:

    private void getData() {
        cursor = db.getAllMarks();
        adapter = new SimpleCursorAdapter(this, 
                android.R.layout.simple_list_item_1, 
                cursor, 
                new String[] { "value" }, 
                new int[] { android.R.id.text1 });
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new in Android Programming. I'm trying to write an app that uses USB
I'm programming an Android app, where I have to store pictures to some online
I am new to android programming and i am trying to develop this app
Android programming. I have a json returning the date from a database as a
Possible Duplicate: Android App high cpu-usage I am programming a app that has to
The android-app I'm programming is going to have a database of products, say 150-250
this is my first Android app and I'm trying to use Sockets to send
I am new to android programming and development. I am working on app that
I programming an android app that can drag image. Top and left of image
I'am begginer in android programming and I'am trying to create app that is logging

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.