【发布时间】:2019-07-29 18:53:11
【问题描述】:
我正在尝试用数据填充数组列表,但方法完成后它会被清空
我应该将项目放入recyclerview,但是如果我的数组列表无法显示要显示的数据,因为在方法完成后它被清空了 但是当手机背光灯熄灭并且我将其重新打开时会显示项目
如果我
Log.d("TAG",mScheduleNames.get(0));
在 on complete 方法中我得到一个输出
但是,如果我在 firebase 用户之前输出它,我会得到一个在方法之后运行的空点异常...... 数组字符串中的数据似乎已经被清空了
public class Schedules extends Fragment {
private static final String TAG = "AddingEvents";
private ArrayList<String> mScheduleNames = new ArrayList<>();
private ArrayList<String> mScheduleImageUrls = new ArrayList<>();
private FirebaseAuth mAuth;
private int i;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mAuth = FirebaseAuth.getInstance();
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_schedules, container, false);
}
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
final FirebaseUser currentUser = mAuth.getCurrentUser();
final FirebaseFirestore db = FirebaseFirestore.getInstance();
i = 0;
db.collection("Schedules")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
if (document.get("Owner").toString().equals(currentUser.getUid())){
mScheduleNames.add(document.get("Schedule").toString());
mScheduleImageUrls.add("https://www.crockerriverside.org/sites/main/files/imagecache/square/main-images/camera_lense_0.jpeg");
i++;
Log.d(TAG, document.getId() + " => " + document.getData());
}
}
if(i == 0){
Toast.makeText(getContext(), "You do not have any existing schedules. \r\n " +
"Click on add schedule at the bottom", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getContext(), "About to display your schedules ...", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getContext(), "Error getting your schedules.", Toast.LENGTH_SHORT).show();
Log.w(TAG, "Error getting your schedules.", task.getException());
}
}
});
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerviews);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this.getContext(), mScheduleNames, mScheduleImageUrls);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
/*
mScheduleNames.add("Steve");
mScheduleImageUrls.add("https://cdn.arstechnica.net/wp-content/uploads/2016/02/5718897981_10faa45ac3_b-640x624.jpg");
mScheduleNames.add("Bella");
mScheduleImageUrls.add("https://www.crockerriverside.org/sites/main/files/imagecache/square/main-images/camera_lense_0.jpeg");
mScheduleNames.add("Carre");
mScheduleImageUrls.add("https://upload.wikimedia.org/wikipedia/commons/thumb/5/5b/Ultraviolet_image_of_the_Cygnus_Loop_Nebula_crop.jpg/691px-Ultraviolet_image_of_the_Cygnus_Loop_Nebula_crop.jpg");
*/
}
}
我希望数据显示在回收站视图中。
【问题讨论】:
-
OnCompleteListener中的所有内容都将异步 执行,这意味着当您调用new RecyclerViewAdapter(...)时,列表尚未填充。您可以在onComplete()回调结束时调用adapter.notifyDataSetChanged()以告知适配器数据已更新。 -
谢谢你这完全有效......我想了解更多关于异步和同步执行的信息
标签: android android-studio arraylist android-recyclerview listener