S
S
sanke462018-05-31 15:05:25
Java
sanke46, 2018-05-31 15:05:25

How to implement ProgressBar with FireBase loading?

I'm trying to figure out where to insert setVisibility(), but it just doesn't work, what should I do?? If there is a more elegant and correct solution, I am ready to completely rewrite the code, but do it right

package com.sanke46.android.e_commerce.fireBase;

import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.sanke46.android.e_commerce.adapter.RecyclerViewAdapter;
import com.sanke46.android.e_commerce.adapter.SalesRecyclerViewAdapter;
import com.sanke46.android.e_commerce.model.Item;

import java.util.ArrayList;

public class FirebaseHandler {

    private static final String TAG = FirebaseHandler.class.getSimpleName();
    private Item item;
    private FirebaseDatabase database = FirebaseDatabase.getInstance();
    private DatabaseReference myRef = database.getReference("product");

    public ArrayList<Item> getAllSalesItem(String titleProduct,
                                           final ArrayList<Item> arrayOfItemProduct,
                                           final SalesRecyclerViewAdapter adapter,
                                           final ProgressBar progressBar,
                                           final LinearLayout mContentLayout){
        arrayOfItemProduct.clear();
        myRef.child(titleProduct).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                progressBar.setVisibility(View.VISIBLE);
                mContentLayout.setVisibility(LinearLayout.GONE);

                for (final DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    item = snapshot.getValue(Item.class);
                    if(item.isSales()) {
                        arrayOfItemProduct.add(item);
                    }
                }
                adapter.notifyDataSetChanged();

                progressBar.setVisibility(View.GONE);
                mContentLayout.setVisibility(LinearLayout.VISIBLE);
            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });
        return arrayOfItemProduct;
    }

    public ArrayList<Item> getAllItem(String titleProduct, final ArrayList<Item> arrayOfItemProduct, final RecyclerViewAdapter adapter){
        arrayOfItemProduct.clear();
        myRef.child(titleProduct).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (final DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    item = snapshot.getValue(Item.class);
                    if(!item.isSales()) {
                        arrayOfItemProduct.add(item);
                    }
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });

        return arrayOfItemProduct;

    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question