我有一个动态Textview,根据微调器添加一个数字。 (ID:Textview3)
我有一个允许整数输入的EditText。 (ID:EDITTEXT)
我想将这两个值一起添加并显示到TextView中。(id:textView4)
上下文:这是一个食物应用程序,根据旋转器的选择添加一个食物项目,然后根据估算的食物重量计算总卡路里。
欢迎所有建议,提前致谢!
public class AddActivity extends Activity implements OnClickListener, OnItemSelectedListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
spinner1 = (Spinner) this.findViewById(R.id.spinner01);
spinner2 = (Spinner) this.findViewById(R.id.spinner02);
chooseCategory();
chooseBreakfast();
chooseLunch();
chooseDinner();
chooseSnack();
}
static Spinner spinner1, spinner2 = null;
public void chooseCategory() {
ArrayAdapter<CharSequence> catAdapter = ArrayAdapter.createFromResource(this, R.array.foodtype, android.R.layout.simple_spinner_item);
catAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(catAdapter);
spinner1.setOnItemSelectedListener(new planOnSelectedListener());
}
public void chooseBreakfast() {
ArrayAdapter<Food> planAdapter1 = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, new Food[]{
new Food(1, "Toast"),
new Food(99, "Cereal"),
new Food(53, "Fruit"),
new Food(153, "Yogurt")
});
spinner2.setAdapter(planAdapter1);
spinner2.setOnItemSelectedListener(this);
Button button = (Button) this.findViewById(R.id.button01);
button.setOnClickListener(this);
}
public void chooseLunch() {
ArrayAdapter<Food> planAdapter2 = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, new Food[]{
new Food(1, "Sandwich"),
new Food(99, "Wrap"),
new Food(53, "Salad"),
});
spinner2.setAdapter(planAdapter2);
spinner2.setOnItemSelectedListener(this);
Button button = (Button) this.findViewById(R.id.button01);
button.setOnClickListener(this);
}
public void chooseDinner() {
ArrayAdapter<Food> planAdapter2 = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, new Food[]{
new Food(1, "Pizza"),
new Food(99, "Pasta"),
new Food(53, "Fish and Chips"),
});
spinner2.setAdapter(planAdapter2);
spinner2.setOnItemSelectedListener(this);
Button button = (Button) this.findViewById(R.id.button01);
button.setOnClickListener(this);
}
public void chooseSnack() {
ArrayAdapter<Food> planAdapter2 = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, new Food[]{
new Food(1, "Crisps"),
new Food(99, "Chocolate"),
new Food(53, "Donut"),
});
spinner2.setAdapter(planAdapter2);
spinner2.setOnItemSelectedListener(this);
Button button = (Button) this.findViewById(R.id.button01);
button.setOnClickListener(this);
}
public void toastState(String prefix, Food cl) {
if (cl != null) {
String desc = "Event: " + prefix + "\nName: " + cl.name;
desc += "\nCalorie: " + String.valueOf(cl.calories);
Toast.makeText(getApplicationContext(), desc, Toast.LENGTH_SHORT).show();
}
}
public void calorieState(Food cl) {
TextView textView3 = (TextView) findViewById(R.id.textView3);
textView3.setText(String.valueOf("Item Calories: " + cl.getCalorie()));
}
public void nameState (Food cl) {
TextView textView6 = (TextView) findViewById(R.id.textView6);
textView6.setText(String.valueOf("Item Name: " + cl.getName()));
}
public class planOnSelectedListener 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
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Get the currently selected State object from the spinner
Food st = (Food) spinner2.getSelectedItem();
// Show it via a toast
toastState("onItemSelected", st);
calorieState(st);
nameState(st);
}
public void onNothingSelected(AdapterView<?> parent) {
}
public void onClick(View v) {
// Get the currently selected State object from the spinner
Food st = (Food) spinner2.getSelectedItem();
toastState("onClick", st);
}
}