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?
You have named your Activity
ListViewwhich 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 INTEGERcolumn set as thePRIMARY KEY, Android requires this_idcolumn 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.)Your code should look more like this:
You might want to consider using the SQLiteDatabase.query() methods, I believe it is a touch faster:
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: