How to get time in slot type in android

淺唱寂寞╮ 提交于 2020-01-24 19:34:19

问题


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?


回答1:


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();

                    }
                });


来源:https://stackoverflow.com/questions/59573807/how-to-get-time-in-slot-type-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!