无法从Firebase检索数据

时间:2019-01-05 03:13:47

标签: java android database firebase data-retrieval

我创建了一个简单的android,用于从firebase发送和接收数据。但是每次我打开一个新活动以显示数据时,它都会空白。

我已经尝试过各种以前回答过的类似问题。

private void send(){
        String n = name.getText().toString();
        String b = bg.getText().toString();
        String d = dob.getText().toString();
        String l = loc.getText().toString();
        String i = id.getText().toString();
        String m = mob.getText().toString();

        if(TextUtils.isEmpty(n) || TextUtils.isEmpty(b) || TextUtils.isEmpty(d) || TextUtils.isEmpty(l) || TextUtils.isEmpty(i)
                || TextUtils.isEmpty(m)){
            Toast.makeText(this, "Details incomplete!!", Toast.LENGTH_SHORT).show();
        }
        else{
            String id = root.push().getKey();
            Person person = new Person(b,d,i,l,m,n);
            root.child(id).setValue(person);
        }
    }

package com.example.yashkrishan.directory;

/**
 * Created by yashkrishan on 4/1/19.
 */

public class Person {
    private String bg;
    private String dob;
    private String id;
    private String loc;
    private String mob;
    private String name;

    public Person(String bg, String dob, String id, String loc, String mob, String name) {
        this.bg = bg;
        this.dob = dob;
        this.id = id;
        this.loc = loc;
        this.mob = mob;
        this.name = name;
    }

    public Person(){}

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

    public void setLoc(String loc) {
        this.loc = loc;
    }

    public void setMob(String mob) {
        this.mob = mob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public void setBg(String bg) {
        this.bg = bg;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {

        return name;
    }

    public String getLoc() {
        return loc;
    }

    public String getMob() {
        return mob;
    }

    public String getDob() {
        return dob;
    }

    public String getBg() {
        return bg;
    }

    public String getId() {
        return id;
    }
}


DatabaseReference ref;
    FirebaseDatabase database;
    ListView listView;
    ArrayList<Person> list;
    ArrayAdapter<Person> adapter;
    Person person;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        person = new Person();
        listView = (ListView)findViewById(R.id.listView);
        database = FirebaseDatabase.getInstance();
        ref = database.getReference("Person");
        list = new ArrayList<>();
        adapter = new ArrayAdapter<Person>(this,R.layout.info, list);
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot ds: dataSnapshot.getChildren()){
                    person = ds.getValue(Person.class);
                    list.add(person);
                }
                listView.setAdapter(adapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

我想在不同活动的列表视图中显示存储在Firebase数据库中的数据列表。

0 个答案:

没有答案