Пользовательский диалог для метода CreateView
возвращается до того, как Firebase Firestore onCompletelistener
извлекает данные в список.Вот почему не отображается представление .
Я сделал RecyclerView
с CardView
, при щелчке на представлении карты мне нужно настраиваемое диалоговое окно, которое извлекает данные из firestore..но перед его извлечением из формы Firestore //оператор return rootView выполняется, поэтому в диалоге не отображаются данные
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.crashlytics.android.Crashlytics;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreSettings;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.List;
import io.fabric.sdk.android.Fabric;
public class DialogSetClassTeacher extends DialogFragment {
private RecyclerView recyclerView;
private SetClassTeacherListAdapter adapter;
private FirebaseFirestore databaseTeachers;
private List<ClassTeacher> teacherLists;
FirebaseApp smaTeacher;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Fabric.with(this.getActivity(), new Crashlytics());
final View rootView = inflater.inflate(R.layout.dialog_set_class_teacher, container, false);
FirebaseOptions options = new FirebaseOptions.Builder()
.setDatabaseUrl(getResources().getString(R.string.dbURL))
.setApiKey(getResources().getString(R.string.api))
.setApplicationId(getResources().getString(R.string.appID))
.setProjectId(getResources().getString(R.string.projectId))
.setStorageBucket(getResources().getString(R.string.storageBucket)).build();
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(true)
.build();
boolean hasBeenInitialized = false;
List<FirebaseApp> fireBaseApps = FirebaseApp.getApps(this.getActivity());
for (FirebaseApp app : fireBaseApps) {
if (app.getName().equals("SMA_Teachers")) {
hasBeenInitialized = true;
}
}
if (!hasBeenInitialized) {
smaTeacher = FirebaseApp.initializeApp(this.getActivity(), options, "SMA_Teachers");
}
databaseTeachers = FirebaseFirestore.getInstance(FirebaseApp.getInstance("SMA_Teachers"));
databaseTeachers.setFirestoreSettings(settings);
recyclerView = rootView.findViewById(R.id.dialogSetClassTeacherRecycleView);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
this.getDialog().setTitle("Select a Class Teacher");
databaseTeachers.collection("Teachers").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
teacherLists = new ArrayList<>();
for (DocumentSnapshot doc : task.getResult()) {
ClassTeacher newDataList =
new ClassTeacher(doc.getString("name"), " ", doc.getString("email"));
teacherLists.add(newDataList);
Toast.makeText(getActivity(), "inside", Toast.LENGTH_SHORT).show();
adapter = new SetClassTeacherListAdapter(getActivity(), teacherLists);
recyclerView.setAdapter(adapter);
}
} else {
Toast.makeText(getActivity(), "Error : " + task.getException(), Toast.LENGTH_SHORT).show();
}
}
});
//before getting a data its return
return rootView;
}
}
`