簡體   English   中英

如何從 Firebase 數據庫存儲中檢索數據並將其顯示到自定義列表視圖中?

[英]How do I retrieve data from firebase database Storage and displaying it into custom listview?

我發現很難在 ListView 中顯示我的社區節點。 這是我在 Firebase 中的樹: 我的 Firebase 樹

我想在 Android 的 ListView 中顯示包含三個社區(community1、2、3)及其詳細信息 MessageSentCount、MessageReceivedCount 的三個項目的列表。

我也是 Firebase 的 Android 新手,我學習了許多教程,但在嘗試應用程序時,我的課程崩潰了:

    DatabaseReference dref;
    ListView listView;
    ArrayList<String> list = new ArrayList<>();
    ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_analyze);

    listView =(ListView)findViewById(R.id.listview);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,list);
    listView.setAdapter(adapter);

    dref = FirebaseDatabase.getInstance().getReference("clusters");
    dref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String value = dataSnapshot.getValue(String.class);
            list.add(value);
            adapter.notifyDataSetChanged();
        }

我的 layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/button_discover"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="@string/button_discover"
        android:onClick="discoverYourself"/>
</LinearLayout>

我查了很多教程,但沒有找到解決方案。 請幫幫我。

謝謝 :)

  • 嗨,請嘗試在添加孩子時添加日志消息

    public void onChildAdded(DataSnapshot dataSnapshot, String s) { String value = dataSnapshot.getValue(String.class); Log.e("value",value) list.add(value); adapter.notifyDataSetChanged(); }

    確保有一些孩子並且問題出在您的列表視圖上

  • 然后嘗試更改: adapter.notifyDataSetChanged(); 通過adapter.notifyItemInserted(list.size()-1);

  • 還嘗試在您的 xml 上更改此設置: android:layout_height="0dip"通過: android:layout_height="wrap_content"

修復轉換異常:

dref = FirebaseDatabase.getInstance().getReference("clusters/Community");
dref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String name = dataSnapshot.child("name").getValue(String.class);
int messagesSentCount = dataSnapshot.child("messagesSentCount").getValue(Integer.class);
list.add(name);
adapter.notifyItemInserted(list.size()-1);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM