比较时间与移动时间

时间:2017-05-11 05:27:47

标签: android listview

我的listview中有两个时间和一个按钮。它们之间的差异是10分钟。我想要的是当我的移动时间到达新时间时按钮必须可点击直到那时它无法点击。以下是我的代码 -

SecondCustom.java

public class SecondCustom extends BaseAdapter {
    Activity a;
    ArrayList<Model> timelist;
    Date newtime;
    Time itime,canceltime;


    public SecondCustom(Activity a, ArrayList<Model> timelist) {
        this.a = a;
        this.timelist= timelist;
    }

    @Override
    public int getCount() {
        return timelist.size();
    }

    @Override
    public Object getItem(int i) {
        return timelist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }



    public class Viewholder{
        TextView srno, ctime, ltime;
        Button buy;
    }

    @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {
        Viewholder viewholder=null;
        if (convertView == null) {
            viewholder = new Viewholder();
            LayoutInflater li = a.getLayoutInflater();
            convertView = li.inflate(R.layout.secondcustom, viewGroup, false);

            viewholder.srno = (TextView) convertView.findViewById(R.id.sr);
            viewholder.ctime = (TextView) convertView.findViewById(R.id.currenttime);



            viewholder.buy = (Button) convertView.findViewById(R.id.st);
            viewholder.ltime = (TextView) convertView.findViewById(R.id.lefttime);
            convertView.setTag(viewholder);
        }
        else {
            viewholder = (Viewholder) convertView.getTag();
        }


        Model model=timelist.get(i);
        viewholder.srno.setText(valueOf(timelist.get(i).getSr()));



        String Time = DateFormat.getTimeInstance().format(new Date());
        viewholder.ctime.setText(Time);

     Calendar cal = Calendar.getInstance();
        SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss a");

        cal.add(Calendar.MINUTE,10); 
     String FutTime= sd.format(cal.getTime());
        viewholder.ltime.setText(FutTime);


        viewholder.buy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(a, "Purchased" + " " + flowerlist.get(i).getSr(), Toast.LENGTH_LONG).show();
                }
            });

        return convertView;
    }
}

secondcustom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="50dp"
        android:layout_height="40dp"
        android:id="@+id/sr"
        android:textSize="15dp"
        android:textAlignment="center"/>

    <TextView
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:id="@+id/currenttime"
        android:textSize="15dp"
        android:textAlignment="center"/>

    <Button
        android:layout_width="70dp"
        android:layout_height="40dp"
        android:id="@+id/st"
        android:text="Buy"
        android:textAllCaps="false"
        android:textSize="15dp"
        android:background="#57ba58"
        android:textAlignment="center"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"/>

    <TextView
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:id="@+id/lefttime"
        android:textSize="15dp"
        android:textAlignment="center"/>


</LinearLayout>

3 个答案:

答案 0 :(得分:0)

使用处理程序在10分钟后点击按钮。

伪代码应如下所示 -

new Handler().postDelayed(new Runnable() {
              public void run() {
               viewholder.buy.setclickable(true);

            }
        }, 10*60*1000);

答案 1 :(得分:0)

new CountDownTimer(30000, 1000) {

    public void onTick(long millisUntilFinished) {
        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
       //here you can have your logic to set text to edittext
    }

    public void onFinish() {
        mTextField.setText("done!");
    }

}.start();

根据您的要求使用countDownTimer。当你的计时器完成后,它将给onFinish()事件。在那你可以执行你的操作。

答案 2 :(得分:0)

倒计时器可能会解决您的问题。设置sleepTime 10秒的倒数计时器

viewholder.buy.setclickable(false);
mCountDownTimer = new CountDownTimer(sleepTime, 1000) {

                public void onTick(long millisUntilFinished) {


                    String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
                            TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
                            TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
                    txtTimer.setText(hms);//display the time 
                }
                public void onFinish() {
                    txtTimer.setText("00:00:00");
                    viewholder.buy.setclickable(true);

                }
            }.start();