如何在Android中禁用已经预订的插槽 [英] How to disable already booking slot in android

查看:68
本文介绍了如何在Android中禁用已经预订的插槽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须禁用已经预订的广告位,并仅向用户显示可用的广告位. 在回收者视图中,时间从09:00Am到09:00Pm可见.已经预订的插槽应处于禁用模式,用户只能选择可用的插槽.

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.

在主要活动中,它存储了从09:00 AM到09:00 PM的所有时间段,并传递给适配器.

in main activity im storing all the time slot from 09:00AM to 09:00PM and pass into adapter.

在适配器类中,我想从firebase中获取预订的插槽,并在time arraylist对象中比较这些插槽.

here in adapter class i want to fetch booked slot from firebase and compare those slots in time arraylist object.

getHourarray()

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模型类

TimeSlot model class

 public class TimeSlot {

public String time;
public String isAvailable;

}

TimeAdapter

TimeAdapter

在适配器类中,它应该获取已经预订的插槽并与arraylist进行比较.如果已预订的插槽等于arraylist时间,则应为禁用模式,其他插槽应为启用模式

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

    }

}

 }

推荐答案

不可能完全实用,但我可以为您提供解决方法.您可以做的就是,当用户单击任何插槽时,只需更新字段isAvailable ="false".例如,用户1拥有全部10个可用插槽,那么他当时选择了第一个插槽,则必须使用isAvailable ="false"更新该节点,而当用户2当时从firbase查询时,仅返回isAvailable值为true的那些结果.这样,您就无需处理时隙问题.

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.

这篇关于如何在Android中禁用已经预订的插槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆