从Firebase数据库填充ListView数据

时间:2018-09-27 18:01:30

标签: java android firebase firebase-realtime-database

您好,我有一个Android应用程序,可从Firebase检索数据。

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

    listViewProducts = (ListView)findViewById(R.id.listView);
    final List<Product> products = new ArrayList<Product>();

    database = FirebaseDatabase.getInstance();
    ref = database.getReference();
    ref.child("Sold").child("Item").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Iterable<DataSnapshot> children = dataSnapshot.getChildren();

            for (DataSnapshot child : children) {
                Product value = child.getValue(Product.class);
                products.add(value);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
    ArrayAdapter<Product> productAdapter = new ArrayAdapter<Product>(getApplicationContext(),
            android.R.layout.simple_list_item_1, products);
    listViewProducts.setAdapter(productAdapter);
}

Product类具有类似于Firebase的描述和数量字符串。

   class Product {
       String Description;
       String Quantity;
   }

我的应用程序没有给我任何错误,但是从firebase加载数据时它只是关闭。

为什么我的应用崩溃了?没有任何错误,请帮忙,先生。

enter image description here

编辑:对于logCat

09-28 02:24:31.404 4679-4688/? E/ANDR-PERF-MPCTL: Invalid profile no. 0, total profiles 0 only
09-28 02:24:31.414 4679-4688/? E/ANDR-PERF-RESOURCEQS: Failed to apply optimization [4, 0]
09-28 02:24:31.444 2929-29436/? E/ActivityThread: Failed to find provider info for com.vivo.smartmultiwindow
09-28 02:24:31.474 2929-29450/? E/ActivityThread: Failed to find provider info for com.vivo.smartmultiwindow
09-28 02:24:31.474 2929-29451/? E/ActivityThread: Failed to find provider info for com.vivo.smartmultiwindow
09-28 02:24:32.394 2929-29461/? E/ActivityThread: Failed to find provider info for com.vivo.smartmultiwindow
09-28 02:24:32.614 7415-11587/? E/NetworkScheduler: Invalid component specified.
09-28 02:24:34.234 29437-29437/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mark.mobilethesis, PID: 29437
    com.google.firebase.database.DatabaseException: Found two getters or fields with conflicting case sensitivity for property: description
        at com.google.android.gms.internal.firebase_database.zzku.zzae(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzku.<init>(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
        at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
        at com.example.mark.mobilethesis.MainActivity$1.onDataChange(MainActivity.java:59)
        at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5619)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:737)
09-28 02:24:34.304 2929-3298/? E/InputDispatcher: channel '4f6cc30 com.example.mark.mobilethesis/com.example.mark.mobilethesis.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
09-28 02:24:34.324 2929-29470/? E/ActivityThread: Failed to find provider info for com.vivo.smartmultiwindow
09-28 02:24:34.324 2929-29471/? E/ActivityThread: Failed to find provider info for com.vivo.smartmultiwindow
09-28 02:24:34.344 2929-29472/? E/ActivityThread: Failed to find provider info for com.vivo.smartmultiwindow

2 个答案:

答案 0 :(得分:0)

至少您在adapter.notifyDataSetChanged()方法中缺少对onDataChange()的调用:

public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    Iterable<DataSnapshot> children = dataSnapshot.getChildren();

    for (DataSnapshot child : children) {
        Product value = child.getValue(Product.class);
        products.add(value);
    }

    productAdapter.notifyDataSetChanged();
}

这当然要求您将productAdapter存储在onDataChange方法可以访问它的位置。

答案 1 :(得分:0)

错误似乎在这里-

这是您的模型课

class Product {
    String Description;
    String Quantity;
}

但是在您的数据库结构中,Quantity的数据类型似乎是整数。

因此,请尝试将模型类更改为-

class Product {
    String Description;
    int Quantity;
}