android - Spinner images are not showing -
folks.
i'm trying implement dynamic images custom spinner in android. went excpet images not show condition, it's appears default image in layout. can point me mistake? hereunder code.
public class customspinneradapter extends simplecursoradapter { int layoutn; layoutinflater minflater; private final cursor mcursor; private final int mlayout; private final layoutinflater mlayoutinflater; private final context mcontext; int arr_images[] = { r.drawable.us, r.drawable.uk, r.drawable.eur, r.drawable.cn, r.drawable.ml, r.drawable.mr}; public customspinneradapter (context context, int layout, cursor c, string[] from, int[] to) { super(context, r.layout.spinnertext, c, from, to); this.mcontext = context; this.mcursor = c; this.mlayout = layout; this.mlayoutinflater = layoutinflater.from(context); } private final class viewholder { public textview title; public imageview flag; } @override public view getview(int position, view convertview, viewgroup parent) { final viewholder viewholder; if (convertview == null) { convertview = mlayoutinflater.inflate(mlayout, parent, false); viewholder = new viewholder(); viewholder.title = (textview) convertview .findviewbyid(r.id.currencytitle); viewholder.flag = (imageview) convertview .findviewbyid(r.id.imageview1); } else { viewholder = (viewholder) convertview.gettag(); } viewholder.flag.setimageresource(arr_images[position]); return convertview; }
}
edit : listener code , cursor :
mdb = mhelper.getwritabledatabase();
string[] headers2 = new string[] {mydbhelper.col_currfirst ,mydbhelper.col_titleone};
cursl = mdb.query(mydbhelper.table_name, headers2, mydbhelper.col_currsecond + "=" + "?", new string[] { "eur" }, null, null, null ); adapters1 = new customspinneradapter(this, r.layout.spinnertext, cursl, headers2, new int[] { r.id.currencytitle , r.id.titlesub}); fromc.setadapter(adapters1); toc.setadapter(adapters1);
row.xml in layout folder
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="3dip" > <imageview android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:layout_torightof="@+id/image" android:padding="3dip" android:layout_margintop="2dip" android:textstyle="bold" android:textcolor="#000000" android:id="@+id/company" android:text="coderzheaven" android:layout_marginleft="5dip" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </relativelayout>
then make spinners in .xml layout , in activity use populate them\
public class myadapter extends arrayadapter<string>{ public myadapter(context context, int textviewresourceid, string[] objects) { super(context, textviewresourceid, objects); } @override public view getdropdownview(int position, view convertview,viewgroup parent) { return getcustomview(position, convertview, parent); } @override public view getview(int position, view convertview, viewgroup parent) { return getcustomview(position, convertview, parent); } int arr_images[] = { r.drawable.us, r.drawable.uk, r.drawable.eur, r.drawable.cn, r.drawable.ml, r.drawable.mr}; public view getcustomview(int position, view convertview, viewgroup parent) { layoutinflater inflater=getlayoutinflater(); view row=inflater.inflate(r.layout.row, parent, false); textview label=(textview)row.findviewbyid(r.id.company); label.settext(strings[position]); imageview icon=(imageview)row.findviewbyid(r.id.image); icon.setimageresource(arr_images[position]); return row; } }
in oncreate() use this
spinner spin; spin = (spinner)findviewbyid(r.id.iconspin);//or whatever id of spinner string[] strings = new string[] {mydbhelper.col_currfirst ,mydbhelper.col_titleone}; spin.setadapter(new myadapter(mainactivity.this, r.layout.row, strings)); spin.setonitemselectedlistener(this);
and make activity
implement onitemselectedlistener
then can override method
@override public void onitemselected(adapterview<?> arg0, view arg1, int arg2, long arg3) { }
to deal selection of spinner
Comments
Post a Comment