Problem Retrieving Data from Firebase and Moving to Object

0

I'm trying to make an inventory app in order to study. Where items can be added, saved in the firebase and then displayed in a RecyclerView

Items are currently being saved in the firebase:

Butwhenitcomestorecoveringthemwith:

valueEventListenerItems=itemsReference.addValueEventListener(newValueEventListener(){@OverridepublicvoidonDataChange(@NonNullDataSnapshotdataSnapshot){itemsList.clear();for(DataSnapshotdados:dataSnapshot.getChildren()){Itemsitems=dados.getValue(Items.class);items.setItemId(dados.getKey());itemsList.add(items);}adapterItems.notifyDataSetChanged();}@OverridepublicvoidonCancelled(@NonNullDatabaseErrordatabaseError){}});}

Itdoesnotwork,thedataisnull.Itriedtorunthefollowinglogs:

Followtheclasscode

Items

publicclassItems{privateStringname,description,itemId;privateintamount;publicItems(){}publicvoidsaveItem(){FirebaseAuthauth=FirebaseConfig.getFirebaseAuth();DatabaseReferenceref=FirebaseConfig.getFirebaseRef();StringuserId=Base64Custom.codify64Base(auth.getCurrentUser().getEmail());ref.child("inventory")
            .child(userId)
            .push()
            .setValue(this);

}


public String getItemId() {
    return itemId;
}

public void setItemId(String itemId) {
    this.itemId = itemId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public int getAmount() {
    return amount;
}

public void setAmount(int amount) {
    this.amount = amount;
}

}

Activity

public class ItemsActivity extends AppCompatActivity {

private RecyclerView recyclerItems;
private AdapterItems adapterItems;
private List<Items> itemsList = new ArrayList<>();

private FirebaseAuth authentication = FirebaseConfig.getFirebaseAuth();

private DatabaseReference itemsReference = FirebaseConfig.getFirebaseRef();

private ValueEventListener valueEventListenerItems;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_items);

    recyclerItems = findViewById(R.id.recyclerItems);

    //configurar adapter recebe dados e formata o layout
    adapterItems = new AdapterItems(itemsList,this);



    //configurar recycler
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerItems.setLayoutManager(layoutManager);
    recyclerItems.setHasFixedSize(true);
    recyclerItems.setAdapter(adapterItems);


    FloatingActionButton fabAddItem = findViewById(R.id.fabAddItem);
    fabAddItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(ItemsActivity.this, AddItemActivity.class));
        }
    });


}

public void showItems(){
    String email = authentication.getCurrentUser().getEmail();
    String id = Base64Custom.codify64Base(email);

    itemsReference.child("inventory")
            .child(id);



    valueEventListenerItems = itemsReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            itemsList.clear();

            for (DataSnapshot dados: dataSnapshot.getChildren() ){

                String name = dados.child("name").getValue(String.class);
                Log.i("name", " name "+name);


                Items items = dados.getValue(Items.class);
                items.setItemId(dados.getKey());
                itemsList.add(items);


            }

            adapterItems.notifyDataSetChanged();

        }

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

        }
    });

}

@Override
protected void onStart() {
    super.onStart();
    showItems();

}

@Override
protected void onStop() {
    super.onStop();
    //remover listener
    itemsReference.removeEventListener(valueEventListenerItems);

}

}

FirebaseConfig

public class FirebaseConfig {

private static FirebaseAuth auth;
private static DatabaseReference ref;

public static DatabaseReference getFirebaseRef(){
    if(ref == null){
        ref = FirebaseDatabase.getInstance().getReference();
    }
    return ref;
}

public static FirebaseAuth getFirebaseAuth(){
    if (auth == null){
        auth = FirebaseAuth.getInstance();
    }
    return auth;
}

}

Can anyone help me? I've been trying all day and I can not fix it. Thank you in advance!

    
asked by anonymous 15.08.2018 / 03:04

0 answers