在Firebase检索回收器视图的所有项目之前,如何制作动画视图

时间:2019-05-31 20:26:42

标签: java android firebase android-fragments

在开发我的android应用程序时遇到一些问题,登录活动后,您转到了一个Tab布局活动,该活动包含3个片段,第一个和第三个具有回收站视图,并且发生非常奇怪的错误,当您加载这些片段时,它们被认为是用firebase填充arrayList的,但是在开始时不会发生,直到您访问过第1和第3时,它们才填充arrayList,不知道我是否在解释,如果转到第一个和第二个碎片,则第一个的回收站视图仍然为空,但是如果转到第三个,则第三个和第一个的回收站视图为空,则不知道为什么。我相信Firebase是异步的,因此它会返回一个空数组,直到一段时间,但是为什么一直到第1和第3个选项卡才返回空数组,我相信这是因为Firebase是异步的,所以我想制作一个动画视图,直到Firebase检索所有项目,这是我的代码,因为也许这是一个问题,因为它对我来说毫无意义,因为您必须转到第1和第3个片段才能加载它。

片段

   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.frag_gastos_layout, container, false);
    auth = FirebaseAuth.getInstance();
    FirebaseUser user = auth.getCurrentUser();
    setGastos(user.getDisplayName());
    adapter = new AdapterDinero(getContext());
    adapter.setDineros(dineros);
    if(dineros.isEmpty()){
        Log.d(TAG, "Vacio.");
    }
    else{
        Log.d(TAG, "Empty.");
    }
    adapter.setOnItemClickListener(this);
    recyclerView = view.findViewById(R.id.rv_Gastos);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false));
    btAdd = view.findViewById(R.id.bt_add_ing);
    btAdd.setOnClickListener(this);
    return view;
}

@Override
public void onClick(View v) {
    Intent intent = new Intent(getActivity(), AddActivity.class);
    startActivity(intent);
}


@Override
public void onItemClick(View view, int position) {
    Intent intent = new Intent(getActivity(), DetalleActivity.class);
    Log.w(TAG, "HA PETADO AQUI");
    intent.putExtra("objDinero", dineros.get(position));
    intent.putExtra("tipo", "Gastos");
    startActivity(intent);
}

private void setGastos(String userName){
    FirebaseRead firebaseOperation = new FirebaseRead();
    firebaseOperation.getGastos(userName, new FirebaseRead.DineroIsLoaded() {
        @Override
        public void dineroIsLoaded(ArrayList<Dinero> auxDinero) {
            dineros.clear();
            dineros.addAll(auxDinero);
        }
    });
}


}

public class FragIngresos extends Fragment implements View.OnClickListener, AdapterDinero.OnItemClickListener {

private static final String TAG = "Fragmento ingresos:";
private Button btAdd;
private FirebaseAuth auth;
private ArrayList<Dinero> dineros = new ArrayList<>();
private RecyclerView recyclerView;
private AdapterDinero adapter;

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.frag_ingresos_layout, container, false);
    auth = FirebaseAuth.getInstance();
    FirebaseUser user = auth.getCurrentUser();
    setIngresos(user.getDisplayName());
    adapter = new AdapterDinero(getContext());
    if(dineros.isEmpty()){
        Log.d(TAG, "Vacio.");
    }
    adapter.setOnItemClickListener(this);
    adapter.setDineros(dineros);
    adapter.notifyDataSetChanged();
    recyclerView = view.findViewById(R.id.rvIngresos);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false));
    btAdd = view.findViewById(R.id.bt_add_ing);
    btAdd.setOnClickListener(this);
    return view;
}

@Override
public void onClick(View v) {
    Intent intent = new Intent(getActivity(), AddActivity.class);
    startActivity(intent);
}

@Override
public void onItemClick(View view, int position) {
    Intent intent = new Intent(getActivity(), DetalleActivity.class);
    Log.w(TAG, "HA PETADO AQUI");
    intent.putExtra("objDinero", dineros.get(position));
    intent.putExtra("tipo", "Ingresos");
    startActivity(intent);
}

public void setIngresos(String userName){
    FirebaseRead firebaseOperation = new FirebaseRead();
    firebaseOperation.getIngresos(userName, new FirebaseRead.DineroIsLoaded() {
        @Override
        public void dineroIsLoaded(ArrayList<Dinero> auxDinero) {
            dineros.clear();
            dineros.addAll(auxDinero) ;
        }
    });
}
}

这是读取Firebase数据库的类:

public class FirebaseRead {
private static final String TAG1 = "Operacion de lectura";
private FirebaseDatabase database = FirebaseDatabase.getInstance();
private ArrayList<Dinero> beneficio;
private ArrayList<Dinero> gastos;
float total;

public interface DineroIsLoaded{
    void dineroIsLoaded(ArrayList<Dinero> auxDinero);
}

public void getGastos(String user, DineroIsLoaded gLoaded ){
    gastos = new ArrayList<>();
    final DineroIsLoaded gastosLoaded = gLoaded;
    DatabaseReference ref = database.getReference().child("Users").child(user).child("Gastos");

    ref.addValueEventListener(new ValueEventListener() {
        @Override

        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            gastos.clear();
            for(DataSnapshot ds : dataSnapshot.getChildren()){
                String nombreAux = ds.child("nombre").getValue().toString();
                String descAux = ds.child("descripcion").getValue().toString();
                String fecha = ds.child("fecha").getValue().toString();
                int precioAux = Integer.parseInt(ds.child("total").getValue().toString());
                Dinero dinero = new Dinero(nombreAux, descAux,precioAux, fecha);
                dinero.setId(ds.getKey());
                gastos.add(dinero);
            }
            gastos = ordenaDinero(gastos);
            gastosLoaded.dineroIsLoaded(gastos);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

0 个答案:

没有答案