如何在布局中显示从Firebase数据库中选择的数据

时间:2019-06-11 11:48:58

标签: android firebase firebase-realtime-database

我正在制作一个在线商店应用程序,在“我的订单”上,我只是想根据在线用户电话号码显示“订单”数据。我想显示从Firebase数据库中选择的数据。

我已指导数据

.child("Orders")
.child(Prevalent.currentOnlineUser.getPhone());

但是错误“无法转换java.lang.string类型的对象

public class MyOrdersActivity extends AppCompatActivity {

private RecyclerView myOrderList;
private DatabaseReference myOrdersRef;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_orders);

    myOrdersRef = FirebaseDatabase.getInstance().getReference()
            .child("Orders")
            .child(Prevalent.currentOnlineUser.getPhone());

    myOrderList = findViewById(R.id.myOrders_list);
    myOrderList.setLayoutManager(new LinearLayoutManager(this));
}

@Override
protected void onStart()
{
    super.onStart();


    FirebaseRecyclerOptions<UserOrders> options =
            new FirebaseRecyclerOptions.Builder<UserOrders>()
                    .setQuery(myOrdersRef, UserOrders.class)
                    .build();

    FirebaseRecyclerAdapter<UserOrders,MyOrdersActivity.UserOrdersViewHolder> adapter =
            new FirebaseRecyclerAdapter<UserOrders, MyOrdersActivity.UserOrdersViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull MyOrdersActivity.UserOrdersViewHolder holder, final int position, @NonNull final UserOrders model)
                {
                    holder.MYuserName.setText("Nama : " + model.getName());
                    holder.MYuserPhoneNumber.setText("Nomor Telepon : " + model.getPhone());
                    holder.MYuserTotalprice.setText("Total Harga = Rp " + model.getTotalAmount());
                    holder.MYuserDateTime.setText("Dipesan Pada: " + model.getDate() + " " + model.getTime());
                    holder.MYusershippingAddress.setText("Alamat: " + model.getAddress() + ", " + model.getCity());`enter code here`
                    holder.MYorderstate.setText("Status: "+ model.getState());

                    holder.MYconfirmPayments.setOnClickListener( new View.OnClickListener() {
                        @Override
                        public void onClick(View view)
                        {
                            String uID = getRef(position).getKey();

                            Intent intent = new Intent(MyOrdersActivity.this, ConfirmPaymentsActivity.class);
                            intent.putExtra("uid", uID);
                            startActivity(intent);
                        }
                    });
}

我的流行

public class Prevalent {
    public static Users currentOnlineUser;

    public static final String UserPhoneKey = "UserPhone";
    public static final String UserPasswordKey = "UserPassword";
}

这是我的基地孩子(订单)

firebase structure

我只想根据在线电话号码显示数据“订单”

我的UserOrders类

public class UserOrders
{
    private String name, phone, address, city, state, date, time, totalAmount;

    public UserOrders()
    {

    }

    public UserOrders(String name, String phone, String address, String city, String state, String date, String time, String totalAmount) {
        this.name = name;
        this.phone = phone;
        this.address = address;
        this.city = city;
        this.state = state;
        this.date = date;
        this.time = time;
        this.totalAmount = totalAmount;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(String totalAmount) {
        this.totalAmount = totalAmount;
    }
}

1 个答案:

答案 0 :(得分:1)

更改此内容:

  myOrdersRef = FirebaseDatabase.getInstance().getReference()
            .child("Orders")
            .child(Prevalent.currentOnlineUser.getPhone());

对此:

  myOrdersRef = FirebaseDatabase.getInstance().getReference()
            .child("Orders")
            .orderByKey();
            .equalTo(Prevalent.currentOnlineUser.getPhone());