如何在基类适配器类中使用if else条件

时间:2014-04-07 10:30:56

标签: java android

有没有办法在我的适配器类中使用。

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.*/
}

2 个答案:

答案 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"; 
相关问题