我有一个基于firebase数据库中的值的setvisibility按钮代码,但无法正常工作。如果Firebase中的状态为“不付款”,我想查看myConfirmPayments按钮,如果Firebase中的状态为“等待付款确认”或“ Payments”,则我想查看myConfirmPayments按钮。
任何人都可以帮助我提供正确的代码吗?
public class MyOrdersActivity extends AppCompatActivity {
private RecyclerView myOrderList;
private Query myOrdersRef;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_orders);
final Button myConfirmPayments = findViewById(R.id.myConfirm_payments);
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
database.child("Orders")
.child("State").addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userDataSnapshot : dataSnapshot.getChildren())
{
UserOrders order = userDataSnapshot.getValue(UserOrders.class);
String state = order.getState();
if (state.equals("Payments"))
{
myConfirmPayments.setVisibility(View.INVISIBLE);
}
else if (state.equals("Waiting Payments Confirm"))
{
myConfirmPayments.setVisibility(View.INVISIBLE);
}
else if (state.equals("Not Payments"))
{
myConfirmPayments.setVisibility(View.VISIBLE);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w("tag", "loadPost:onCancelled", databaseError.toException());
}
});
myOrdersRef = FirebaseDatabase.getInstance().getReference()
.child("Orders")
.orderByKey().equalTo(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());
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);
}
});
holder.MYshowOrderBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
String uID = getRef(position).getKey();
Intent intent = new Intent(MyOrdersActivity.this, AdminUserProductsActivity.class);
intent.putExtra("uid", uID);
startActivity(intent);
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
CharSequence options[] = new CharSequence[]
{
"Ya",
"Tidak"
};
AlertDialog.Builder builder = new AlertDialog.Builder(MyOrdersActivity.this);
builder.setTitle("Sudah Menerima Barang ini ?");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
if (i == 0)
{
String uID = getRef(position).getKey();
RemoverOrder(uID);
}
else
{
finish();
}
}
});
builder.show();
}
});
}
@NonNull
@Override
public MyOrdersActivity.UserOrdersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.orders_layout, parent, false);
return new MyOrdersActivity.UserOrdersViewHolder(view);
}
};
myOrderList.setAdapter(adapter);
adapter.startListening();
}
public static class UserOrdersViewHolder extends RecyclerView.ViewHolder
{
public TextView MYuserName, MYuserPhoneNumber, MYuserTotalprice, MYuserDateTime, MYusershippingAddress, MYorderstate;
public Button MYconfirmPayments, MYshowOrderBtn;
public UserOrdersViewHolder(View itemView)
{
super(itemView);
MYuserName = itemView.findViewById(R.id.myOrders_user_name);
MYuserPhoneNumber = itemView.findViewById(R.id.myOrders_phone_number);
MYuserTotalprice = itemView.findViewById(R.id.myOrders_total_price);
MYuserDateTime = itemView.findViewById(R.id.myOrders_date_time);
MYusershippingAddress = itemView.findViewById(R.id.myOrders_address_city);
MYorderstate = itemView.findViewById(R.id.myOrders_state);
MYconfirmPayments = itemView.findViewById(R.id.myConfirm_payments);
MYshowOrderBtn= itemView.findViewById(R.id.myShow_all_products);
}
}
private void RemoverOrder(String uID)
{
}
}
这是我的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;
}
}
这是我的结构火力地堡