android - spinner initialization in a custom view -
i try make custom view i've got problem spinner. when launch application ok. preferences read , spinner setselection
method work great (display picture ok). when change spinner value, there no problem : preferences saved , when start application new choice display.
my problem comes when rotating device, spinner value reset last spinner value (vv2 in code). don't understand...
here code :
the widget_value_view.xml
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" > <spinner android:id="@+id/my_spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:background="@android:color/transparent" /> <textview android:id="@+id/my_value_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="15dp" android:layout_centervertical="true" android:background="@android:color/transparent" /> </merge>
the valueview.java class :
package com.example.spinnertest; import java.util.arrays; import java.util.list; import android.content.context; import android.content.res.typedarray; import android.util.attributeset; import android.view.gravity; import android.view.layoutinflater; import android.widget.linearlayout; import android.widget.spinner; import android.widget.textview; public class valueview extends linearlayout { protected textview mvaluetv = null; protected spinner mspinner = null; protected list<string> mchoicelist; private myspinneradapter mspinneradapter; public valueview(context context) { super(context); } public valueview(context context, attributeset attributes){ super(context,attributes); typedarray = context.gettheme().obtainstyledattributes(attributes,r.styleable.valueview,0, 0); try { string valuelist = a.getstring(r.styleable.valueview_valuelist); mchoicelist = arrays.aslist(valuelist.split("\\|")); } { a.recycle(); } setorientation(linearlayout.horizontal); setgravity(gravity.center_vertical); layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); inflater.inflate(r.layout.widget_value_view, this, true); mspinner = (spinner) getchildat(0); mvaluetv = (textview) getchildat(1); mspinneradapter = new myspinneradapter(context, mchoicelist); mspinner.setadapter(mspinneradapter); } /** * change textview value */ public void setvalue(double value,string format){ mvaluetv.settext("todo"); invalidate(); requestlayout(); } /** * set current item of spinner * @param position of item */ public void setselection(int position, boolean animate) { // mspinneradapter.notifydatasetchanged(); mspinner.setselection(position,animate); // invalidate(); // requestlayout(); } public int getselecteditemposition() { return mspinner.getselecteditemposition(); } }
in class use adapter define 2 layout , 1 class : myspinneradapter.java
package com.example.spinnertest; import java.util.list; import android.content.context; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; public class myspinneradapter extends baseadapter { public static final string item_altitude = "altitude"; public static final string item_speed = "speed"; public static final string item_distance = "distance"; private static final string log_tag = "spinner_test"; private context mcontext; private list<string> mchoicelist; public myspinneradapter(context context, list<string> values) { this.mcontext = context; this.mchoicelist = values; } @override public int getcount() { return mchoicelist.size(); } @override public object getitem(int position) { return mchoicelist.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) { layoutinflater layout = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service); convertview = layout.inflate(r.layout.item_spinner_value, null); } imageview imview = (imageview) convertview.findviewbyid(r.id.item_spinner_value_iv); log.d(log_tag,"spinner adapter position = " + string.valueof(position)); imview.setimageresource(getspinneritemimage(position)); return convertview; } @override public view getdropdownview(int position, view convertview, viewgroup parent) { if (convertview == null) { layoutinflater layout = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service); convertview = layout.inflate(r.layout.item_spinner_list, null); } imageview imview = (imageview) convertview.findviewbyid(r.id.item_spinner_list_iv); textview textview = (textview) convertview.findviewbyid(r.id.item_spinner_list_tv); textview.settext(mchoicelist.get(position)); imview.setimageresource(getspinneritemimagesmall(position)); return convertview; } private int getspinneritemimagesmall(int position) { string item = mchoicelist.get(position); if (item.equals(item_altitude)) { return r.drawable.altitude_small; } else if (item.equals(item_distance)) { return r.drawable.distance_small; } else if (item.equals(item_speed)) { return r.drawable.vitesse_small; } else { return r.drawable.date_small; } } private int getspinneritemimage(int position) { string item = mchoicelist.get(position); if (item.equals(item_altitude)) { return r.drawable.altitude; } else if (item.equals(item_distance)) { return r.drawable.distance; } else if (item.equals(item_speed)) { return r.drawable.vitesse; } else { return r.drawable.date; } } }
and 2 layout item_spinner_list.xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <imageview android:id="@+id/item_spinner_list_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:padding="5dp" android:src="@android:drawable/ic_menu_gallery" /> <textview android:id="@+id/item_spinner_list_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:padding="5dp" android:layout_torightof="@+id/item_spinner_list_iv" android:text="durée" /> </relativelayout>
and item_spinner_value.xml :
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" > <imageview android:id="@+id/item_spinner_value_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:padding="5dp" android:src="@android:drawable/ic_menu_gallery" /> </relativelayout>
and mainactivity.java
package com.example.spinnertest; import android.app.activity; import android.content.sharedpreferences; import android.os.bundle; import android.preference.preferencemanager; public class mainactivity extends activity { private sharedpreferences mpref; private valueview mvv1; private valueview mvv2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mpref = preferencemanager.getdefaultsharedpreferences(getapplicationcontext()); mvv1 = (valueview)findviewbyid(r.id.value1); mvv2 = (valueview)findviewbyid(r.id.value2); mvv1.setselection(mpref.getint("pref_val1", 0),false); mvv2.setselection(mpref.getint("pref_val2", 1),false); } @override public void onpause() { sharedpreferences.editor editor = mpref.edit(); editor.putint("pref_val1", mvv1.getselecteditemposition()); editor.putint("pref_val2", mvv2.getselecteditemposition()); editor.commit(); super.onpause(); } }
and layout :
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.example.spinnertest" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.spinnertest.valueview android:id="@+id/value1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_margin="10dp" android:background="@android:color/transparent" custom:valuelist="speed|altitude|distance" /> <com.example.spinnertest.valueview android:id="@+id/value2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/value1" android:layout_margin="10dp" custom:valuelist="speed|altitude|distance" /> </relativelayout>
i cannot explain why code not working, prefer save state of spinner locally (i mean in activity); it's normal android workflow screen rotation.
something this:
string mvv1="1"; string mvv2="2"; @override protected void oncreate(bundle savedinstancestate) { setcontentview(r.layout.activity_main); mpref = preferencemanager.getdefaultsharedpreferences(getapplicationcontext()); mvv1 = (valueview)findviewbyid(r.id.value1); mvv2 = (valueview)findviewbyid(r.id.value2); if (savedinstancestate != null) { mvv1.setselection(savedinstancestate.getint(mvv1),false); mvv2.setselection(savedinstancestate.getint(mvv2),false); } else{ mvv1.setselection(mpref.getint("pref_val1", 0),false); mvv2.setselection(mpref.getint("pref_val2", 1),false); } } @override public void onpause() { sharedpreferences.editor editor = mpref.edit(); editor.putint("pref_val1", mvv1.getselecteditemposition()); editor.putint("pref_val2", mvv2.getselecteditemposition()); editor.commit(); super.onpause(); } @override protected void onsaveinstancestate (bundle outstate) { super.onsaveinstancestate(outstate); outstate.putint(mvv1, mvv1.getselecteditemposition()); outstate.putint(mvv2, mvv2.getselecteditemposition()); }
my syntax maybe not perfect because wrote in editor hope got idea: status written in preferences activity paused if activity rotates (your method should work maybe there cache access preferences or something...) then, it'll use specific bundle it: 1 filled when screen rotates , used when screen recreated.
test , if it's working ;)
Comments
Post a Comment