我必须禁用已经预订的广告位,并仅向用户显示可用的广告位。在回收者视图中,时间从09:00Am到09:00Pm可见。已预订的插槽应处于禁用模式,用户只能选择可用的插槽。
在主要活动中,我存储了从09:00 AM到09:00 PM的所有时隙并传递到适配器。
这里是适配器类,我想从firebase获取预订的插槽并在时间arraylist对象中比较这些插槽。
getHourarray()
ArrayList<TimeSlot>times = new ArrayList<TimeSlot>();
String firstDate = "26/02/2019";
String firstTime = "09:00 AM";
String secondDate = "26/02/2019";
String secondTime = "09:00 PM";
String format = "hh:mm a";
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date dateObj1 = null;
try {
dateObj1 = sdf.parse(firstTime);
} catch (ParseException e) {
e.printStackTrace();
}
Date dateObj2 = null;
try {
dateObj2 = sdf.parse(secondTime);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("Date Start: "+dateObj1);
System.out.println("Date End: "+dateObj2);
long dif = dateObj1.getTime();
int i=0;
while (dif < dateObj2.getTime()) {
Date slot = new Date(dif);
SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm a");
TimeSlot t = new TimeSlot();
t.time = String.valueOf(sdf1.format(slot));
t.isAvailable = "Available";
times.add(t);
dif += 3600000;
}
TimeSlot模型类
public class TimeSlot {
public String time;
public String isAvailable;
}
TimeAdapter
在适配器类中,它应该获取已经预订的插槽并与arraylist进行比较。如果已预订的插槽等于arraylist时间,则应为禁用模式,其他插槽应为启用模式
public class TimeAdapter extends RecyclerView.Adapter<TimeAdapter.TimeViewHolder> {
private AdapterCallback mAdapterCallback;
IMethodCaller iMethodCaller;
ArrayList<TimeSlot> times;
private Context context;
String start_time="";
public String getStart_time() {
return start_time;
}
public void setStart_time(String start_time) {
this.start_time = start_time;
}
public TimeAdapter( ArrayList<TimeSlot> times, Context context,IMethodCaller iMethodCaller) {
this.iMethodCaller = iMethodCaller;
this.times = times;
this.context = context;
}
@Override
public TimeAdapter.TimeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.time_slot_layout, parent, false);
TimeViewHolder viewHolder = new TimeViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final TimeAdapter.TimeViewHolder holder, final int position) {
holder.text_time.setText(times.get(position).getTime());
holder.text_description.setText(times.get(position).getIsAvailable());
int count=0;
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.cardView.setCardBackgroundColor(context.getResources().getColor(android.R.color.darker_gray));
holder.text_description.setTextColor(context.getResources()
.getColor(android.R.color.white));
holder.text_time.setTextColor(context.getResources().getColor(android.R.color.white));
start_time = times.get(position).toString();
iMethodCaller.getTimefromAdapter(times.get(position));
}
});
}
@Override
public int getItemCount() {
return times.size();
}
public static class TimeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ItemClickListener itemClickListener;
protected TextView text_time,text_description;
protected CardView cardView;
public TimeViewHolder(View itemView) {
super(itemView);
cardView=(CardView)itemView.findViewById(R.id.card_time_slot);
text_time = (TextView) itemView.findViewById(R.id.time_slot);
text_description=(TextView)itemView.findViewById(R.id.txt_time_slot_description);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
itemClickListener.onClick(v, getAdapterPosition(), false);
}
}
}
整个实用程序可能无法实现,但是我可以为您提供一个方法。您可以做的只是在用户单击任意插槽时更新字段isAvailable =“ false”。示例用户1拥有全部10个可用插槽,并且他当时选择了第一个插槽,则必须使用isAvailable =“ false”更新该节点,并且当用户2当时从firbase查询时,仅返回isAvailable值为true的那些结果。这样,您就无需处理时隙问题。