// Here is my fragment
public class BookAppointmentFragment extends Fragment {
TextView timetv,dateTv;
Button bookAppoinmentBtn,bookingPopup;
RelativeLayout datepickerLayout;
Calendar myCalendar;
DatePickerDialog datePickerDialog;
String strStartDate,strSelectedDate;
List<Boolean> numberList;
List<String>timeSlotList;
RecyclerView recyclerView;
TimeSlotAdapter recyclerGridAdapter;
ProgressBar progressBar;
public BookAppointmentFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.nestedscroll, container, false);
numberList=new ArrayList<>();
progressBar=view.findViewById(R.id.progressBar);
bookAppoinmentBtn = view.findViewById(R.id.bookAppoinment);
timetv = view.findViewById(R.id.timeTv);
datepickerLayout = view.findViewById(R.id.datepickerLayout);
recyclerView = view.findViewById(R.id.recyclerView);
for(int i=0;i<=17;i++){
numberList.add(false);
}
details();
return view;
}
public void details() {
myCalendar = Calendar.getInstance();
datepickerLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
datepickerLayout.setBackgroundResource(R.drawable.green_button);
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(getContext(),new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String strdate=year+"-"+(monthOfYear+1)+"-"+dayOfMonth;
String dateFormat=formateDateFromstring("yyyy-MM-dd","EE dd, MMM yyyy",strdate);
strStartDate=date_after;
new GetContacts().execute();
}
}, yy, mm, dd);
datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
datePickerDialog.show();
}
});
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String currentStylistID=strStylistid;
String duration=stylistDetails2.getDuration().toString();
String url= "https://cloudfunctions.net/bokking?date="+strStartDate+"&duration="+duration+"¤tStylistID="+currentStylistID;
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if(jsonStr!=null){
try {
JSONObject jsonObj = new JSONObject(jsonStr);
String str1clk=jsonObj.getString("1");
String str2clk=jsonObj.getString("2");
String str3clk=jsonObj.getString("3");
String str4clk=jsonObj.getString("4");
String str5clk=jsonObj.getString("5");
String str6clk=jsonObj.getString("6");
String str7clk=jsonObj.getString("7");
String str8clk=jsonObj.getString("8");
String str9clk=jsonObj.getString("9");
String str10clk=jsonObj.getString("10");
String str11clk=jsonObj.getString("11");
String str12clk=jsonObj.getString("12");
String str13clk=jsonObj.getString("13");
String str14clk=jsonObj.getString("14");
String str15clk=jsonObj.getString("15");
String str16clk=jsonObj.getString("16");
String str17clk=jsonObj.getString("17");
String str18clk=jsonObj.getString("18");
timeSlotList=new ArrayList<>();
timeSlotList.add(str1clk);
timeSlotList.add(str2clk);
timeSlotList.add(str3clk);
timeSlotList.add(str4clk);
timeSlotList.add(str5clk);
timeSlotList.add(str6clk);
timeSlotList.add(str7clk);
timeSlotList.add(str8clk);
timeSlotList.add(str9clk);
timeSlotList.add(str10clk);
timeSlotList.add(str11clk);
timeSlotList.add(str12clk);
timeSlotList.add(str13clk);
timeSlotList.add(str14clk);
timeSlotList.add(str15clk);
timeSlotList.add(str16clk);
timeSlotList.add(str17clk);
timeSlotList.add(str18clk);
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
int numberOfColumns = 3;
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), numberOfColumns));
recyclerGridAdapter = new TimeSlotAdapter(getContext(), timeSlotList,strSelectedDate,numberList,timetv);
recyclerView.setAdapter(recyclerGridAdapter);
progressBar.setVisibility(View.GONE);
}
}
}
// here is my recycler adapter
public class TimeSlotAdapter extends RecyclerView.Adapter {
private LayoutInflater mInflater;
private String strSelectedDate;
private List<Boolean> numberList;
private List<String> mData;
private TextView timetv;
public TimeSlotAdapter(Context context, List<String> data, String strSelectedDate, List<Boolean> numberList, TextView timetv) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
this.strSelectedDate = strSelectedDate;
this.numberList = numberList;
this.timetv=timetv;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
return new listViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
// to get current date
SimpleDateFormat currentDate = new SimpleDateFormat("dd/MM/yyyy");
Date todayDate = new Date();
String thisDate = currentDate.format(todayDate);
Log.d("date", thisDate);
if (thisDate.equals(strSelectedDate)) {
// to get current time
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
Date currentLocalTime = cal.getTime();
String strTime = currentLocalTime.toString();
String strCurrenttime;
strCurrenttime = formateDateFromstring("EEEE MMM dd hh:mm:ss Z yyyy", "HH", strTime);
if (strCurrenttime.contains("00")) {
strCurrenttime = formateDateFromstring("EEEE MMM dd hh:mm:ss Z yyyy", "hh", strTime);
}
String strCurrentmin = formateDateFromstring("EEEE MMM dd hh:mm:ss Z yyyy", "mm", strTime);
Integer currentHour = Integer.parseInt(strCurrenttime);
Integer currentMin = Integer.parseInt(strCurrentmin);
String strCurrentTimeMin;
if (currentMin < 30) {
currentMin = 00;
String strmin = currentMin.toString();
strmin.concat("00");
strCurrentmin = strmin;
strCurrentTimeMin = strCurrenttime.concat(":").concat(strCurrentmin);
// currentMin=Integer.parseInt(strCurrentmin);
} else {
currentMin = 30;
String strmin = currentMin.toString();
strCurrentmin = strmin;
strCurrentTimeMin = strCurrenttime.concat(":").concat(strCurrentmin);
// currentMin=Integer.parseInt(strCurrentmin);
}
if (mData.get(position).equals("")) {
numberList.set(position, true);
((listViewHolder) holder).timeslot.setBackgroundResource(R.drawable.inactive_slots);
((listViewHolder) holder).radioButton.setClickable(false);
} else {
String freeBusyHour = formateDateFromstring("HH:mm", "HH", mData.get(position));
Integer hour = Integer.parseInt(freeBusyHour);
if (hour < currentHour) {
numberList.set(position, true);
((listViewHolder) holder).radioButton.setText(mData.get(position));
((listViewHolder) holder).timeslot.setBackgroundResource(R.drawable.inactive_slots);
((listViewHolder) holder).radioButton.setClickable(false);
} else {
((listViewHolder) holder).radioButton.setTextColor(Color.WHITE);
((listViewHolder) holder).radioButton.setText(mData.get(position));
((listViewHolder) holder).timeslot.setBackgroundResource(R.drawable.active_slots);
}
if (strCurrentTimeMin.equals(mData.get(position))) {
((listViewHolder) holder).radioButton.setText(mData.get(position));
(((listViewHolder) holder).timeslot).setBackgroundResource(R.drawable.active_slots);
}
}
} else {
if (mData.get(position).equals("")) {
((listViewHolder) holder).radioButton.setText(mData.get(position));
((listViewHolder) holder).timeslot.setBackgroundResource(R.drawable.inactive_slots);
} else {
((listViewHolder) holder).radioButton.setText(mData.get(position));
((listViewHolder) holder).radioButton.setTextColor(Color.WHITE);
((listViewHolder) holder).timeslot.setBackgroundResource(R.drawable.active_slots);
}
}
}
@Override
public int getItemCount() {
return mData.size();
}
private class listViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView radioButton;
RelativeLayout timeslot;
String strStartTime;
public listViewHolder(View itemView) {
super(itemView);
radioButton = itemView.findViewById(R.id.radioBtton);
timeslot = itemView.findViewById(R.id.mslot1);
timeslot.setOnClickListener(this);
}
public void onClick(View view) {
if(numberList.get(getAdapterPosition()).equals(true)){
Toast.makeText(itemView.getContext(),"Grey timeslot's are already booked please choose some other timeslot",Toast.LENGTH_LONG).show();
}else {
timeslot.setBackgroundResource(R.drawable.green_button);
strStartTime=radioButton.getText().toString();
timetv.setText(strStartTime);
}
}
}
}
I am using gridlayout manager in recyclerview after selecting the date from datepicker i am passing some parameters through firebase cloud function.The response which i am getting from cloud function is string.
I will populate all the strings in grid if any of the string is like("") i should disable(grid cell in grey color) that grid cell and the other grid cell should be active(grid cell color is light blue).Now my issue is when i select the grid cell which is active i have to highlight that cell.
Here i can able to select only active grid cells and it should be single select.when selecting active grid other active grid cells should be in someother color selected grid cell should be in different color.
Also if the user again selects the date from datepicker i have to refresh the grid and i have to follow the same process.
Please help me how to achieve this i am messing up with selecting the grid cells.