android - Keep bouncing between errors, can't find good fix for Spinner/Adapter -
i can't seem figure out simplecursoradapter, every time fix 1 error, 1 pops , if follow steps fix one, first 1 comes again. feel i'm going in circles here, here's chunk of code i'm trying debug, note, first part creating db, figured helpful in figuring out.
sqlitedatabase rpgdb = null; string classfields = " (classid, classname, classhp)"; try { rpgdb = this.openorcreatedatabase("rpgdb", mode_private, null); rpgdb.execsql("drop table if exists " + classtable); rpgdb.execsql("create table if not exists " + classtable + " (classid int(3), classname text, classhp int(4));"); rpgdb.execsql("insert " + classtable + classfields + " values (1, 'warrior', 10);"); rpgdb.execsql("insert " + classtable + classfields + " values (2, 'rogue', 7);"); rpgdb.execsql("insert " + classtable + classfields + " values (3, 'mage', 5);"); string query = "select classname _id " + classtable; cursor cursor = rpgdb.rawquery(query, null); rpgdb.close(); int[] = new int[] { r.id.classdropdown }; string[] spinnerfields = new string[] { "_id" }; /** * test code check value of cursor */ int count = cursor.getcount(); cursor.movetofirst(); while (cursor.isafterlast() == false) { string test = cursor.getstring(0); cursor.movetonext(); } simplecursoradapter cursoradapter = new simplecursoradapter(this, r.layout.main, cursor, spinnerfields, to); int cursorcount = cursoradapter.getcount(); // cursoradapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner classdropdown = (spinner) this.findviewbyid(r.id.classdropdown); classdropdown.setadapter(cursoradapter); } catch (exception ex) { system.out.println("exception: " + ex); } populating spinner simplecursoradapter seems ridiculously troublesome.
edit: error code
invalid statement in fill window if comment out rpgdb.close() line
spinner not view can bounds simplecursoradapter if change select statement , spinnerfields (with db commented out):
string query = "select classname " + classtable; string[] spinnerfields = new string[] { "classname" }; i get:
column '_id' not exist edit xml: here main.xml (also id main) xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@color/white" > <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textsize="11pt" android:text="@string/hello" android:textcolor="@color/basetextcolor" /> <spinner android:id="@+id/classdropdown" android:layout_width="match_parent" android:layout_height="wrap_content" /> </linearlayout> explanation: if comes upon in future, luksprog, understand bit better (and showed me fixes):
the android.r.id , android.r.layout "ids/layouts" default xml layouts android components such textfield , spinner "list" layouts. spinner object not complete on own. need xml portion define how list looks , 1 lines (the text1 default android textfield).
so in short, assign "to" field textfield or use 1 of default android ones such android.id.text1, in order use believe have use layout contains default android field, andoid.r.layout (in case, simple_spinner_item). lastly, set dropdownresource own xml or android default (mine android.r.layout.simple_spinner_dropdown_item).
check this:
string query = "select _id, classname " + classtable; cursor cursor = rpgdb.rawquery(query, null); int[] = new int[] { android.r.id.text1 }; string[] spinnerfields = new string[] { "classname"}; simplecursoradapter cursoradapter = new simplecursoradapter(this, android.r.layout.simple_spinner, cursor, spinnerfields, to); cursoradapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner classdropdown = (spinner) this.findviewbyid(r.id.classdropdown); classdropdown.setadapter(cursoradapter); invalid statement in fill window
you can't close database connection if want pull data cursor.
spinner not view can bounds simplecursoradapter
the to int array represents ids of views row layout file pass simplecursoradapter bind data from array (r.layout.main, hope have layout file , pass activity's layout). id r.id.classdropdown used there represents spinner view , simplecursoradapter doesn't know how bind data that(it can bind data textview or imageview). if want spinner on each row(do want this? or want default spinner row layout?) use custom adapter or simplecursoradapter.viewbinder.
column '_id' not exist
because don't have column name in table classtable. column should declared like:
integer primary key autoincrement maybe can check this simple tutorial spinners.
Comments
Post a Comment