0
I’m trying to make an inventory app in order to study. Where items can be added, saved to firebase and then displayed in a Recyclerview
Items are being saved normally in firebase:
But in time to retrieve them with:
valueEventListenerItems = itemsReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
itemsList.clear();
for (DataSnapshot dados: dataSnapshot.getChildren() ){
Items items = dados.getValue(Items.class);
items.setItemId(dados.getKey());
itemsList.add(items);
}
adapterItems.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
It doesn’t work, the data goes blank. I tried to run the following logs:
Follows the code of the classes
Items
public class Items {
private String name, description, itemId;
private int amount;
public Items() {
}
public void saveItem(){
FirebaseAuth auth = FirebaseConfig.getFirebaseAuth();
DatabaseReference ref = FirebaseConfig.getFirebaseRef();
String userId = 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;
}
}
Could someone help me? I’m trying all day and I can’t fix it. Thank you in advance!
Specify the child (Children) of the inventory in
for
or useitemsReference.child("inventory").addEvent...
. Ps.: No use covering the email.– Valdeir Psr
Thank you very much, friend! I spent more than 10 hours searching on and had found nothing!
– user95764