android - Listview not showing text from my Database -
i have listview setup shows 3 rows numbers 1-3 , it's not showing entries database.i've tried find answer listview subject vague , can't find clear answer on how show text db entries. here codes xml layout,listview class , cursor entry in database.
layout:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/nfbackground" android:orientation="vertical" > <imageview android:id="@id/titlebar" android:layout_width="wrap_content" android:layout_height="66dp" android:src="@drawable/nftitlebar" /> <listview android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/searchimageview" android:textcolor="@color/black" android:textsize="25dp" android:textstyle="bold" android:text="" android:orientation="vertical" > </listview> </linearlayout>
code:
package com.fullfrontalgames.numberfighter; import com.fullfrontalgames.numberfighter.dbadapter; import com.fullfrontalgames.numberfighter.r; import android.app.activity; import android.database.cursor; import android.os.bundle; import android.support.v4.widget.simplecursoradapter; import android.widget.listadapter; import android.widget.listview; public class playafriend extends activity { @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.playafriend); listview friendlv = (listview) findviewbyid(android.r.id.list); dbadapter db = new dbadapter(this); db = db.open(); cursor friendslist = db.getallfriends(); string[] = new string[] {"id","username","friends"}; // column/columns here int[] = new int[] {android.r.id.text1}; @suppresswarnings("deprecation") listadapter cursoradapter = new simplecursoradapter(this, android.r.layout.simple_list_item_1, friendslist, from, to); friendlv.setadapter(cursoradapter); db.close(); } }
code:
public cursor getallfriends() { cursor cursor = db.rawquery("select rowid _id,* nfdb", null); int irow = cursor.getcolumnindex("id"); int iname = cursor.getcolumnindex("username"); int ifriends = cursor.getcolumnindex("friends"); if (cursor.movetofirst()) { { textview friendslist = new textview(context); friendslist.setid(integer.parseint(cursor.getstring(irow))); friendslist.settext(cursor.getstring(iname)); friendslist.settext(cursor.getstring(ifriends)); } while (cursor.movetonext()); } return cursor; }
you need create layout 2 textview listview columns
list_items.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:orientation="vertical" > <textview android:id="@+id/textview_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:id="@+id/textview_friends" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>
change
string[] = new string[] {"username","friends"}; // column/columns here int[] = new int[] {textview_name, textview_friends}; simplecursoradapter cursoradapter = new simplecursoradapter(this, r.layout.list_items, friendslist, from, to, 0); friendlv.setadapter(cursoradapter); //db.close(); close db in ondestroy
change
public cursor getallfriends() { return db.rawquery("select * nfdb", null); }
Comments
Post a Comment