如何从android

时间:2017-10-05 05:18:50

标签: android firebase android-recyclerview

早安所有

我想在Recycleview适配器中从Firebase获取数据

这是我的Firebase结构:

enter image description here

在我的recycleView适配器中,数据来自api。

firebase结构说明

此处 user_product_bid 是我的主要节点,“42”是我的product_id(子节点),并且在“出价”值下可用。我想根据适配器positionwise product_id从firebase获取数据。我已成功从recycleView发布数据但在获取时出现问题

请帮助我完成此任务或建议我。

2 个答案:

答案 0 :(得分:0)

使用以下代码获取数据并将其填充到recyclerview

List<User_Product_Bid> userPdtBidObj = new ArrayList<User_Product_Bid>();
private DatabaseReference firebaseInstance = FirebaseDatabase.getInstance().getReference();
private DatabaseReference 
        userPdtsBidRef = firebaseInstance.child("user_product_bid");
userPdtsBidRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            long count = dataSnapshot.getChildrenCount();
            if(count != 0) {
            for (DataSnapshot dataObj : dataSnapshot.getChildren()) {

                  User_Product_Bid obj = dataObj.getValue(User_Product_Bid.class);
                  userPdtBidObj.add(obj);
             }
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        });

User_Product_Bid的模型类

@IgnoreExtraProperties
public class User_Product_Bid {

    @SerializedName("bids")
    private boolean List<String> bids;

    public User_Product_Bid() {
    }

    public List<String> getBids() {
        return bids;
    }

    public void setBids(List<String> bids) {
        this.bids = bids;
    }
}

答案 1 :(得分:0)

你可以简单地做到这一点..有一个简单的方法。但你必须为此制作一个Model类。这取决于你...

import socket
import sys
import traceback

HOST, PORT = '', 80

def between(left,right,s):
    before,_,a = s.partition(left)
    a,_,after = a.partition(right)
    return a

filereq = ""
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
lines = []
print("Started!")
listen_socket.listen(1)
print("Listening")
while True:
        try:
                lines = []
                client_connection, client_address = listen_socket.accept()
                print("Connected")
                request = client_connection.recv(1024)
                print("Received Data!")

                # Decode the data before processing.
                decoded = request.decode('utf-8')
                filereq = between("GET /", " HT", decoded)
                print(filereq)
                filereq = open(filereq)
                for line in filereq:
                        lines.append(line)
                print(lines)
                sendata = ''.join(lines)
                print(sendata)
                http_response = """\
                HTTP/1.1 200 OK 

                {}
                """.format(sendata)
                print(http_response)

                # Encode the response before sending.
                encoded = http_response.encode('utf-8')
                client_connection.sendall(encoded)
                print("Sent the Data!")
                client_connection.close()
                print("Connection Closed!")
        except Exception:
                # Print the traceback if there's an error.
                traceback.print_exc(file=sys.stderr)
相关问题