How can Retrieve data in ListView from Firebase

前端 未结 2 1669
余生分开走
余生分开走 2021-01-06 15:46

\"Data

Can you help me in this problem, I try to display data from in firebase in Listv

相关标签:
2条回答
  • 2021-01-06 16:22

    Because you are using the wrong listener .You should use addValueEventListener () if you want to listen to changes continuously and if you need to listen just once, you can use addListenerForSingleValueEvent()

    ValueEventListner Example

    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
    

    For more info , you can see this firebase sample on GitHub

    0 讨论(0)
  • 2021-01-06 16:32
    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) {
    
        }
    });
    
     }}
    

    This listener is actually used for lists, so its not from the listener. Also since the user is not authenticated you need to change the rules in your database to :-

     {
    "rules": {
       ".read": "true",
       ".write": "true"
     }
    }
    
    0 讨论(0)
提交回复
热议问题