java - Setting value to item from Spinner -
so created spinner , set 5 values in it. i'm building calculator , want every item spinner have value (they have text value, want them have number value). before that, user has enter tdee, don't bother that, it's number, , number multiplys value of selected spinner item. happends when button clicked, , value prints down in text view.
the problem is, don't have idea how pass values spinner items.
here's code:
strings:
<string-array name="razina_aktivnosti"> <item>sjedeći (malo ili bez aktivnosti, pretežno sjedeći posao)</item> <item>laka aktivnosti (lake vježbe/sport 1-3 dana tjedno)</item> <item>umjeren (umjerene vježbe/sport 3-5 dana tjedno)</item> <item>teške aktivnosti (teže vježbe/sport 6-7 dana tjedno)</item> <item>ekstremne aktivnosti (teške dnevne vježbe/sport fizički posao 2 x dnevno)</item> </string-array>
xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="hr.app.liftme.liftmehr.kalkulatoritdee" tools:showin="@layout/activity_kalkulatori_tdee"> <linearlayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearlayout22" android:layout_marginbottom="10dp"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="unesite svoj bmr:" android:id="@+id/textview115" /> <edittext android:layout_width="match_parent" android:layout_height="wrap_content" android:inputtype="numberdecimal" android:ems="10" android:id="@+id/editbmrinput" /> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/linearlayout22" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:id="@+id/linearlayout23" android:layout_marginbottom="10dp"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="odaberite razinu aktivnosti" android:id="@+id/aktivnost" android:layout_below="@+id/linearlayout22" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <spinner android:id="@+id/spinnertdee" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/aktivnost" android:layout_alignparentleft="true"> </spinner> </linearlayout> <linearlayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/linearlayout23"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="izračunaj" android:id="@+id/btnizracunajtdee" android:layout_gravity="center_horizontal" android:layout_marginbottom="10dp" android:background="#424242" android:textcolor="#ffffff" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="vaš tdee je:" android:id="@+id/textview116" android:layout_gravity="center_horizontal" android:layout_marginbottom="5dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:id="@+id/textviewrezultattdee" android:layout_gravity="center_horizontal" /> </linearlayout> </relativelayout>
java:
package hr.app.liftme.liftmehr; import android.content.intent; import android.os.bundle; import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.spinner; import android.widget.textview; import android.widget.toast; public class kalkulatoritdee extends appcompatactivity { spinner spinner; arrayadapter<charsequence> adapter; button btnizracunaj; edittext editbmrinput; textview textviewrezultat; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_kalkulatori_tdee); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true); intent intent = getintent(); spinner = (spinner) findviewbyid(r.id.spinnertdee); adapter = arrayadapter.createfromresource(this, r.array.razina_aktivnosti, android.r.layout.simple_spinner_item); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner.setadapter(adapter); btnizracunaj = (button) findviewbyid(r.id.btnizracunajtdee); editbmrinput = (edittext) findviewbyid(r.id.editbmrinput); textviewrezultat = (textview) findviewbyid(r.id.textviewrezultattdee); btnizracunaj.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v){ double prvopolje = double.parsedouble(editbmrinput.gettext().tostring()); // here's i've stucked!!! double rezultat = 0; btnizracunaj.settext(double.tostring(rezultat)); } }); spinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { toast.maketext(getbasecontext(), parent.getitemidatposition(position)+" selected", toast.length_long).show(); } @override public void onnothingselected(adapterview<?> parent) { } }); } }
any ideas?
use getselecteditem()
spinner:
spinner.getselecteditem().tostring()
so, in code replace btnizracunaj
listener with:
btnizracunaj.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(mainactivity.this, spinner.getselecteditem().tostring(), toast.length_short).show(); } });
you going have parse have on strings.xml. recommend set them numbers test:
<string-array name="razina_aktivnosti"> <item>1.1</item> <item>2.2</item> <item>3.3</item> <item>4.4</item> <item>5.5</item> </string-array>
edit:
with strings.xml set floating numbers, new onclicklistener like:
btnizracunaj.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { double prvopolje = double.parsedouble(editbmrinput.gettext().tostring()); double rezultat = 0; toast.maketext(mainactivity.this, spinner.getselecteditem().tostring(), toast.length_short).show(); rezultat = double.valueof(spinner.getselecteditem().tostring()); btnizracunaj.settext(string.valueof(rezultat * prvopolje)); } });
Comments
Post a Comment