android - adding values to listView -


i trying populate listview....but cant add 2 string values database used in simplecursoradapter.... one, 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 , add sender , body values...here open database     cursor  cur=messagedb.rawquery("select sender, body 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[] = 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 me populate listview?

you have named activity listview built-in class name, highly recommend changing unique prevent confusion or naming conflicts.

in sqlite table, ought have _id integer column set primary key, android requires _id column bind data listview, spinner, etc. (technically sqlite3 creates column automatically, believe best define yourself.)

    messagedb.execsql(         "create table if not exists tab2(" +         " _id integer primary key," +         " sender int(13)," +         " body varchar)"); 

your code should more this:

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.my_list);      // using sqliteopenhelper might best, i'll assume command works     messagedb=listview.this.openorcreatedatabase("message",0, null);      cursor cur = messagedb.rawquery("select _id, sender, body tab2", null);     string[] = new string[] { "_id", "sender", "body" }     int[] = 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 consider using sqlitedatabase.query() methods, believe touch faster:

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

i recommend defining static variables column names future ease-of-coding.

lastly, should work table without _id column, again don't recommend it:

cursor cur = messagedb.rawquery("select rowid _id, sender, body tab2", null); 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -