android - How to make a Wheel picker -
i trying make wheel picker this have tried downloading project files included in .zip download wheel-demo.apk , notes.txt. notes.txt not have instructions on how use file android studio. found post suggested using listviews create same effect. rather making own spent day searching internet , found source code here when imported files project ide showed dozens of errors. through trial , error managed fix 3 errors. pretty sure have put relevant code below
mainactivity.java:
onwheelscrolllistener scrolledlistener = new onwheelscrolllistener() { public void onscrollingstarted(wheelview wheel) { wheelscrolled = true;// "cannot resolve symbol wheelscrolled } public void onscrollingfinished(wheelview wheel) { wheelscrolled = false;// "cannot resolve symbol wheelscrolled updatestatus(); } }; // wheel changed listener private final onwheelchangedlistener changedlistener = new onwheelchangedlistener() { public void onchanged(wheelview wheel, int oldvalue, int newvalue) { if (!wheelscrolled)// "cannot resolve symbol wheelscrolled { updatestatus(); } } }; private void initwheel1(int id) { wheelview wheel = (wheelview) findviewbyid(id); wheel.setadapter(new arraywheeladapter<string>(wheelmenu1)); //cannot resolve method 'setadapter(com.projectname.arraywheeladapter<java.lang.string>) wheel.setvisibleitems(2); wheel.setcurrentitem(0); wheel.addchanginglistener(changedlistener); wheel.addscrollinglistener(scrolledlistener); }
onwheelscrolllistener.java:
public interface onwheelscrolllistener { /** * callback method invoked when scrolling started. * @param wheel wheel view state has changed. */ void onscrollingstarted(wheelview wheel); /** * callback method invoked when scrolling ended. * @param wheel wheel view state has changed. */ void onscrollingfinished(wheelview wheel);}
onwheelchangedlistener.java:
public interface onwheelchangedlistener { /** * callback method invoked when current item changed * @param wheel wheel view state has changed * @param oldvalue old value of current item * @param newvalue new value of current item */ void onchanged(wheelview wheel, int oldvalue, int newvalue); }
arraywheeladapter.java
public class arraywheeladapter<t> extends abstractwheeltextadapter { // items private t items[]; /** * constructor * @param context current context * @param items items */ public arraywheeladapter(context context, t items[]) { super(context); //setemptyitemresource(text_view_item_resource); this.items = items; } @override public charsequence getitemtext(int index) { if (index >= 0 && index < items.length) { t item = items[index]; if (item instanceof charsequence) { return (charsequence) item; } return item.tostring(); } return null; } @override public int getitemscount() { return items.length; } }
all 3 of .java files have been added import list in mainactivity thinking might solve issue, didn't. thank advice far.
try this
public class mainactivity extends activity { // todo: externalize string-array string wheelmenu1[] = new string[]{"name 1", "name 2", "name 3", "name 4", "name 5", "name 6","name 7","name 8","name 9"}; string wheelmenu2[] = new string[]{"age 1", "age 2", "age 3"}; string wheelmenu3[] = new string[]{"10", "20","30","40","50","60"}; // wheel scrolled flag private boolean wheelscrolled = false; private textview text; private edittext text1; private edittext text2; private edittext text3; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); initwheel1(r.id.p1); initwheel2(r.id.p2); initwheel3(r.id.p3); text1 = (edittext) this.findviewbyid(r.id.r1); text2 = (edittext) this.findviewbyid(r.id.r2); text3 = (edittext) this.findviewbyid(r.id.r3); text = (textview) this.findviewbyid(r.id.result); } // wheel scrolled listener onwheelscrolllistener scrolledlistener = new onwheelscrolllistener() { public void onscrollstarts(wheelview wheel) { wheelscrolled = true; } public void onscrollends(wheelview wheel) { wheelscrolled = false; updatestatus(); } }; // wheel changed listener private final onwheelchangedlistener changedlistener = new onwheelchangedlistener() { public void onchanged(wheelview wheel, int oldvalue, int newvalue) { if (!wheelscrolled) { updatestatus(); } } }; /** * updates entered pin status */ private void updatestatus() { text1.settext(wheelmenu1[getwheel(r.id.p1).getcurrentitem()]); text2.settext(wheelmenu2[getwheel(r.id.p2).getcurrentitem()]); text3.settext(wheelmenu3[getwheel(r.id.p3).getcurrentitem()]); text.settext(wheelmenu1[getwheel(r.id.p1).getcurrentitem()] + " - " + wheelmenu2[getwheel(r.id.p2).getcurrentitem()] + " - " + wheelmenu3[getwheel(r.id.p3).getcurrentitem()]); } /** * initializes wheel * * @param id * wheel widget id */ private void initwheel1(int id) { wheelview wheel = (wheelview) findviewbyid(id); wheel.setadapter(new arraywheeladapter<string>(wheelmenu1)); wheel.setvisibleitems(2); wheel.setcurrentitem(0); wheel.addchanginglistener(changedlistener); wheel.addscrollinglistener(scrolledlistener); } private void initwheel2(int id) { wheelview wheel = (wheelview) findviewbyid(id); wheel.setadapter(new arraywheeladapter<string>(wheelmenu2)); wheel.setvisibleitems(2); wheel.setcurrentitem(0); wheel.addchanginglistener(changedlistener); wheel.addscrollinglistener(scrolledlistener); } private void initwheel3(int id) { wheelview wheel = (wheelview) findviewbyid(id); wheel.setadapter(new arraywheeladapter<string>(wheelmenu3)); wheel.setvisibleitems(2); wheel.setcurrentitem(0); wheel.addchanginglistener(changedlistener); wheel.addscrollinglistener(scrolledlistener); } /** * returns wheel id * * @param id * wheel id * @return wheel passed id */ private wheelview getwheel(int id) { return (wheelview) findviewbyid(id); } /** * tests wheel value * * @param id * wheel id * @param value * value test * @return true if wheel value equal passed value */ private int getwheelvalue(int id) { return getwheel(id).getcurrentitem(); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@drawable/layout_bg"> <linearlayout android:layout_margintop="24dp" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <com.example.wheel.wheelview android:id="@+id/p1" android:layout_height="wrap_content" android:layout_width="wrap_content" /> <com.example.wheel.wheelview android:id="@+id/p2" android:layout_height="wrap_content" android:layout_width="wrap_content" /> <com.example.wheel.wheelview android:id="@+id/p3" android:layout_height="wrap_content" android:layout_width="wrap_content" /> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="24dp"> <edittext android:id="@+id/r1" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_margintop="24dp" android:layout_gravity="center_horizontal" android:textsize="18sp" android:textcolor="#000"> </edittext> <edittext android:id="@+id/r2" android:layout_width="80dip" android:layout_height="wrap_content" android:layout_margintop="24dp" android:layout_gravity="center_horizontal" android:textsize="18sp" android:textcolor="#000"> </edittext> <edittext android:id="@+id/r3" android:layout_width="80dip" android:layout_height="wrap_content" android:layout_margintop="24dp" android:layout_gravity="center_horizontal" android:textsize="18sp" android:textcolor="#000"> </edittext> </linearlayout> <textview android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="24dp" android:layout_gravity="center_horizontal" android:textsize="18sp" android:textcolor="#fff" android:text="your choice"> </textview> </linearlayout>
also try demo
Comments
Post a Comment