如何使用列表视图将Firebase中的所有数据检索到页面上?

时间:2019-04-30 07:48:26

标签: android firebase firebase-realtime-database

使用listview从firebase检索数据时显示为空吗?我想将所有值都提取到页面上]

public class viewpgdetails extends AppCompatActivity {

    private DatabaseReference mDatabase;
    private FirebaseDatabase database;
    private TextView ft_email, ft_address, ft_fact, ft_owner, ft_phone, ft_pin, ft_rent, ft_pgname;
    private FirebaseStorage fstorage;
    private ListView list_view;
    private StorageReference storageRef;
    private ImageView view_img1, view_img2, view_img3, view_img4, view_img5;


    //FirebaseListAdapter adapter;
    public static Context context;
    ArrayList<String> list;
    ArrayAdapter<String> adapter;
    user info;


   // final String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpgdetails);
        FirebaseDatabase database = FirebaseDatabase.getInstance();

       // String key=mDatabase.getKey();
        DatabaseReference mDatabase = database.getReference().child("USER");
        list = new ArrayList<>();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);


        list_view = (ListView) findViewById(R.id.list_view);

        mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


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


                        user info =ds.getValue(user.class);
                        //adapter.add(String.valueOf(info.getOwner()+"   "));
                     // adapter.add(String.valueOf(info.getEmail()));
                    adapter.add(String.valueOf(info.getAddress()));

                                //+ info.getOwner().toString() + "    " + info.getPhone().toString() + "   " + info.getAddress().toString() + "    " + info.getPin().toString() + "  " + info.getFact().toString() + "   " + info.getRent().toString());

                    }

                list_view.setAdapter(adapter);

                // showData(dataSnapshot);

                //Toast.makeText(viewpgdetails.this, dataSnapshot.toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(viewpgdetails.this, "failed to get data", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

I have added my database structure below From Firebase docs:

  

通常在从数据库中检索项目列表时使用child_added事件。

所以我认为你应该写类似的东西:

ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
      user info = dataSnaphot.getValue(user.class);
      adapter.add(String.valueOf(info.getAddress()));
}

ref应该引用所有用户的数据库父级,例如: enter image description here