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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:45:09+00:00 2026-06-04T00:45:09+00:00

I am fetching data from an sqlite database to Android mobile in a listview.

  • 0

I am fetching data from an sqlite database to Android mobile in a listview. The data comes one by one in the list. I will display two textview in custom listview using cursor adapter.
Main.java

public class Main extends Activity  {

    private SQLiteAdapter mySQLiteAdapter;
    /** Called when the activity is first created. */


        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ListView listContent = (ListView) findViewById(R.id.customlist);

            /*
             * Create/Open a SQLite database and fill with dummy content and close
             * it
             */
            mySQLiteAdapter = new SQLiteAdapter(this);
            mySQLiteAdapter.openToWrite();
            // mySQLiteAdapter.deleteAll();
            mySQLiteAdapter = new SQLiteAdapter(this);
            mySQLiteAdapter.openToRead();

            final Cursor cursor = mySQLiteAdapter.queueAll();
            startManagingCursor(cursor);

            String[] from = new String[] { SQLiteAdapter.KEY_CONTENT };
            int[] to = new int[] { R.id.text };

            final SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
                    R.layout.row, cursor, from, to);

            listContent.setAdapter(cursorAdapter);

            mySQLiteAdapter.close();
            }
    }

SQLiteAdapter.java

public class SQLiteAdapter {

    public static final String MYDATABASE_NAME = "test";
    public static final String MYDATABASE_TABLE = "test";
    public static final int MYDATABASE_VERSION = 1;     
    public static final String KEY_ID = "_id";
    public static final String KEY_CONTENT = "test"


    // create table MY_DATABASE (ID integer primary key, Content text not null);
    private static final String SCRIPT_CREATE_DATABASE = "create table test (_id integer primary key autoincrement, "
             + "test text not null)";

    private SQLiteHelper sqLiteHelper;
    private SQLiteDatabase sqLiteDatabase;

    private Context context;

    public SQLiteAdapter(Context c) {
        context = c;
    }

    public SQLiteAdapter openToRead() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
                MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getReadableDatabase();
        return this;
    }

    public SQLiteAdapter openToWrite() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,
                MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        sqLiteHelper.close();
    }


    public int deleteAll() {
        return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
    }

    public Cursor queueAll() {
        String[] columns = new String[] { KEY_ID, KEY_CONTENT };
        Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null,
                null, null, null, null);

        int id=cursor.getColumnIndex(KEY_CONTENT);
        cursor.moveToFirst();
        if(cursor!=null)
        {
            do      
            {
                String keyid=cursor.getString(id);
                System.out.println(keyid);
            }while(cursor.moveToNext());
        }

        return cursor;
    }

    private static class SQLiteHelper extends SQLiteOpenHelper {

        public SQLiteHelper(Context context, String name,
                CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(SCRIPT_CREATE_DATABASE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

        }
    }
    }

Using the above code I can display key_content in listview. Now I will display key_id, and key_content both in listview using cursor adapter.

  • 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-04T00:45:10+00:00Added an answer on June 4, 2026 at 12:45 am
     Cursor cursor = mySQLiteAdapter.queueAll();
    

    in queueAll() method

     Cursror c = sqLiteDatabase.rawQuery("select * from TABLENAME",null);
    
     return c;
    

    instead of * you can add your table column name like key_id,key_content

    And get id and name

    String myContent = cursor.getString(cursor.getColumnIndex("key_content")).toString().trim();
    
        long id = cursor.getLong(cursor.getColumnIndex("key_id"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to fetch data from SQLITE database and display it in a LIST.
I'm creating a application that will fetch data from an SQLite database and display
i am fetching a data from sqlite in android which is as follows URL
I am having one array where i am fetching data from database for some
I'm currently fetching data from a MySQL database using JDBC and executeQuery . One
I am fetching the data from a database and I am appending it to
I'm fetching data from external database (I cannot edit it so don't suggest that
I want to create a dynamic (fetching data from the database) XML sitemap which
I can't get nl2br function to work after fetching data from my database: $result
Should application expect the unexpected when fetching data from database? Lets say that we

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.