Reputation: 101
I am getting a blank listView on running this code.I have checked the contents of the array by displaying them in a text view to the bottom of the listView.Changing to linear layout didn't work as well. This is my first time using a listView so forgive me if its a stupid question.
XML CODE
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_background"
tools:context=".current_month">
<ListView
android:id="@+id/barcode_detail"
android:layout_width="374dp"
android:layout_height="403dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.486"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.277" />
<TextView
android:id="@+id/title_tv"
android:layout_width="299dp"
android:layout_height="42dp"
android:layout_marginStart="141dp"
android:layout_marginEnd="141dp"
android:fontFamily="sans-serif-black"
android:textAlignment="center"
android:textColor="#F7F3F3"
android:textSize="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.037" />
<TextView
android:id="@+id/resultView"
android:layout_width="245dp"
android:layout_height="143dp"
android:layout_marginStart="29dp"
android:layout_marginTop="29dp"
android:layout_marginEnd="65dp"
android:layout_marginBottom="65dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/bar_code_scan"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/barcode_detail" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/bar_code_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:clickable="true"
android:focusable="true"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/fruit_scan"
app:layout_constraintEnd_toEndOf="@+id/fruit_scan"
app:srcCompat="@drawable/ic_barcode_scan" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/scanButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="339dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:clickable="true"
android:contentDescription="TODO"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/ic_baseline_add_24" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fruit_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:clickable="true"
android:focusable="true"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/scanButton"
app:layout_constraintEnd_toEndOf="@+id/scanButton"
app:srcCompat="@drawable/ic_vegetable_scan" />
</androidx.constraintlayout.widget.ConstraintLayout>
JAVA CODE (A subset of the oncreate function)
final ArrayList<String> dat = new ArrayList<>();
final FirebaseUser users = firebaseAuth.getCurrentUser();
String result = users.getEmail().replace(".","");
DatabaseReference databaseReference2 = FirebaseDatabase.getInstance().getReference().child("Users").child(result).child("January").child("Product");
databaseReference2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
dat.add(snapshot.child("productname").getValue().toString());
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
ArrayAdapter arrayAdapter = new ArrayAdapter(current_month.this, android.R.layout.simple_list_item_1,dat);
barcode_detail.setAdapter(arrayAdapter);
Upvotes: 0
Views: 40
Reputation: 9073
Your arrayAdapter
is not being notified of the changes made to list.
after adding items to the list you should call arrayAdapter.notifyDataSetChanged();
Your code should look something like this:
final ArrayList<String> dat = new ArrayList<>();
final FirebaseUser users = firebaseAuth.getCurrentUser();
String result = users.getEmail().replace(".","");
ArrayAdapter arrayAdapter = new ArrayAdapter(current_month.this, android.R.layout.simple_list_item_1,dat);
barcode_detail.setAdapter(arrayAdapter);
DatabaseReference databaseReference2 = FirebaseDatabase.getInstance().getReference().child("Users").child(result).child("January").child("Product");
databaseReference2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
dat.add(snapshot.child("productname").getValue().toString());
}
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
Upvotes: 1