从微调器创建计算器
Posted
技术标签:
【中文标题】从微调器创建计算器【英文标题】:Create a calculator from a spinner 【发布时间】:2015-04-07 15:41:33 【问题描述】:我创建了两个微调器,用户可以在其中选择膳食类型,然后选择食物(例如“早餐”和“粥”)。然后,我需要将食物卡路里乘以用户在编辑文本中输入的重量,但我不确定我是如何做到这一点的。
如何将我的微调器选项与数字相关联?
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
【问题讨论】:
阅读面向对象编程 让我正确理解这一点。有两个Spinner
s。一个显示早餐类型,另一个显示食物。然后有一个EditText
,用户在其中输入重量。你必须将与食物相关的卡路里乘以重量。这是正确的吗?
是的,没错。
【参考方案1】:
我在没有代码编辑器的情况下执行此操作,因此请原谅语法错误,但通常您应该使用一个对象来存储相关数据。为您的微调器使用自定义适配器。这是一些帮助您入门的代码。
您应该创建一个名为 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)
);
【讨论】:
你给了我一个很好的开始。谢谢! :)以上是关于从微调器创建计算器的主要内容,如果未能解决你的问题,请参考以下文章