如何在Java文件中创建变量全局变量

时间:2016-03-26 16:39:35

标签: java variables global-variables

在卢阿,我可以做global variablename并将其全球化 在java中有一种简单的方法吗?

以下是我的代码,我希望loc变量为全局变量,以便public View getView(int position, View convertView, ViewGroup parent){可以使用它:

class custom_row_adapter extends ArrayAdapter<Integer> {
      custom_row_adapter(Context context, int picId, String url) {
        super(context, R.layout.activity_custom_row_adapter, picId);
        String loc = url;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater picInflater = LayoutInflater.from(getContext());
        View customView = picInflater.inflate(R.layout.activity_custom_row_adapter, parent, false);
        String singlePic = getItem(loc + String.valueOf(position) + ".jpg"); **//this is where loc is viewed**
        ImageView theImage = (ImageView) customView.findViewById(R.id.singleImg);
        Glide.with(this).load(singlePic).into(theImage);
        return customView;
}
     

}

1 个答案:

答案 0 :(得分:2)

只需将其添加为和此字段

即可
public class example {

private String loc;

class custom_row_adapter extends ArrayAdapter<Integer> {
    custom_row_adapter(Context context, int picId, String url) {
        super(context, R.layout.activity_custom_row_adapter, picId);
        loc = url;
    }
  }
}