方法后清空arraylist 运行
arraylist is emptied after the method is run
我正在尝试用数据填充数组列表,但在该方法完成后它被清空了
我应该将项目放入回收视图,但是如果我的数组列表无法显示要显示的数据,因为它在方法完成后被清空
然而,当 phone 背光灯熄灭并且我将其重新打开时,这些项目会显示出来
如果我
Log.d("TAG",mScheduleNames.get(0));
在 on complete 方法中我得到一个输出
但是,如果我在 firebase 用户之前输出它,我会得到一个在方法之后运行的空点异常...
arraystring里面的数据好像被清空了
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");
*/
}
}
我希望数据显示在回收站视图中。
谢谢本 P
这有效
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());
}
adapter.notifyDataSetChanged();
}
});
我正在尝试用数据填充数组列表,但在该方法完成后它被清空了
我应该将项目放入回收视图,但是如果我的数组列表无法显示要显示的数据,因为它在方法完成后被清空 然而,当 phone 背光灯熄灭并且我将其重新打开时,这些项目会显示出来
如果我
Log.d("TAG",mScheduleNames.get(0));
在 on complete 方法中我得到一个输出
但是,如果我在 firebase 用户之前输出它,我会得到一个在方法之后运行的空点异常... arraystring里面的数据好像被清空了
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");
*/
}
}
我希望数据显示在回收站视图中。
谢谢本 P 这有效
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());
}
adapter.notifyDataSetChanged();
}
});