我有计算BMI的应用程序。我试图根据结果显示不同的文字和图像。使用文本一切都很好,但我不知道如何处理ImageView。
我的代码:
public class ResultActivity extends MainActivity {
private String height;
private String weight;
//?private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
List<ResultModel> results = new ArrayList<ResultModel>();
results.add(new ResultModel(16, "under weight", R.drawable.image_imc_under weight));
result.add(new ResultModel(18, "ideal weight"", R.drawable.image_imc_ideal_weight));
result.add(new ResultModel(25, "overweight"", R.drawable.image_imc_owerweight));
result.add(new ResultModel(31, "obesity"", R.drawable.image_imc_obesity));
result.add(new ResultModel(99, "morbid obesity"", R.drawable.image_imc_morbid_obesity));
TextView tvResultIMC = (TextView) findViewById(R.id.result_imc_tv);
TextView tvResult = (TextView) findViewById(R.id.result_tv);
ImageView imageResult = (ImageView)findViewById(R.id.result_image_view);
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
weight= extras.getString("WEIGHT");
heiht= extras.getString("HEIGHT");
} else {
weight= (String) savedInstanceState.get("WEIGHT");
heiht= (String) savedInstanceState.get("HEIGHT");
}
float valueWeight;
float valueHeight;
int imc;
valueWeight= Float.parseFloat(weight);
valueHeight= Float.parseFloat(heiht)/100;
imc = (int) (valueWeight/ (valueHeight* valueHeight));
for(ResultModel index: results) {
if(imc > index.getmLimit()) {
tvResultIMC.setText(Integer.toString(imc));
tvResult .setText(index.getRes());
imageResult.setImageResource(index.getmImageView());
break;
}
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putCharSequence("WEIGHT",weight);
savedInstanceState.putCharSequence("HEIGHT",height);
}
}
我确实试图找到解决方案而没有。 Android工作室向我显示错误:
1. ResultModel中的1.ResultModel()无法应用于
ImageView中的2.setImageResource无法应用。
拜托,有人可以解释我做错了吗?
非常感谢提前!
答案 0 :(得分:0)
我明白了!
在公共课ResultModel
我必须这样做:
1。private int imageView;
2.In构造函数
this.imageView= imageView;
3.新的吸气者
public int getImageView() {
return imageView;
}