このFirebase構造のデータを取得するにはどうすればよいですか?エラー:タイプjava.util.ArrayListのオブジェクトをタイプモデルQuotationOrderに変換できません

テオを使う

いくつかの方法を試しましたが、それでも内部のデータを取得できません。注文の0,1,2 ...であるため、QuotationOrderクラスに間違ったモデルを作成した可能性があります。Firebaseは初めてです。助けていただければ幸いです。

これがコードと私のモデルです。正しいモデルを作成したかどうかを教えてください。

List<QuotationOrder> quoteList;
quoteList = new ArrayList<>();

final DatabaseReference quotationListRef = FirebaseDatabase.getInstance().getReference()
                .child("QuotationOrder")
                .child(Prevalent.currentOnlineUser.getPhone());
        quotationListRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {                   Log.e("Query Refa",dataSnapshot.getRef().toString());
                Log.e("Query DataSa","\n "+dSnapshot.toString() );
                    for(DataSnapshot ds : dSnapshot.getChildren()) {
                        QuotationOrder quote =  ds.getValue(QuotationOrder.class);
                        quoteList.add(quote);
                     Log.e("Query Refa",dataSnapshot.getRef().toString());
                Log.e("Query Refb",ds.getRef().toString());
                    Log.e("Query DataSb","\n "+ds.toString() );;
public class QuotationOrder {

   String itemID;
    String itemName;
    String itemPrice;
    String itemDescription;
    String itemCategoryType;
    String itemQuantity;
    ArrayList<Cart> order;

    public QuotationOrder() {
    }

    public QuotationOrder(String itemID, String itemName, String itemPrice, String itemDescription, String itemCategoryType, String itemQuantity, ArrayList<Cart> order) {
        this.itemID = itemID;
        this.itemName = itemName;
        this.itemPrice = itemPrice;
        this.itemDescription = itemDescription;
        this.itemCategoryType = itemCategoryType;
        this.itemQuantity = itemQuantity;
        this.order = order;
    }

    public ArrayList<Cart> getOrder() {
        return order;
    }

    public void printOrders() {
        for (Cart carts : this.order) {
            System.out.println(carts);
        }
    }

更新あなたの提案に従った後、arraylistquotelistのサイズはまだ0です。また、モデルクラスを追加しました。正しいモデルを作成したかどうか教えてください。ありがとうございます。データベース構造

UPDATE22つのループのデータスナップショットには下の図の値があるようです。私が直面している問題はそれを保存することです。私が間違っていなければ、データを配列リストに保存できないようです。どうやって保管するの?私のモデルはうまくいきませんでしたか?Log.d( "TAG"、String.valueOf(quote.getItemCategoryType()));からのエラー nullポインタ例外が発生する可能性があります

https://i.stack.imgur.com/sfFBI.png

テオを使う

それで、アレックスからのいくつかの助けと他の投稿からのいくつかのヒントで、私は私の質問への答えを理解することができました。実際に文字列であるitemPriceがintとして宣言されている場合、モデルクラスで実際に問題が発生しました。助けてくれてありがとう、アレックス。

List<Cart> itemList;
itemList = new ArrayList<>();

final DatabaseReference quotationListRef = FirebaseDatabase.getInstance().getReference()
                .child("QuotationOrder")
                .child(Prevalent.currentOnlineUser.getPhone());
                
                 quotationListRef.child(Prevalent.currentOnlineUser.getPhone())
                                            .addListenerForSingleValueEvent(new ValueEventListener() {
                                        @Override
                                        public void onDataChange(@NonNull DataSnapshot dataSnapshot)
                                        {
                                            for (DataSnapshot quotationSnapshot: dataSnapshot.getChildren())
                                            {
                                                
                                                for (DataSnapshot productSnapshot: quotationSnapshot.getChildren())
                                                {
                                                   Cart cart = productSnapshot.getValue(Cart.class);
                                                    itemList.add(cart);

                                                    
                                                    for(Cart a: itemList)
                                                    {
                                                        Log.e("cart","\n "+a.getItemCategoryType() );
                                                    }
                                                }
                                            }

                                        }

                                        @Override
                                        public void onCancelled(@NonNull DatabaseError databaseError)
                                        {
                                            throw databaseError.toException();
                                        }

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ