运行该方法后清空arraylist

时间:2019-07-29 18:53:11

标签: android android-studio arraylist android-recyclerview listener

我正在尝试用数据填充数组列表,但是方法完成后它会被清空

我应该将物品放入recyclerview中,但是如果我的数组列表不能显示要显示的数据,因为在完成该方法后将其清空 但是,当手机背光灯熄灭并重新打开时,这些项目会显示出来

如果我

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");
        */
    }
}

我希望数据将显示在“回收者”视图中。

1 个答案:

答案 0 :(得分:0)

谢谢Ben 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();
                    }
                });
相关问题