我有一个带有三个片段的选项卡式活动。片段1现在向Firestore添加信息,并片段化两个和三个拉取信息。
使用片段1添加数据后,仅片段3中的微调器更新,而片段2中的微调器不更新。
它们具有相同的代码来加载微调器,所以我不明白为什么第二个片段不会像第三个片段那样重新加载微调器。有人可以帮助确定为什么它的行为与片段3不同吗?
片段2代码(不更新的代码):
public class AddFacultyFragment extends Fragment {
@BindView(R.id.spinner_search_UniFac)
Spinner spinner_search_UniFac;
ArrayList<University> listUni = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_faculty,
container,
false);
ButterKnife.bind(this, view);
FirebaseFirestore db = FirebaseFirestore.getInstance();
final CollectionReference[] colRef = {db.collection("university")};
colRef[0].get().addOnCompleteListener((OnCompleteListener<QuerySnapshot>) task -> {
if (task.isSuccessful()) {
listUni.clear();
for(DocumentSnapshot a : task.getResult()){
University u = new University();
u.setID(a.getId());
u.setDesc(a.get("Desc").toString());
listUni.add(u);
}
}
});
ArrayAdapter<University> spinnerArrayAdapter = new ArrayAdapter<>
(getContext(), android.R.layout.simple_spinner_item,
listUni);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout
.simple_spinner_dropdown_item);
spinner_search_UniFac.setAdapter(spinnerArrayAdapter);
return view;
}
public AddFacultyFragment() {
// Required empty public constructor
}
}
片段三(在我向firestore中添加内容后会更新的片段)
public class AddModuleFragment extends Fragment {
@BindView(R.id.spinner_search_UniMod)
Spinner spinner_search_UniMod;
ArrayList<University> listUni = new ArrayList<>();
ArrayList<Faculty> listFac = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_module,
container,
false);
ButterKnife.bind(this, view);
FirebaseFirestore db = FirebaseFirestore.getInstance();
final CollectionReference[] colRef = {db.collection("university")};
colRef[0].get().addOnCompleteListener((OnCompleteListener<QuerySnapshot>) task -> {
if (task.isSuccessful()) {
listUni.clear();
for(DocumentSnapshot a : task.getResult()){
University u = new University();
u.setID(a.getId());
u.setDesc(a.get("Desc").toString());
listUni.add(u);
}
}
});
ArrayAdapter<University> spinnerArrayAdapter = new ArrayAdapter<>
(getContext(), android.R.layout.simple_spinner_item,
listUni);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout
.simple_spinner_dropdown_item);
spinner_search_UniMod.setAdapter(spinnerArrayAdapter);
return view;
}
public AddModuleFragment() {
// Required empty public constructor
}
}
因此,请澄清一下:
答案 0 :(得分:1)
使用视图分页器“ taps”时。片段创建时,方法onCreateView
仅调用一次。如果您有2个以上的水龙头。当从一个移动到三个“水龙头不能彼此相邻”时。方法onCreateView
调用
所以解决方案是:
在点击2中覆盖onResime
方法,并调用应该更新微调框的代码。
@Override
public void onResume() {
updateSpinner();
super.onResume();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_module,
container,
false);
updateSpinner();
return view;
}
private void updateSpinner(){
ButterKnife.bind(this, view);
FirebaseFirestore db = FirebaseFirestore.getInstance();
final CollectionReference[] colRef = {db.collection("university")};
colRef[0].get().addOnCompleteListener((OnCompleteListener<QuerySnapshot>) task -> {
if (task.isSuccessful()) {
listUni.clear();
for(DocumentSnapshot a : task.getResult()){
University u = new University();
u.setID(a.getId());
u.setDesc(a.get("Desc").toString());
listUni.add(u);
}
}
});
ArrayAdapter<University> spinnerArrayAdapter = new ArrayAdapter<>
(getContext(), android.R.layout.simple_spinner_item,
listUni);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout
.simple_spinner_dropdown_item);
spinner_search_UniMod.setAdapter(spinnerArrayAdapter);
}