How can Retrieve data in ListView from Firebase

前端 未结 2 1670
余生分开走
余生分开走 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:32

    public class MainActivity extends AppCompatActivity{
     ListView listView;
        FirebaseDatabase firebaseDatabase;
        DatabaseReference databaseReference;
        ArrayList arrayList = new ArrayList<>();
         ArrayAdapter 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(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"
     }
    }
    

提交回复
热议问题