2016-03-26 16 views
1

Sadece global variablename yapabildiğim ve global yapabilmesi için java'da bunu yapmanın basit bir yolu var mı?Bir Java dosyasında bir değişkeni global olarak nasıl yapabilirim

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

Bu durumda, 'public static String loc' inanıyorum diye düşünmeniz gerekir eve ama sen 'class custom_row .....' satırından sonra onu koymak gerekir ve ona erişmek için 'custom_row_adapter.loc' – 3kings

+0

yapmalısınız gerekir Bir dil olarak java küresel değişken kapsamı gibi bir şey yoktur gider (değişkenler için genel bir ad alanı yoktur). Ancak, referansı (örneğin, MyClass.myStaticPublicField) – Durandal

+0

@ 3kings çalışmasını tam olarak niteleyerek herhangi bir genel statik üyeye başvurabilirsiniz! Cevap olarak gönderilsin mi? – Pythogen

cevap

2

sadece bu

gibi olarak ekleyin ve saha: Burada

Ben public View getView(int position, View convertView, ViewGroup parent){ kullanabilirsiniz böylece loc değişken küresel olmak istiyorum, benim kodudur
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; 
    } 
    } 
} 
İlgili konular