I'm having trouble showing stored data in ListView. Everything works well without errors but my data is not showing.
In my app a teacher registers and chooses the subjects they want to teach. The data is stored in Firebase database. On the next activity they are supposed to review their chosen subjects (the subjects are supposed to be shown in ListView). But, I just can't seem to get it to work. It shows up blank. I don't know what I'm doing wrong.
ConfirmSubjectsActivity
import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; import com.google.firebase.database.ChildEventListener; 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 java.util.ArrayList; public class ConfirmSubjectsActivity extends AppCompatActivity { private DatabaseReference mDatabase; private ListView list_id; private ArrayList<String> arrayList = new ArrayList<>(); private ArrayAdapter<String> arrayAdapter; private FirebaseAuth mAuth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_confirm_subjects); mAuth = FirebaseAuth.getInstance(); FirebaseUser user = mAuth.getCurrentUser(); String userId = user.getUid(); arrayAdapter = new ArrayAdapter<String>(ConfirmSubjectsActivity.this, android.R.layout.simple_list_item_1, arrayList); list_id = findViewById(R.id.list_id); list_id.setAdapter(arrayAdapter); mDatabase = FirebaseDatabase.getInstance().getReference(); mDatabase.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { for (DataSnapshot subjectssnapshot: snapshot.getChildren()){ String value = subjectssnapshot.child("Predmeti").getValue(String.class); arrayList.add(value); } } @Override public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { } @Override public void onChildRemoved(@NonNull DataSnapshot snapshot) { } @Override public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { } @Override public void onCancelled(@NonNull DatabaseError error) { } }); } }
"Predmeti" = "Subjects" just fyi
https://stackoverflow.com/questions/66697761/show-firebase-data-in-listview March 19, 2021 at 03:21AM
没有评论:
发表评论