列出android中的孩子

时间:2017-06-02 09:13:26

标签: android listview firebase firebase-realtime-database

我是Android编程新手。我想在Android中列出Firebase中的所有子列表。

我写了这段代码。但是当我启动应用程序时,listview返回null。

出了什么问题?

public class haber_goruntule extends AppCompatActivity {
private ListView liste;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_haber_goruntule);
    final List<connector_haber> mesajlar=new ArrayList<connector_haber>();

    liste= (ListView) findViewById(R.id.list);
    FirebaseDatabase firebaseDatabase=FirebaseDatabase.getInstance();
    DatabaseReference ref=firebaseDatabase.getReference();

    ref.child("haberler").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Iterable<DataSnapshot> children=dataSnapshot.getChildren();
            for(DataSnapshot child : children){

             connector_haber haber=child.getValue(connector_haber.class);
                mesajlar.add(haber);
            }


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    final ArrayAdapter<connector_haber> arrayAdapter=new ArrayAdapter<connector_haber>(this,android.R.layout.simple_list_item_1,mesajlar);
    liste.setAdapter(arrayAdapter);
  }
 }

3 个答案:

答案 0 :(得分:1)

数据是从Firebase异步加载的。当它回来时,你将它添加到适配器,但Android不再知道它。要使其了解,请在添加数据后致电notifyDataSetChanged()

final ArrayAdapter<connector_haber> arrayAdapter=new ArrayAdapter<connector_haber>(this,android.R.layout.simple_list_item_1,mesajlar);
liste.setAdapter(arrayAdapter);
ref.child("haberler").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Iterable<DataSnapshot> children=dataSnapshot.getChildren();
        for(DataSnapshot child : children){

            connector_haber haber=child.getValue(connector_haber.class);
            mesajlar.add(haber);
        }
        adapter.notifyDataSetChanged(); // tell the adapter to update the view

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore this error
    }
});

答案 1 :(得分:1)

试试这个:

   ref.child("haberler").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Iterable<DataSnapshot> children=dataSnapshot.getChildren();
        for(DataSnapshot child : children){

            connector_haber haber=child.getValue(connector_haber.class);
            mesajlar.add(haber);
        }
       final ArrayAdapter<connector_haber> arrayAdapter=new ArrayAdapter<connector_haber>(this,android.R.layout.simple_list_item_1,mesajlar);
       liste.setAdapter(arrayAdapter); 

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore this error
    }
});

答案 2 :(得分:0)

嗯..有一个用于使用firebase实现UI的开源库。试试这个FirebaseUI