GridView加载数据的时间太长

时间:2019-02-14 06:09:11

标签: java android firebase gridview load

我不明白为什么我的gridview会永远花费时间来加载数据。我们将不胜感激任何帮助。如果满足某些条件并且使用firebase中的值,我尝试将值加载到DataAdapter中,虽然效果很好,但确实需要很长时间。

这是我在下面的初始化

//Initialize Grid View for programming
        gridview = (GridView) view.findViewById(R.id.gridview);
        //Connect DataSet to Adapter
        lowerAdapter flowerAdapter = new lowerAdapter(getActivity(), `

    mFlowerDataSet); //Now Connect Adapter To GridView
        gridview.setAdapter(flowerAdapter);
        //Add Listener For Grid View Item Click
        gridview.setOnItemClickListener(this);

这是数据集

    //Creating Data Set By Adding flower objects
private ArrayList<lower> prepareDataSet() {

    final ArrayList<lower> flowerData = new ArrayList<>();
    jerrycanAmount = getActivity().getIntent().getExtras().getString("jerrycanAmount");
    final String companyName = getActivity().getIntent().getExtras().getString("Tap");
    getDistaances = FirebaseDatabase.getInstance().getReference().child("WaterPoints").child(companyName);
    final lower[] flower = new lower[1];

    getDistaances.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            //Check if company offers smaal scale water
            if (dataSnapshot.child("smallscalecheck").exists()) {

                //Company does not offer small scale water check
                error.setVisibility(View.VISIBLE);
                errorImage.setVisibility(View.VISIBLE);
                relativeLayoutA.setVisibility(View.GONE);
                relativeLayoutB.setVisibility(View.VISIBLE);
            } else {

                getDistaances.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        if (!dataSnapshot.child("jerrycan").exists()) {

                            //Data is not available
                            relativeLayoutA.setVisibility(View.GONE);
                            relativeLayoutB.setVisibility(View.VISIBLE);
                            error.setVisibility(View.VISIBLE);
                            error.setText("We couldn't find this data from this company");
                            errorImage.setVisibility(View.VISIBLE);

                        } else {

                            //Company Offers Small Scale Services
                            final int jerrycanAmount = (Double.valueOf(dataSnapshot.child("jerrycan").getValue().toString())).intValue();
                            //1st Item
                            flower[0] = new lower();
                            flower[0].setFlowerName("Ksh " + String.valueOf(jerrycanAmount));
                            flower[0].setPhotoPath(R.drawable.yellowjerrican);
                            flowerData.add(flower[0]);

                            //2nd Item
                            flower[0] = new lower();
                            flower[0].setFlowerName("Dispensers");
                            flower[0].setPhotoPath(R.drawable.dispenser);
                            flowerData.add(flower[0]);
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Toast.makeText(getActivity(), databaseError.getMessage().toString(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(getActivity(), databaseError.getMessage().toString(), Toast.LENGTH_SHORT).show();
        }
    });

    return flowerData;
}

0 个答案:

没有答案