有没有办法在我的适配器类中使用。
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
if(!flag){
////
}
// Here i want to set if else condition but i am not able to do this.*/
}
答案 0 :(得分:1)
您只能在方法或构造函数中使用if-else
语法。我假设您要初始化ImageAdapter
的字段,查看一些参数。如果是这样,你可以在构造函数中执行:
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public String myField;
public ImageAdapter(int mGalleryItemBackground, Context mContext, boolean flag) {
this.mGalleryItemBackground = mGalleryItemBackground;
this.mContext = mContext;
if(!flag){
myField = "abc";
} else {
myField = "cba";
}
}
}
答案 1 :(得分:1)
您可以通过以下代码执行此操作。
String field = flag?"success":"fail";