android - Some help understanding columnIndex in ViewBInder -
skip bottom if want see question without context
the android app i'm building has simple table 3 columns:
_id integer primary key..., name text, color int
this table called categories
. load categories database , feed them simplecursoradapter
use spinner
so:
string[] = new string[] { listdbadapter.key_category_name, listdbadapter.key_category_color }; int[] = new int[] { r.id.categoryspinneritem }; mcategoryspinneradapter = new simplecursoradapter(this, r.layout.category_spinner_item, categorycursor, from, to); mcategoryspinneradapter .setviewbinder(new categoryspinnerviewbinder()); mcategoryspinner.setadapter(mcategoryspinneradapter);
i set custom viewbinder
because want category name text of spinner item, , color background color. viewbinder
looks this:
private static final int name_column = 1; private static final int color_column = 2; @override public boolean setviewvalue(view view, cursor cursor, int columnindex) { textview textview = (textview) view; string name = cursor.getstring(name_column); int color = cursor.getint(color_column); textview.settext(name); textview.setbackgroundcolor(color); return true; }
here question (finally)
in setviewvalue method, columnindex supposed doing? documentation says "the column @ data can found in cursor" when debug through setviewvalue hit 3 times , columnindex 1
.
i expecting debugger setviewvalue once each entry in array, columnindex first of 1
, 2
. or maybe once each column in query results.
the above code works, can desired functionality because of name_column , color_column constants. i'd interested hear explanation of setviewvalue , parameters more experienced custom viewbinders.
in source of simplecursoradapter, setviewvalue
called in bindview
:
bound = binder.setviewvalue(v, cursor, from[i]);
where third param from[i], interesting one, iteration on int[], represents column indexes used. index [i] iteration comes int[] to passed constructor of adapter, , in case, has 1 item - r.id.categoryspinneritem
edit: in 2 words, string[] , int[] should equivalent, same size , in same order - each column name need int r.id... first view id connected first column id from[0], second from[1] , on, , if pass 10 columns, have 3 r.id-s, from[2] :)
Comments
Post a Comment