Search code examples
javaandroiddynamic-arraysjava-timeandroid-timepicker

How to get time in slot type in android


I am trying to get time in slot type. for example bike shop is open 9am to 9pm so we can get the bike in that time. if anyone books a bike in evening 5 pm to 7 pm slot then it should show 9am to 5pm (available), 5pm to 7pm (not available) and 7pm to 9pm (available) slots.

so that another user who wants to book same bike they can understand the bike is not available for this slot and he can book on only available slots.

me how to achieve this?

enter image description here


Solution

  • A model class to hold data

    class TimeSlot {
    String time;
    boolean isAvailable;
    } 
    

    Custom adapter to change content of gridview

    public class CustomListAdapter extends BaseAdapter {
        private  ArrayList<TimeSlot> list;
    
        private LayoutInflater mInflater;
        Context con;
    
        public CustomListAdapter(Context context, ArrayList<TimeSlot> list) {
            this.list = list;
    this.con = context;
            mInflater = LayoutInflater.from(context);
        }
    
        public int getCount() {
            return list.size();        
        }
    
        public Object getItem(int position) {
            return list.get(position);
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            convertView = new TextView(con);
    //set layout params or inflate xml layout
            convertView.setText(list.get(position).time);
            convertView.setEnabled(list.get(position).isAvailable);
            return convertView;
        }
    }
    

    Main activity

        List<TimeSolt> slots ;
        //assign the data from network or local...
    
        //create gridview from xml ...
    
        gridView.setColumnCount(3);
    
        //other properties goes here..
    
        //create custom adapter
    
        adapter = new CustomAdapter(this, slots);
    
        //other properties like onitemclick....
    
    
    
    gridView.setAdapter(adapter);
    
                //create gridview from xml ...
            calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
                        @Override
                        public void onSelectedDayChange(@NonNull CalendarView calendarView, int year, int month, int date) {
            //change the data of slots  according to date 
        //slots = newData; and call 
        adapter.notifyDataSetChanged();
    
                        }
                    });