我必须禁用已经预订的时段并仅向用户显示可用的时段。
在回收站视图中,时间从 09:00Am 到 09:00Pm 可见。已预订的时段应处于禁用模式,并且用户只能选择可用的时段。
在主要活动中,我存储从 09:00AM 到 09:00PM 的所有时间段并传递到适配器中。
在适配器类中,我想从 firebase 获取预订的插槽,并在时间 arraylist 对象中比较这些插槽。
获取小时数组()
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;
}
时隙模型类
public class TimeSlot {
public String time;
public String isAvailable;
}
时间适配器
在适配器类中,它应该获取已经预订的插槽并与数组列表进行比较。如果已经预订的插槽等于 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);
}
}
}