如何在adnroid中设置内部接口内部全局变量的值并使用接口外部的值
有什么方法可以在接口内部设置全局变量的值并在接口外部使用该值吗?
这是我的代码示例:
class A{
static ArrayList<String> myList = new ArrayList<>();
public static void main(String[] args) {
get(new Inter() {
@Override
public void callBack(ArrayList<String> list) {
myList = list; // here list is the textual data from firebase and I want to use it outside
// this get function call
}
});
myList.toString(); // I want to use it here. But its value out of the function call was null
// but it has the same value with list inside the function call
}
static void get(Inter inter){
// here I want to get some textual data from firebase into ArrayList of String
list = array of String from firebase
inter.callBack(list);
}
interface Inter{
void callBack(ArrayList<String> list);
}
}
Is there any way to set value of global variable inside an interface and use the value outside of it?
Here is sample of my code:
class A{
static ArrayList<String> myList = new ArrayList<>();
public static void main(String[] args) {
get(new Inter() {
@Override
public void callBack(ArrayList<String> list) {
myList = list; // here list is the textual data from firebase and I want to use it outside
// this get function call
}
});
myList.toString(); // I want to use it here. But its value out of the function call was null
// but it has the same value with list inside the function call
}
static void get(Inter inter){
// here I want to get some textual data from firebase into ArrayList of String
list = array of String from firebase
inter.callBack(list);
}
interface Inter{
void callBack(ArrayList<String> list);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
get()
呼叫之后,您不能直接使用列表,因为get呼叫是异步并且尚未完成的。您可以在接口之外创建一个方法,该方法将列表作为参数以参数为例:
或者您可以使用方法 - 参考:
You can't use the list directly after the
get()
call, because the get call is async and has not yet finished.You can create a method outside the interface that takes the list as a parameter:
Or you can use method-reference: