0
我有一個活動與幾個紡紗器,顯示不同的產品。此刻,我的紡紗廠顯示吐司消息,顯示從列表中選擇的項目。但是,我想擴展功能,以便顯示與產品相關的值。從Spinner發送集體數據到不同的活動
例如:用戶選擇「apple」,Toast顯示「130kcal」。在活動結束時,會出現一個按鈕,將所有金額添加並將其存儲在不同的意圖中供以後查看。
我得到我必須以某種方式存儲與微調項目ID(可能?)相關聯的變量中的值,然後基於此調用敬酒,但我不知道如何。
任何幫助表示讚賞,謝謝。
代碼:
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>
的strings.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>
我不確定我是否理解你。你能否更詳細地澄清一下? – Ciaran 2013-05-06 12:17:18
已應用onItemSelected上spinner.their你可以保存設置值或在每個微調框的getSelectedItem和總結任何變種或提供給我你的班級我會演示你如何做.. – Manmohan 2013-05-06 12:22:50
我已經在代碼中添加現在,如果你能幫助我通過,這將是巨大的:) – Ciaran 2013-05-06 12:34:06