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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:52:04+00:00 2026-06-07T00:52:04+00:00

I am trying to populate a ListView ….but I cant add 2 string values

  • 0

I am trying to populate a ListView….but I cant add 2 string values from database used in SimpleCursorAdapter….
Any one, help me

Code

ListView.java

public class ListView extends ListActivity {

SQLiteDatabase messagedb;
List<String> senderArray = new ArrayList<String>();
List<String> bodyArray = new ArrayList<String>();

String[] simpleSenderArray = new String[ senderArray.size() ];

String[] simpleBodyArray = new String[ bodyArray.size() ];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_list);

    senderArray.toArray( simpleSenderArray );
    bodyArray.toArray( simpleBodyArray );

    messagedb=ListView.this.openOrCreateDatabase("message",0, null);

            //This database created already and add sender and body values...here just open that database
    Cursor  cur=messagedb.rawQuery("select sender, body from tab2", null);
    while(cur.moveToNext())
    {
        String sender = cur.getString(cur.getColumnIndex("sender"));
        String body = cur.getString(cur.getColumnIndex("body"));
        senderArray.add(sender);
        bodyArray.add(body);
    }
    cur.close();


    messagedb.close();

     int[] to = new int[] { R.id.sender_entry, R.id.body_entry };

     SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.my_list_entry, cur,simpleSenderArray, to);

     this.setListAdapter(mAdapter);


}

}

my_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

<ListView

    android:id="@android:id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />


</LinearLayout>

my_list_entry.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
<TextView
    android:id="@+id/sender_entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="28dip" />
 <TextView
    android:id="@+id/body_entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="28dip" />
</LinearLayout>

Can somebody help me to populate my ListView?

  • 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-07T00:52:05+00:00Added an answer on June 7, 2026 at 12:52 am

    You have named your Activity ListView which is already a built-in class name, I highly recommend changing it to something unique to prevent any confusion or naming conflicts.

    In your SQLite table, you ought to have an _id INTEGER column set as the PRIMARY KEY, Android requires this _id column to bind the data to any ListView, Spinner, etc. (Technically SQLite3 creates a column like this automatically, but I believe it is best to define it yourself.)

        messagedb.execSQL(
            "CREATE TABLE IF NOT EXISTS tab2(" +
            " _id INTEGER PRIMARY KEY," +
            " sender INT(13)," +
            " body varchar)");
    

    Your code should look more like this:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_list);
    
        // Using a SQLiteOpenHelper might be best, but I'll assume that this command works
        messagedb=ListView.this.openOrCreateDatabase("message",0, null);
    
        Cursor cur = messagedb.rawQuery("select _id, sender, body from tab2", null);
        String[] from = new String[] { "_id", "sender", "body" }
        int[] to = new int[] { R.id.sender_entry, R.id.body_entry };
    
        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.my_list_entry, cur, from, to);
    
        this.setListAdapter(mAdapter);
    }
    

    You might want to consider using the SQLiteDatabase.query() methods, I believe it is a touch faster:

    Cursor cur = messagedb.query("tab2", new String[] {"_id", "sender", "body"}, null, null, null, null, null);
    

    I also recommend defining static variables for all you column names for future ease-of-coding.

    Lastly, this should work for a table without the _id column, but again I don’t recommend it:

    Cursor cur = messagedb.rawQuery("select rowid as _id, sender, body from tab2", null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to populate a listview from another class, but I'm geting this error:
I'm trying to populate listview from my SQLite database... this is how I get
I am trying to populate ListView Item with an object from MyClass. One of
I am trying to populate a listview from sqlite database. My code is using
I'm trying to populate an android ListView with data from DB. I've Created a
im trying to populate my listview beneath my tab function but for some reason
I'm trying to populate images from a directory into a listview with a small
I'm trying to populate a listview with products. I can do it, but I
I'm trying to use a SimpleCursorAdapter to populate a ListView. However, constructing the SimpleCursorAdapter
I'm trying to call .notifyDataSetChange() on a SimpleCursorAdapter displayed in a ListView from an

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.