[英]Using Firestore callback in another fragment
I'm trying to seperate my code into MVC-like pieces.我试图将我的代码分成类似 MVC 的部分。 I have a Fragment where I want to display the user name fetched from Firestore.
我有一个片段,我想在其中显示从 Firestore 获取的用户名。 Also I have a class (HomeController) where I put the logic.
我还有一个 class (HomeController),我把逻辑放在那里。 In this class I try to implement a method which fetches the user name from the document.
在这个 class 中,我尝试实现一种从文档中获取用户名的方法。
HomeController家庭控制器
public void getWelcomeNameText(final getNameCallback callback) {
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
welcomeName = document.getString("clientName");
}
}
callback.onCallback(welcomeName);
}
});
}
I'm using interface for the callback.我正在使用回调接口。
public interface getNameCallback {
void onCallback(String name);
}
Also in the constructor I call the method above.同样在构造函数中,我调用了上面的方法。
public HomeController() {
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
userID = fAuth.getUid();
docRef = fStore.collection("Clients").document(userID);
activeJobRef = fStore.collection("ActiveJobs");
getWelcomeNameText(new getNameCallback() {
@Override
public void onCallback(String name) {
welcomeName = name;
//I want to pass this welcomeName to HomeFragment
}
});
}
This fetches the user name perfectly, but I want to use this method in my Fragment called HomeFragment to set the text of a TextView. So actually I want to pass the welcomeName variable.这完美地获取了用户名,但我想在我的片段中使用这个名为 HomeFragment 的方法来设置 TextView 的文本。所以实际上我想传递welcomeName变量。 But actually I have no idea how to do it.
但实际上我不知道该怎么做。 I implement the getNameCallback in the Fragment.
我在片段中实现了getNameCallback 。
HomeFragment主页片段
public class HomeFragment extends Fragment implements HomeController.getNameCallback, View.OnClickListener {
//variables
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
View view = inflater.inflate(R.layout.fragment_home, container, false);
postJob = view.findViewById(R.id.button_add_job);
logout = view.findViewById(R.id.button_sign_out);
bottomNav = getActivity().findViewById(R.id.bottom_nav);
bottomNav.setVisibility(View.VISIBLE);
loginName = view.findViewById(R.id.textView_login_name);
noDataText = view.findViewById(R.id.textView_no_active_job);
homeController = new HomeController();
//here I want something like: loginName.setText(userNameFromCallback);
return view;
}
@Override
public void onCallback(String name) {
//maybe I have to do something here?
}
Thanks for reading this - probably a very amateur - question!感谢阅读这个 - 可能是一个非常业余的 - 问题!
Got the solution, I knew it was a noob-ish question:) I just called:找到了解决方案,我知道这是一个菜鸟问题 :) 我刚刚打电话给:
homeController.getWelcomeNameText(new HomeController.getNameCallback() {
@Override
public void onCallback(String name) {
loginName.setText(name);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.