1

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?

4
  • I see that you create a table here: messagedb.execSQL("CREATE TABLE IF NOT EXISTS tab2(sender INT(13),body varchar)"); But have you added any rows to you database? Commented Jul 2, 2012 at 16:36
  • this database created already and add sender and body values...here just open that database Commented Jul 2, 2012 at 16:39
  • this database created already and add sender and body values...here just open that database.........any idea? Commented Jul 2, 2012 at 16:41
  • Are you sure your cursor isn't empty? Commented Jul 2, 2012 at 17:29

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

9 Comments

@Dipin.K.G I didn't think it would be perfect, but I'll need some information to help you. Please add the LogCat errors and the code you are currently using to your question.
How can i open this Body message while i am clicking the items on list view
and also how can i add date and time also adding in this list as receiving date and time
Use an OnItemClickListener to catch which row the user clicks, use the row's _id inside this listener to open the Body of the message. (But where do you want to open it: in a new Activity, a dialog, a TextView?)
You can add a time column to your database, like this: time TIMESTAMP DEFAULT current_timestamp. DEFAULT current_timestamp means if you this column is null when you insert a row, the database will automatically set time to the date/time at that moment.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.