java - How to switch ListView to TextView to retrieve data from database -
i'm looking way retrieve 1 specific datum database, right can retrieve data using listview - not textview.
here java file.
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); db = (new databasehelper(this)).getwritabledatabase(); searchtext = (edittext) findviewbyid (r.id.searchtext); employeelist = (listview) findviewbyid (r.id.list); } public void search(view view) { // || concatenation operation in sqlite cursor = db.rawquery("select _id, firstname, lastname, title employee firstname = ?", new string[]{searchtext.gettext().tostring()}); adapter = new simplecursoradapter( this, r.layout.employee_list_item, cursor, new string[] {"title"}, new int[] {r.id.title}); employeelist.setadapter(adapter); }
my xml files: main.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="fill_parent"> <linearlayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <edittext android:id="@+id/searchtext" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <button android:id="@+id/searchbutton" android:text="search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="search"/> </linearlayout> <listview android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </linearlayout>
employee_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="8px"> <textview android:id="@+id/firstname" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:id="@+id/lastname" android:layout_marginleft="6px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_torightof="@id/firstname"/> <textview android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/firstname"/> </relativelayout>
i tried change specific listview references textview got errors on simplecursoradapter.
you don't need adapter anymore. so, now, instead of this:
adapter = new simplecursoradapter( this, r.layout.employee_list_item, cursor, new string[] {"title"}, new int[] {r.id.title}); employeelist.setadapter(adapter);
use this
if (cursor != null) && (cursor.movetofirst) { txtmytextview.settext(string,format("%s %s %s", cursor.getstring(cur.getcolumnindex("title")), cursor.getstring(cur.getcolumnindex("firstname")), cursor.getstring(cur.getcolumnindex("lastname")))) }
output: mr. abdul hanan
[edit]
if prefer output like: mr. hanan, abdul
then, change string format , parameters order to
txtmytextview.settext(string,format("%s %s, %s", cursor.getstring(cur.getcolumnindex("title")), cursor.getstring(cur.getcolumnindex("lastname")), cursor.getstring(cur.getcolumnindex("firstname"))))
Comments
Post a Comment