View Pager Fragment应该加载1次以显示Firebase中的数据(异步任务)

时间:2018-07-28 05:11:14

标签: android firebase android-fragments android-asynctask

我有带有ViewPager的TabLayout,其中托管了3个Fragment。片段1和2进行得很顺利,但是在片段3中我遇到了问题,因为我应该重新加载1次(打开应用程序-滑动至碎片3 [仍然为空]-滑动至碎片1-滑动至碎片3-并且仅显示数据)

我认为此问题是由异步任务引起的,但我不知道如何解决此问题。请帮助我。

ThreeFragment.java 包com.example.yehezkiel.eclassapp;

    public ThreeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        v =  inflater.inflate(R.layout.fragment_three, container, false);

        userRef = FirebaseDatabase.getInstance().getReference("users");
        users = FirebaseAuth.getInstance().getCurrentUser();
        mataKuliahRef = FirebaseDatabase.getInstance().getReference("courses");
        daftarPengumumanRef = FirebaseDatabase.getInstance().getReference("pengumuman");

        mAuth = FirebaseAuth.getInstance();

        mRecyclerView = (RecyclerView) v.findViewById(R.id.PengumumanRView);
        mRecyclerView.setHasFixedSize(true);

        adapter = new PengumumanAdapter(getActivity().getApplicationContext(), consolidatedList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(adapter);


        userRef.child(users.getUid()).child("courses").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(final DataSnapshot dataSnapshot) {

                if(dataSnapshot.exists()){
                    long datacount = dataSnapshot.getChildrenCount();
                    int i = 0;
                    for(DataSnapshot dataHasil : dataSnapshot.getChildren()){
                        final String idCourse =  dataHasil.getKey();
                        obj3.add(idCourse);
                        Log.e("obj3", obj3.toString());
                            queryObj3(obj3);

                    }
                }else {

                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });


        return v;
    }

    public void queryObj3(final ArrayList<String> obj3){

//note ternyata stelah dilihat lewat log, semua looping sblum proses query (cth: mataKul.child().addlistener) akan di prosess terlebih dahulu... dan stelah itu baru query di looping, dan juga untuk looping ke lv query berikutnya... looping query sblumnya akan dislesaikan dulu baru mulai looping####
        final ArrayList<Integer> test1 = new ArrayList<>();
        final ArrayList<Integer> test2 = new ArrayList<>();
        for(int j = 0 ; j<obj3.size() ;j++){
            int k = 0;

                mataKuliahRef.child(obj3.get(j)).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(final DataSnapshot dataSnapshot) {
                        if (dataSnapshot.exists()) {
                            final String name = (String) dataSnapshot.child("name").getValue();
                            if (dataSnapshot.hasChild("pengumuman")) {
                                for (DataSnapshot idKeyPeng : dataSnapshot.child("pengumuman").getChildren()) {
//                                listTugasKey.add(idKeyPeng.getKey());
                                    test1.add(0); // size akan slalu bertmbah jika ada childkey pengumuman
                                    daftarPengumumanRef.child(idKeyPeng.getKey()).addValueEventListener(new ValueEventListener() {
                                        @Override
                                        public void onDataChange(DataSnapshot dataSnapshot2) {
                                            DaftarPengumuman pengumuman = new DaftarPengumuman();
                                            pengumuman = dataSnapshot2.getValue(DaftarPengumuman.class);
                                            pengumuman.setNama_p(name);
                                            listPengumuman.add(pengumuman);
                                            test2.add(0); // size akan bertmbah jika child key sudah di load

                                            if (test1.size() == test2.size()) {
                                                hashMap();
                                            }

                                        }

                                        @Override
                                        public void onCancelled(DatabaseError databaseError) {

                                        }
                                    });
                                }//end of for

                            }//end of has child
                            else {
                            }


                        }//end of if exist


                    }//end of first ondatachange


                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
        }
    }



    private HashMap<String, List<DaftarPengumuman>> groupDataIntoHashMap(List<DaftarPengumuman> listOfDaftarPengumuman) {

        HashMap<String, List<DaftarPengumuman>> groupedHashMap = new HashMap<>();

        for (DaftarPengumuman daftarPengumuman : listOfDaftarPengumuman) {

            String hashMapKey = daftarPengumuman.getNama_p();

            if (groupedHashMap.containsKey(hashMapKey)) {
                // The key is already in the HashMap; add the pojo object
                // against the existing key.
                groupedHashMap.get(hashMapKey).add(daftarPengumuman);
            } else {
                // The key is not there in the HashMap; create a new key-value pair
                List<DaftarPengumuman> list = new ArrayList<>();
                list.add(daftarPengumuman);
                groupedHashMap.put(hashMapKey, list);
            }
        }


        return groupedHashMap;
    }

    private void hashMap(){
        HashMap<String, List<DaftarPengumuman>> groupedHashMap = groupDataIntoHashMap(listPengumuman);

        for (String nama_p : groupedHashMap.keySet()) {
            PengumumanItem nameItem = new PengumumanItem();
            nameItem.setNama_matkul(nama_p);
            consolidatedList.add(nameItem);


            for (DaftarPengumuman daftarPengumuman : groupedHashMap.get(nama_p)) {
                GeneralItem generalItem = new GeneralItem();
                generalItem.setDaftarPengumuman(daftarPengumuman);//setBookingDataTabs(bookingDataTabs);
                consolidatedList.add(generalItem);
            }
        }
    }


}

1 个答案:

答案 0 :(得分:1)

尝试一下

viewPager.setOffscreenPageLimit(3);