简体   繁体   English

从微调器设置项目的值

[英]Setting value to item from Spinner

So i created a Spinner and set 5 values in it. 所以我创建了一个微调器,并在其中设置了5个值。 I'm building a calculator and want to every item from Spinner have some value (they have text value, but I want them to have number value). 我正在构建一个计算器,并且希望Spinner中的每个项目都具有一定的价值(它们具有文本价值,但我希望它们具有数字价值)。 Before that, user has to enter his TDEE, don't bother with that, it's just a number, and that number then multiplys with value of selected spinner item. 在此之前,用户必须输入其TDEE,而不必理会,它只是一个数字,然后该数字与所选微调项的值相乘。 That all happends when the button is clicked, and then the value prints down in text view. 当单击按钮时,所有这些都会发生,然后在文本视图中打印出该值。

The problem is, I don't have idea how to pass values to spinner items. 问题是,我不知道如何将值传递给微调器项目。

Here's the 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 i fizički posao 2 x dnevno)</item>

    </string-array>

XML: 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: 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 WHERE 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 the getSelectedItem() from the Spinner : 使用Spinner中getSelectedItem()

spinner.getSelectedItem().toString()

So, in your code replace your btnIzracunaj listener with: 因此,在您的代码中,将btnIzracunaj侦听器替换为:

btnIzracunaj.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
      Toast.makeText(MainActivity.this, spinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
  }
});

You are going to have to parse what you have on your strings.xml . 您将必须解析在strings.xml中的内容 I would recommend you to set them as numbers just to 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 your strings.xml set with only floating numbers, your new onClickListener would look like: 在将strings.xml设置为仅包含浮点数的情况下,新的onClickListener如下所示:

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));
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM