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

  • Home
  • SEARCH
  • 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 8475319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:51:39+00:00 2026-06-10T17:51:39+00:00

I’ve been beating my head around trying to populate a ListView with data from

  • 0

I’ve been beating my head around trying to populate a ListView with data from an SQLite database and can’t seem to figure it out. I’ve read countless tutorials and equally countless posts here, but I’m obviously not getting something critical. Was hoping someone could give me a hint as to why the following two pieces of code aren’t working together, or if I should be looking at something else entirely. Any help would be appreciated. The result I’m getting is a force close.

Method that initiates populating ListView object

public void displayNurseRoster(){

    listView = (ListView) findViewById(R.id.list);

    // create instance of DbCommunicator
    DbCommunicator rosterView = new DbCommunicator(this);

    // open instance
    rosterView.open();

    // instantiate SimpleCursorAdapter instance and set value
    SimpleCursorAdapter cursorAdapter;
    cursorAdapter = rosterView.getRosterListViewAdapter(this);

    // close database instance
    rosterView.close();

    // set adapter to listView
    listView.setAdapter(cursorAdapter);
}

Method that returns SimpleCursorAdapter:

public SimpleCursorAdapter getRosterListViewAdapter (Context context) {

    // method variables 
    int[] to = new int[] {R.id.rosterListLname};
    // used ArrayList because of unpredictability of array length
    List<String> dataArray = new ArrayList<String>(); 
    String[] columns = new String[] {KEY_NURSE_ROWID, KEY_LNAME}; 

    // create cursor
    Cursor cursor = sqldb.query(NURSE_TABLE, columns, null, null, null,
            null, KEY_LNAME);

    int iLname = cursor.getColumnIndex(KEY_LNAME);

    cursor.moveToFirst();

    String result = "";

    while(!cursor.isAfterLast()){
        result = result 
                + cursor.getString(iLname) + "\n";
        dataArray.add(result);

    }
    // convert ArrayList to String array for use with SimpleCursorAdapter
    String [] from = (String[]) dataArray.toArray();

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(context,
            R.layout.edit_roster, cursor, from, to);

    return 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-10T17:51:40+00:00Added an answer on June 10, 2026 at 5:51 pm

    Okay, I got it to work and wanted to share to help out anyone else struggling with this and 2) to get feedback on my fix in case anyone with more experience has a better suggestion.

    I actually wanted to show three items in the list view, so here’s the code that worked. One of the problems I was having was that I was extending ListActivity, but found in another StackOverflow post that that is not a good idea when there is only one list view to worry about.

    package com.deadEddie.staffingmanagement;
    
    import android.app.Activity;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleCursorAdapter;
    
    public class EditRoster extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_roster);
        displayNurseRoster();   
    }
    
    public void displayNurseRoster(){
    
        ListView listView = (ListView) findViewById(R.id.list);
    
        int[] to = new int[] {
                R.id.rosterListLname, 
                R.id.rosterListFname, 
                R.id.rosterListMI };
    
        String[] from = new String [] {
                DbCommunicator.KEY_LNAME, 
                DbCommunicator.KEY_FNAME, 
                DbCommunicator.KEY_MI};
    
        // create instance of DbCommunicator
        DbCommunicator rosterView = new DbCommunicator(this);
    
        // open instance
        rosterView.open();
    
        // get & manage cursor
        Cursor cursor = rosterView.getRosterCursor(this);
        startManagingCursor(cursor);
    
        // instantiate cursor adaptor
        ListAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.nurse_list, cursor, from, to);
        cursor.moveToNext();
    
        // set adapter to listView
        listView.setAdapter(adapter);
    }// displayNurseRoster()
    }
    

    Here’s the method in my DbCommunicator class. What finally made the difference was that I was not creating an instance of SQLiteQueryBuilder and using that, but instead I was using sqldb.query(…), as shown above. I’m not sure what the difference is, but it finally did the trick. If anyone would like to share, please do.

    public Cursor getRosterCursor (Context context) {
    
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        queryBuilder.setTables(NURSE_TABLE);
    
        Cursor cursor = queryBuilder.query(sqldb, new String[] {
                KEY_NURSE_ROWID, 
                KEY_LNAME,
                KEY_FNAME,
                KEY_MI},
                null, null, null, null, null);
    
        return cursor;
    }
    

    A couple other newbie lessons for anyone else out there:
    1. Always use the “_id” field in the cursor. The cursor cannot function without that.
    2. The while or for loop is not necessary for a simple list view. The cursor adapter handles that.
    3. There is no need to call the moveToFirst() method on the cursor.

    Hope that’s helpful to someone.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a jquery bug and I've been looking for hours now, I can't
Does anyone know how can I replace this 2 symbol below from the string
I have a view passing on information from a database: def serve_article(request, id): served_article
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

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.