java - ListView not displaying text but displaying number (used SimpleAdapter) -
so i'm creating to-do app , i'm newbie @ java programming , android studio, hope can bear me.
here's mainactivity containing simpleadapter display list.
listview lv; arraylist<hashmap<string, string>> list; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_listview_main_activity); lv=(listview)findviewbyid(r.id.listingtasks); dbconnector db=new dbconnector(this); list=new arraylist<hashmap<string,string>>(); list=db.display(); string[] from=new string[]{"_id","task"}; int[] to=new int[]{r.id.id,r.id.tasknamecustom1}; listadapter ad; ad = new simpleadapter(listview_main_activity.this,list, r.layout.custom,from,to); lv.setadapter(ad);
the dbconnector class extends sqliteopenhelper class , contains methods insert , display records in sqlitedatabase. here's dbconnector class:
public class dbconnector extends sqliteopenhelper { public cursor cr; public dbconnector(context context) { super(context, "database", null, 1); } @override public void oncreate(sqlitedatabase db) { string sqlquery = "create table tasktable(_id integer primary key autoincrement, task text);"; db.execsql(sqlquery); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { string sqlqueryupgrade = "drop table if exists tasktable;"; db.execsql(sqlqueryupgrade); oncreate(db); }
here's display method within dbconnector class:
public arraylist<hashmap<string,string>> display() { arraylist<hashmap<string,string>> list = new arraylist<hashmap<string,string>>(); sqlitedatabase sb = this.getreadabledatabase(); string sql = "select * tasktable;"; cr = sb.rawquery(sql, null); //takes cursor position before first record if (cr.movetofirst()) { { hashmap<string,string> hm = new hashmap<string,string>(); hm.put("_id", cr.getstring(0)); hm.put("task", cr.getstring(1)); list.add(hm); } while (cr.movetonext()); } return list; }
here's custom.xml file:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancesmall" android:text="small text" android:id="@+id/id" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="large text" android:id="@+id/tasknamecustom1" />
there no syntactical errors. don't find red underlines anywhere , app runs on phone. problem listview of main activity displays number of tasks doesn't display text of task itself.
here's simpleadapter class:
public class simpleadapter extends baseadapter implements filterable, themedspinneradapter { private final layoutinflater minflater; private int[] mto; private string[] mfrom; private viewbinder mviewbinder; private list<? extends map<string, ?>> mdata; private int mresource; private int mdropdownresource; /** layout inflater used {@link #getdropdownview(int, view, viewgroup)}. */ private layoutinflater mdropdowninflater; private simplefilter mfilter; private arraylist<map<string, ?>> munfiltereddata; /** * constructor * * @param context context view associated simpleadapter running * @param data list of maps. each entry in list corresponds 1 row in list. * maps contain data each row, , should include entries specified in * "from" * @param resource resource identifier of view layout defines views list * item. layout file should include @ least named views defined in "to" * @param list of column names added map associated each * item. * @param views should display column in "from" parameter. these should * textviews. first n views in list given values of first n columns * in parameter. */ public simpleadapter(context context, list<? extends map<string, ?>> data, @layoutres int resource, string[] from, @idres int[] to) { mdata = data; mresource = mdropdownresource = resource; mfrom = from; mto = to; minflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); } /** * @see android.widget.adapter#getcount() */ public int getcount() { return mdata.size(); } /** * @see android.widget.adapter#getitem(int) */ public object getitem(int position) { return mdata.get(position); } /** * @see android.widget.adapter#getitemid(int) */ public long getitemid(int position) { return position; } /** * @see android.widget.adapter#getview(int, view, viewgroup) */ public view getview(int position, view convertview, viewgroup parent) { return createviewfromresource(minflater, position, convertview, parent, mresource); } private view createviewfromresource(layoutinflater inflater, int position, view convertview, viewgroup parent, int resource) { view v; if (convertview == null) { v = inflater.inflate(resource, parent, false); } else { v = convertview; } bindview(position, v); return v; } /** * <p>sets layout resource create drop down views.</p> * * @param resource layout resource defining drop down views * @see #getdropdownview(int, android.view.view, android.view.viewgroup) */ public void setdropdownviewresource(int resource) { mdropdownresource = resource; } /** * sets {@link android.content.res.resources.theme} against drop-down views * inflated. * <p> * default, drop-down views inflated against theme of * {@link context} passed adapter's constructor. * * @param theme theme against inflate drop-down views or * {@code null} use theme adapter's context * @see #getdropdownview(int, view, viewgroup) */ @override public void setdropdownviewtheme(resources.theme theme) { if (theme == null) { mdropdowninflater = null; } else if (theme == minflater.getcontext().gettheme()) { mdropdowninflater = minflater; } else { final context context = new contextthemewrapper(minflater.getcontext(), theme); mdropdowninflater = layoutinflater.from(context); } } @override public resources.theme getdropdownviewtheme() { return mdropdowninflater == null ? null : mdropdowninflater.getcontext().gettheme(); } @override public view getdropdownview(int position, view convertview, viewgroup parent) { final layoutinflater inflater = mdropdowninflater == null ? minflater : mdropdowninflater; return createviewfromresource(inflater, position, convertview, parent, mdropdownresource); } private void bindview(int position, view view) { final map dataset = mdata.get(position); if (dataset == null) { return; } final viewbinder binder = mviewbinder; final string[] = mfrom; final int[] = mto; final int count = to.length; (int = 0; < count; i++) { final view v = view.findviewbyid(to[i]); if (v != null) { final object data = dataset.get(from[i]); string text = data == null ? "" : data.tostring(); if (text == null) { text = ""; } boolean bound = false; if (binder != null) { bound = binder.setviewvalue(v, data, text); } if (!bound) { if (v instanceof checkable) { if (data instanceof boolean) { ((checkable) v).setchecked((boolean) data); } else if (v instanceof textview) { // note: keep instanceof textview check @ bottom of these // ifs since lot of views textviews (e.g. checkboxes). setviewtext((textview) v, text); } else { throw new illegalstateexception(v.getclass().getname() + " should bound boolean, not " + (data == null ? "<unknown type>" : data.getclass())); } } else if (v instanceof textview) { // note: keep instanceof textview check @ bottom of these // ifs since lot of views textviews (e.g. checkboxes). setviewtext((textview) v, text); } else if (v instanceof imageview) { if (data instanceof integer) { setviewimage((imageview) v, (integer) data); } else { setviewimage((imageview) v, text); } } else { throw new illegalstateexception(v.getclass().getname() + " not " + " view can bounds simpleadapter"); } } } } } /** * returns {@link viewbinder} used bind data views. * * @return viewbinder or null if binder not exist * * @see #setviewbinder(android.widget.simpleadapter.viewbinder) */ public viewbinder getviewbinder() { return mviewbinder; } /** * sets binder used bind data views. * * @param viewbinder binder used bind data views, can null * remove existing binder * * @see #getviewbinder() */ public void setviewbinder(viewbinder viewbinder) { mviewbinder = viewbinder; } /** * called bindview() set image imageview if * there no existing viewbinder or if existing viewbinder cannot * handle binding imageview. * * method called instead of {@link #setviewimage(imageview, string)} * if supplied data int or integer. * * @param v imageview receive image * @param value value retrieved data set * * @see #setviewimage(imageview, string) */ public void setviewimage(imageview v, int value) { v.setimageresource(value); } /** * called bindview() set image imageview if * there no existing viewbinder or if existing viewbinder cannot * handle binding imageview. * * default, value treated image resource. if * value cannot used image resource, value used * image uri. * * method called instead of {@link #setviewimage(imageview, int)} * if supplied data not int or integer. * * @param v imageview receive image * @param value value retrieved data set * * @see #setviewimage(imageview, int) */ public void setviewimage(imageview v, string value) { try { v.setimageresource(integer.parseint(value)); } catch (numberformatexception nfe) { v.setimageuri(uri.parse(value)); } } /** * called bindview() set text textview if * there no existing viewbinder or if existing viewbinder cannot * handle binding textview. * * @param v textview receive text * @param text text set textview */ public void setviewtext(textview v, string text) { v.settext(text); } public filter getfilter() { if (mfilter == null) { mfilter = new simplefilter(); } return mfilter; } /** * class can used external clients of simpleadapter bind * values views. * * should use class bind values views not * directly supported simpleadapter or change way binding * occurs views supported simpleadapter. * * @see simpleadapter#setviewimage(imageview, int) * @see simpleadapter#setviewimage(imageview, string) * @see simpleadapter#setviewtext(textview, string) */ public static interface viewbinder { /** * binds specified data specified view. * * when binding handled viewbinder, method must return true. * if method returns false, simpleadapter attempts handle * binding on own. * * @param view view bind data * @param data data bind view * @param textrepresentation safe string representation of supplied data: * either result of data.tostring() or empty string * never null * * @return true if data bound view, false otherwise */ boolean setviewvalue(view view, object data, string textrepresentation); } /** * <p>an array filters constrains content of array adapter * prefix. each item not start supplied prefix * removed list.</p> */ private class simplefilter extends filter { @override protected filterresults performfiltering(charsequence prefix) { filterresults results = new filterresults(); if (munfiltereddata == null) { munfiltereddata = new arraylist<map<string, ?>>(mdata); } if (prefix == null || prefix.length() == 0) { arraylist<map<string, ?>> list = munfiltereddata; results.values = list; results.count = list.size(); } else { string prefixstring = prefix.tostring().tolowercase(); arraylist<map<string, ?>> unfilteredvalues = munfiltereddata; int count = unfilteredvalues.size(); arraylist<map<string, ?>> newvalues = new arraylist<map<string, ?>>(count); (int = 0; < count; i++) { map<string, ?> h = unfilteredvalues.get(i); if (h != null) { int len = mto.length; (int j=0; j<len; j++) { string str = (string)h.get(mfrom[j]); string[] words = str.split(" "); int wordcount = words.length; (int k = 0; k < wordcount; k++) { string word = words[k]; if (word.tolowercase().startswith(prefixstring)) { newvalues.add(h); break; } } } } } results.values = newvalues; results.count = newvalues.size(); } return results; } @override protected void publishresults(charsequence constraint, filterresults results) { //noinspection unchecked mdata = (list<map<string, ?>>) results.values; if (results.count > 0) { notifydatasetchanged(); } else { notifydatasetinvalidated(); } } }
}
can please please me? thank you!
hey first replace code in dbconnector in display method.
hm.put("_id", cr.getstring(0));
with
hm.put("_id", cr.getint(0)+"");
and post getview() method of simple adapter, or whole class can further.
okk try replace
string text = data == null ? "" : data.tostring();
with
string text = data == null ? "" : (string)data;
put code in
view v; if (convertview == null) { v = inflater.inflate(resource, parent, false); } else { v = convertview; } bindview(position, v); return v;
into
@override public view getdropdownview(int position, view convertview, viewgroup parent) { view v; if (convertview == null) { v = inflater.inflate(resource, parent, false); } else { v = convertview; } bindview(position, v); return v;
}
hope work you.
Comments
Post a Comment