我创建了两个微调器,用户可以在其中选择一种膳食类型,然后选择一种食物(例如,早餐'粥和#39;)。然后,我需要根据用户在编辑文本中输入的重量来计算食物卡路里,但我不知道我是如何做到这一点的。
如何将我的微调器选项与数字相关联?
public class MainActivity3 extends ActionBarActivity {
EditText num1;
EditText num2;
EditText num3;
EditText num4;
EditText num5;
Button btnSave;
EditText calorieResult;
public Spinner catspinner, planspinner3, planspinner1, planspinner2;
protected void onCreate(Bundle mbfSplash) {
super.onCreate(mbfSplash);
setContentView(R.layout.addmeal);
chooseCategory();
chooseBreakfast();
chooseLunch();
chooseDinner();
chooseSnack();
}
public void chooseCategory() {
catspinner = (Spinner) findViewById(R.id.catspinner);
ArrayAdapter<CharSequence> catAdapter = ArrayAdapter.createFromResource(this, R.array.ducategory, android.R.layout.simple_spinner_item);
catAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catspinner.setAdapter(catAdapter);
catspinner.setOnItemSelectedListener(new planOnClickListener());
}
private void chooseBreakfast() {
planspinner1 = (Spinner) findViewById(R.id.planspinner);
List<String> planlist = new ArrayList<String>();
planlist.add("Porridge-40g/140cal");
planlist.add("Coffee-10g/10cal");
planlist.add("Toast-30g/87cal");
ArrayAdapter<String> planAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, planlist);
planAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
planspinner1.setAdapter(planAdapter1);
}
private void chooseLunch() {
planspinner2 = (Spinner) findViewById(R.id.planspinner);
List<String> planlist1 = new ArrayList<String>();
planlist1.add("Salad-50g/40cal");
planlist1.add("Sandwich-50g/200cal");
ArrayAdapter<String> planAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, planlist1);
planAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
planspinner2.setAdapter(planAdapter2);
}
private void chooseDinner() {
planspinner2 = (Spinner) findViewById(R.id.planspinner);
List<String> planlist2 = new ArrayList<String>();
planlist2.add("Curry-100g/400cal");
planlist2.add("Pasta-40g/160cal");
planlist2.add("Pizza100g/220cal");
ArrayAdapter<String> planAdapter3 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, planlist2);
planAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
planspinner2.setAdapter(planAdapter3);
}
private void chooseSnack() {
planspinner1 = (Spinner) findViewById(R.id.planspinner);
List<String> planlist = new ArrayList<String>();
planlist.add("Crisps-30g/70cal");
planlist.add("Chocolate-100g/210cal");
ArrayAdapter<String> planAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, planlist);
planAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
planspinner1.setAdapter(planAdapter1);
}
public class planOnClickListener implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View v, int pos,
long id) {
parent.getItemAtPosition(pos);
if (pos == 0) {
chooseBreakfast();
} else if (pos == 1) {
chooseLunch();
} else if (pos == 2) {
chooseDinner();
} else if (pos == 3) {
chooseSnack();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
}
答案 0 :(得分:0)
我没有代码编辑器这样做,所以请原谅语法错误,但一般情况下你应该使用一个对象来存储相关的数据。为您的微调器使用自定义适配器。这里有一些代码可以帮助您入门。
您应该创建一个名为Food的对象。
public class Food {
int mCalories;
String mName;
public Food(String name, int calories) {
mName= name;
mCalories = calories;
}
}
现在您可以为您的微调器使用自定义数组适配器。
public class FoodArrayAdapter extends ArrayAdapter<Food> {
private List<Food> mItems;
private Context mContext;
private int mResource;
public FoodArrayAdapter(Context ctx, int resource, List<Food> items) {
super(activity, resource);
mItems = items;
mContext = ctx;
mResource = resource;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
//You could use a view holder pattern here, won't really matter for a simple text spinner.
Food item = mItems.get(position);
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(mResource, parent);
TextView textView = (TextView) v.findViewById(android.R.id.text1);
if (item != null) {
textView.setText(food.getName());
}
return v;
}
@Override
public Food getItem(int position) {
return items.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getDropDownView(int position, View convertView, ViewGroup parent);
//or make another view for when the spinner is expanded
}
@Override
public int getCount() {
return mItems.getSize();
}
}
现在只需将自定义适配器与主要活动中的微调器相关联。
Spinner spinner = (Spinner) layout.findViewById(R.id.myspinner1);
spinner.setAdapter(new FoodAdapter(this, android.R.layout.simple_spinner_item, list);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
Food food = (Food) parent.getItemAtPosition(pos);
//Do whatever you need to do with selected value. You have reference to both the food and the calories.
}
public void onNothingSelected( AdapterView<?> parent){
}
}
);