【发布时间】:2019-11-26 03:02:05
【问题描述】:
if else 语句会在适配器类中执行吗?
我正在尝试制作类似于 Shopee 应用的通知消息。因此,如果管理员已提交客户跟踪号,我希望用户收到通知,客户将在通知页面中收到消息,该消息将显示包含其跟踪号的文本。如果他们还没有跟踪号。该通知不应显示在客户页面上。
黄色突出显示的是正确的输出,但红线不是。由于管理员尚未更新客户订单跟踪号,因此该通知本不应该发出。
adapter class
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {
if (orderList.get(i).getTrackingNum() == ""){
myViewHolder.note.setText("Your order has not been shipped yet!" +orderList.get(i).dateTime);
}
else
myViewHolder.note.setText(" Your order ID :" + orderList.get(i).getDateTime()+" has been posted. The Tracking number is :" + orderList.get(i).getTrackingNum());
}
notificationClass
protected void onStart() {
super.onStart();
if (dbNotification != null) {
dbNotification.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
orderList = new ArrayList<>();
for (DataSnapshot orderSnapShot : dataSnapshot.getChildren()) {
for (DataSnapshot ds : orderSnapShot.getChildren()) {
orderList.add(ds.getValue(Order.class));
}
}
}
notificationAdapter NotificationAdapter = new notificationAdapter(orderList);
recyclerView.setAdapter(NotificationAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(CustNotification.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
public class Order {
public String cust_id;
public String cart_id;
public String total;
public String name;
public String address;
public String phone;
public String status;
public String dateTime;
public String courier;
public String trackingNum;
public String methodPay;
public Order() {
}
public Order(String cust_id, String cart_id, String total, String name, String address, String phone, String status, String dateTime,String courier, String trackingNum, String methodPay) {
this.cust_id = cust_id;
this.total = total;
this.name = name;
this.address = address;
this.phone = phone;
this.cart_id = cart_id;
this.status = status;
this.dateTime=dateTime;
this.courier=courier;
this.trackingNum=trackingNum;
this.methodPay=methodPay;
}
【问题讨论】:
-
在比较
String时不能使用==,会得到意想不到的结果。将orderList.get(i).getTrackingNum() == ""更改为orderList.get(i).getTrackingNum().equals("")。您甚至可以使用orderList.get(i).getTrackingNum().isEmpty()来检查运单号是否为空。
标签: java android firebase firebase-realtime-database