如何从Firebase

时间:2017-10-29 10:19:27

标签: java android firebase firebase-realtime-database

Data Structure in Firebase

你可以帮我解决这个问题,我尝试在Listview中的firebase中显示数据,但不显示任何内容!

ListView listView;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
ArrayList<String> arrayList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fairbase_list);

    firebaseDatabase=FirebaseDatabase.getInstance();
    databaseReference= firebaseDatabase.getReference("Users");


    listView=(ListView) findViewById(R.id.list);
    final ArrayAdapter<String>  arrayAdapter= new ArrayAdapter<String>(this ,android.R.layout.simple_list_item_1,arrayList);
    listView.setAdapter(arrayAdapter);
    Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show();
    databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String value=dataSnapshot.getValue(String.class);
            arrayList.add(value);
            arrayAdapter.notifyDataSetChanged();

        }

}

2 个答案:

答案 0 :(得分:2)

public class MainActivity extends AppCompatActivity{
 ListView listView;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;
    ArrayList<String> arrayList = new ArrayList<>();
     ArrayAdapter<String> arrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("Users");


listView = (ListView) findViewById(R.id.lists);

Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show();
databaseReference.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        String value=dataSnapshot.getValue(String.class);
        arrayList.add(value);
        arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, arrayList);
        listView.setAdapter(arrayAdapter);

    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

 }}

此侦听器实际上用于列表,因此它不是来自侦听器。 此外,由于用户未经过身份验证,您需要将数据库中的规则更改为: -

 {
"rules": {
   ".read": "true",
   ".write": "true"
 }
}

答案 1 :(得分:0)

因为你使用的是错误的听众。如果你想连续听取变化,你应该使用addValueEventListener (),如果你只需要听一次,你可以使用addListenerForSingleValueEvent()

ValueEventListner示例

ValueEventListener postListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // Get Post object and use the values to update the UI
        Post post = dataSnapshot.getValue(Post.class);
        // ...
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // Getting Post failed, log a message
        Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
        // ...
    }
};
mPostReference.addValueEventListener(postListener);
PostDetailActivity.java

有关详细信息,您可以在GitHub

上查看此firebase示例