java - Populate ListView with SQL database ERROR -
im new android, , im trying display data sql database listview seems able retrieve data de database, way of inserting listview seems incorrect. log cat throws me following error:
caused by: android.content.res.resources$notfoundexception: string resource id #0x2
if please @ code , give me advice of how can fix it, apreciate :), in advance
this main activity code
public class mainactivity extends listactivity { string key_id = "_id"; string key_hole = "hoyo"; string key_par = "par"; string key_handicap = "handicap"; string key_yards = "yardaje"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); databasehelper mydbhelper = new databasehelper(this); arraylist<hashmap<string, string>> menuitems = new arraylist<hashmap<string, string>>(); try { mydbhelper.createdatabase(); } catch (ioexception ioe) { throw new error("unable create database"); } try { mydbhelper.opendatabase(); }catch(sqlexception sqle){ throw sqle; } cursor c = mydbhelper.getallcoursecontacts(); if(c.movetofirst()) { do{ hashmap<string, string> map = new hashmap<string, string>(); map.put(key_hole, c.getstring(1)); map.put(key_par, getstring(2)); map.put(key_handicap, getstring(3)); map.put(key_yards,getstring(4)); }while(c.movetonext()); } mydbhelper.close(); listadapter adapter = new simpleadapter(this, menuitems, r.layout.list_item, new string[] { key_hole, key_par, key_handicap, key_yards }, new int[] { r.id.hole, r.id.par, r.id.handicap,r.id.yards }); setlistadapter(adapter); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } }
my activity xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <listview android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawselectorontop="false"/> </linearlayout>
and item_list 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="match_parent" android:orientation="vertical" > <textview android:id="@+id/hole" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize = "40sp"/> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <textview android:id="@+id/par" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp"/> <textview android:id="@+id/handicap" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp"/> <textview android:id="@+id/yards" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="20sp"/> </linearlayout> </linearlayout>
the problem here:
map.put(key_hole, c.getstring(1)); // correct map.put(key_par, getstring(2)); // incorrect! map.put(key_handicap, getstring(3)); // incorrect! map.put(key_yards,getstring(4)); // incorrect!
you have getstring()
instead of c.getstring()
last three.
Comments
Post a Comment