问题
I have to disable already booking slot and show only available slot to user. in recycler view time will be visible from 09:00Am to 09:00Pm. already booked slot should be in disable mode and user can select only available slots.
in main activity im storing all the time slot from 09:00AM to 09:00PM and pass into adapter.
here in adapter class i want to fetch booked slot from firebase and compare those slots in time arraylist object.
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 model class
public class TimeSlot {
public String time;
public String isAvailable;
}
TimeAdapter
In adapter class it should fetch already bookedslot and compare with arraylist. if already booked slot is equal to arraylist time then it should be disable mode other slots should be enable mode
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);
}
}
}
回答1:
Entire practical might not possible but i can give you the idea how you can do this. What you can do is just update the field isAvailable="false" when user click on any slot. example user 1 have all 10 slot avaliable and he selected the first one at that time you have to update that node with isAvailable="false" and when user 2 came at that time from firbase query only those result where value of isAvailable is true. With this way you don't need to deal with time slot stuff.
来源:https://stackoverflow.com/questions/59598156/how-to-disable-already-booking-slot-in-android