I have thi开发者_JAVA百科s code(class in class) in java (andorid)
public class History extends Activity {
public Integer Type;
private static class HistoryAdapter extends BaseAdapter {
//I don't know how to access variable "Type" here
Integer num = Type;// error
.
.
.
}
.
.
.
}
I can't add more classes above "extends BaseAdapter"
Can someone help me, how can I access variable Type in class HistoryAdapter
Thanks
HistoryAdapter
is a static class, which means that it doesn't have access to its parent class History
. You'll either need to not make it not static (if possible), or you need to have the parent class pass the value of Type
in through a helper function.
Example:
public class History extends Activity {
public Integer Type;
private static class HistoryAdapter extends BaseAdapter {
Integer num;
HistoryAdapter(Integer num) {
this.num = num;
}
}
void foo() {
HistoryAdapter = new HistoryAdapter(Type);
}
}
or
public class History extends Activity {
public Integer Type;
private class HistoryAdapter extends BaseAdapter {
Integer num = Type;
.
.
.
}
.
.
.
}
If your inner class needs access to the fields of the containing class, it has to not be static
... then it will have that access. Alternatively, you can just give HistoryAdapter
a constructor that takes an Integer type
and pass the type from History
to it when you create it.
Also, please don't capitalize the first letter of fields or variables in Java.
精彩评论