我是初学者,想要将if-else转换为切换。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
LinearLayout l = (LinearLayout)
inflater.inflate(R.layout.mf, container, false);
int pos = this.getArguments().getInt("pos");
TextView tv = (TextView) l.findViewById(R.id.text);
tv.setText("Product " + pos);
/*
* Put images with Hard-Coding
*/
if (pos == 0) {
ImageView product_photo = (ImageView) l.findViewById(R.id.myoffer_image);
product_photo.setImageResource(R.drawable.myoffers_0);
Toast.makeText(this.getActivity(), "Product" + pos + " is selected.", Toast.LENGTH_LONG).show();
start_websocket(pos);
}
else if (pos == 1) {
ImageView product_photo = (ImageView) l.findViewById(R.id.myoffer_image);
product_photo.setImageResource(R.drawable.myoffers_1);
Toast.makeText(this.getActivity(), "Product" + pos + " is selected.", Toast.LENGTH_LONG).show();
start_websocket(pos);
}
Myoffers_LinearLayout root = (Myoffers_LinearLayout) l.findViewById(R.id.root);
float scale = this.getArguments().getFloat("scale");
root.setScaleBoth(scale);
return l;
}
我改变了它,就像下面的代码一样..
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
LinearLayout l = (LinearLayout)
inflater.inflate(R.layout.mf, container, false);
int pos = this.getArguments().getInt("pos");
TextView tv = (TextView) l.findViewById(R.id.text);
tv.setText("Product " + pos);
/*
* Put images with Hard-Coding
*/
ImageView product_photo = (ImageView) l.findViewById(R.id.myoffer_image);
switch(pos){
case 0:
product_photo.setImageResource(R.drawable.myoffers_0);
Toast.makeText(this.getActivity(), "Product" + pos + " is selected.", Toast.LENGTH_LONG).show();
start_websocket(pos);
break;
case 1:
product_photo.setImageResource(R.drawable.myoffers_1);
Toast.makeText(this.getActivity(), "Product" + pos + " is selected.", Toast.LENGTH_LONG).show();
start_websocket(pos);
break;
}
但是,这一行出现了错误......
此方法必须返回View
类型的结果public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
你能给我正确的代码转换方法吗? 谢谢。