我有几个旋转器的活动,展示了不同的产品。目前,我的微调器显示一条 toast 消息,显示从列表中选择的项目。但是,我想扩展功能,以便显示与产品关联的值。
例如:用户选择“apple”,Toast 显示“130kcal”。在活动结束时,将有一个按钮添加所有金额并将它们存储在不同的意图中以供以后查看。
我知道我必须以某种方式将值存储在与微调器项目 ID 关联的变量中(也许?),然后基于此调用 toast,但我不确定如何。
任何帮助表示赞赏,谢谢。
代码:
package com.example.c3347115app;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MyMeals extends Activity {
Spinner sp;
public int apple = 130;
public int toast1 = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_meals);
sp = (Spinner) findViewById(R.id.spinner1);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(getBaseContext(), sp.getSelectedItem().toString(),
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
//Do nothing
}
});
}
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyMeals" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/my_meals" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/breakfast" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="@array/breakfast_array"
android:prompt="@string/breakfast_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/snack"/>
<Spinner
android:id="@+id/spinner2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="@array/breakfast_array"
android:prompt="@string/breakfast_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lunch" />
<Spinner
android:id="@+id/spinner3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="@array/breakfast_array"
android:prompt="@string/breakfast_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/snack1"/>
<Spinner
android:id="@+id/spinner4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="@array/breakfast_array"
android:prompt="@string/breakfast_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dinner"/>
<Spinner
android:id="@+id/spinner5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="@array/breakfast_array"
android:prompt="@string/breakfast_title" />"
</LinearLayout>
字符串.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Eat Right</string>
<string name="action_settings">Settings</string>
<string name="title_activity_mot_d">MotD</string>
<string name="title_activity_meal_plans">MealPlans</string>
<string name="title_activity_your_meals">YourMeals</string>
<string name="title_activity_progress">Progress</string>
<string name="motd">Meal of the Day</string>
<string name="my_meals">My Meals</string>
<string name="meal_plans">Meal Plans</string>
<string name="my_progress">My Progress</string>
<string name="breakfast">Breakfast</string>
<string name="breakfast_title">Choose Breakfast</string>
<string name="snack">Snack</string>
<string name="snack_title">Choose Snack</string>
<string name="lunch">Lunch</string>
<string name="lunch_title">Choose Lunch</string>
<string name="snack1">Snack</string>
<string name="snack1_title">Choose Second Snack</string>
<string name="dinner">Dinner</string>
<string name="dinner_title">Choose Dinner</string>
<string-array name="breakfast_array">
<item>Cereal</item>
<item>1 slice toast</item>
<item>2 slice toast</item>
<item>Eggs and bacon</item>
<item>Eggs and sausage</item>
<item>Eggs, sausage and bacon</item>
<item>Yoghurt</item>
<item>Apple</item>
</string-array>
</resources>