Firebase Android中的嵌套列表对象

时间:2018-07-04 08:26:06

标签: android firebase arraylist firebase-realtime-database nested

https://ibb.co/ieRwsJ

https://ibb.co/c4xmsJ

https://ibb.co/d8gJ6d

这是我的数据库结构。

order
  key
    address
    cart node with 3 childs
    phone
    total etc

我已经尝试了所有方法进入创建的购物车对象,但是当它给我一个错误并且当我尝试在recyclerview中检索它时,它只会返回第一个“ 0”子对象。我尝试使用哈希图和数组列表,并且还提供了recyclerview适配器来显示数据检索。

//this is my pojo below.

public class Order {

private String phone;
private String address;
private String time;
private String date;
private String message;
private String total;
private HashMap<String,Cart> cart;
private String tax;
private String status;

public Order() {
}

//getter and setter


//code for looping over the child node
  orderDB.child("orders").child(currentUser).addValueEventListener(new 
  ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                for (DataSnapshot orderSnapshot : dataSnapshot.getChildren()) {
                    Order order = orderSnapshot.getValue(Order.class);
                    onGoingOrderList.add(order);                   
                    rvOnGoingOrders.setAdapter(onGoingOrderadapter);

                }

            }
        }

//code for my adapter
public void onBindViewHolder(OngoingOrderViewholder holder, int position) {
   holder.tvProductnameOngoingOrders.setText(onGoingOrderArrayList.get(position).getCart().get(position).getName());

这是我将数据检索到哈希图中时遇到的错误       https://ibb.co/d8gJ6d

1 个答案:

答案 0 :(得分:1)

您的代码中的问题是您正在for循环内设置适配器。要解决此问题,请移动以下代码行:

import os
import glob
import mixer
import operator

def title_match(title, songlist):
    """Returns the song from `songlist` whose title has 
    the most unique words in common with `title`.
    If two such songs have the same amount of words intersecting, 
    only returns one song."""

    title = set(tuple(title))
    matches = {song: len(title & set(song.lower().split())) for song in songlist}
    result = max(matches.items(), key=operator.itemgetter(1))[0]

    return result


def detectWords(response):
    response = response.lower()
    words = response.split()

    if 'play' in words:
        os.chdir("/home/pi/Desktop/Stemherkenning/Music")
        songlist = [file for file in glob.glob("*mp3")]
        title = words[words.index("play")+1:]
        file = title_match(title, songlist) #grabs closest matching file
        print("Playing: " + file)
        mixer.music.load(file)
        mixer.music.play()

    if 'stop' in words:
        print("Stopped")
        mixer.music.stop()

在for循环之外,否则最终将只显示一项。