1

There are 3 textviews and 1 button in my listview.One of textview contains time.I want button must get clicked after 20mins of intime.I want to compare Intime with current time and if difference >20 mins then button must get clicked.Following is my code-

MainActivity.java

public class MainActivity extends Activity  {

    TextView txtno,txtname,txtintime;
    ListView listView;
    ArrayList<Model> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtno=(TextView)findViewById(R.id.no);
        txtname=(TextView)findViewById(R.id.name);
        txtintime=(TextView)findViewById(R.id.intime);

        listView = (ListView) findViewById(R.id.list);
        data=new ArrayList<>();

        data.add(new Model(1,"ABC","11:45:50 AM"));
        data.add(new Model(2,"PQR","11:50:50 AM"));
        data.add(new Model(1,"XyZ","12:45:50 PM"));
        data.add(new Model(1,"SHjhsj","04:45:50 PM"));

        Custom c=new Custom(this,data);
        listView.setAdapter(c);
    }

}

Model.java

public class Model {

    int no;
    String name,intime;

    public Model(int no, String name, String intime) {
        this.no = no;
        this.name = name;
        this.intime = intime;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getIntime() {
        return intime;
    }

    public void setIntime(String intime) {
        this.intime = intime;
    }
}

Custom.java

public class Custom extends BaseAdapter {
    Activity a;
    ArrayList<Model> data;

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


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

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

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


    public class Viewholder {
        TextView srno, name, intime;
        Button end;
    }

    @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.list_item, viewGroup, false);
            viewholder.srno = (TextView) convertView.findViewById(R.id.cno);
            viewholder.name = (TextView) convertView.findViewById(R.id.cname);
            viewholder.intime = (TextView) convertView.findViewById(R.id.ctime);
            viewholder.end = (Button) convertView.findViewById(R.id.cend);
            convertView.setTag(viewholder);
        }else {
            viewholder=(Viewholder)convertView.getTag();
        }

        final Model model = data.get(i);
        viewholder.srno.setText(valueOf(data.get(i).getNo()));
        viewholder.name.setText(valueOf(data.get(i).getName()));
        viewholder.intime.setText(valueOf(data.get(i).getIntime()));



                viewholder.end.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(a, data.get(i).getName()+" Exited", Toast.LENGTH_LONG).show();
                    }
                });



        return convertView;
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.abc.timer.MainActivity">


    <TextView
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:id="@+id/no"
        android:text="CustNo"
        android:textSize="20dp"
        android:textColor="#000"/>

    <TextView
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:id="@+id/name"
        android:layout_toRightOf="@+id/no"
        android:text="Name"
        android:textSize="20dp"
        android:textColor="#000"/>

    <TextView
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:id="@+id/intime"
        android:layout_toRightOf="@+id/name"
        android:text="InTime"
        android:textSize="20dp"
        android:textColor="#000"/>

    <TextView
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:id="@+id/exit"
        android:text="Exit"
        android:layout_toRightOf="@+id/intime"
        android:textSize="20dp"
        android:textColor="#000"/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_below="@+id/no"
        android:id="@+id/list"
        ></ListView>


</RelativeLayout>

list_item.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="70dp"
        android:layout_height="match_parent"
        android:id="@+id/cno"
        android:gravity="center"/>

    <TextView
        android:layout_width="70dp"
        android:layout_height="match_parent"
        android:id="@+id/cname"
        android:gravity="center"/>

    <TextView
        android:layout_width="80dp"
        android:layout_height="match_parent"
        android:id="@+id/ctime"
        android:gravity="center"/>

  <Button
      android:layout_width="90dp"
      android:layout_height="40dp"
      android:text="End"
      android:id="@+id/cend"
      android:background="#c14c4a"
      android:gravity="center"/>

</LinearLayout>
Neha
  • 15
  • 5

2 Answers2

0

Use an AlarmManager with your specified time and the moment alarm will trigger you can enable the button, hence enabling the click event for the button.

Edit:

Either you can go ahead with handler object and use handler.postDelayed() method or there itself you can create a Timer Object and schedule the timer task within that object and do the stuff.

Saurabh7474
  • 450
  • 5
  • 12
0

Assuming that u know how to convert the time strings into timestamps,modify ur adapter like this:

 public class Custom extends BaseAdapter {
        Activity a;
        ArrayList<Model> data;

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


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

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

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


        public class Viewholder {
            TextView srno, name, intime;
            Button end;
        }

        @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.list_item, viewGroup, false);
                viewholder.srno = (TextView) convertView.findViewById(R.id.cno);
                viewholder.name = (TextView) convertView.findViewById(R.id.cname);
                viewholder.intime = (TextView) convertView.findViewById(R.id.ctime);
                viewholder.end = (Button) convertView.findViewById(R.id.cend);
                convertView.setTag(viewholder);
            }else {
                viewholder=(Viewholder)convertView.getTag();
            }

            final Model model = data.get(i);
            viewholder.srno.setText(valueOf(data.get(i).getNo()));
            viewholder.name.setText(valueOf(data.get(i).getName()));
            viewholder.intime.setText(valueOf(data.get(i).getIntime()));
final Runnable r = new Runnable() {
    public void run() {
       viewholder.end.setEnabled(true);
    }
};
               if(yourObjectTimestamp - currentTimeStamp > 20){ //this u need to do on ur own
                   viewholder.end.setEnabled(true); // I assumed u know how to convert times to timestamps
    }else{
     viewholder.end.setEnabled(false);
   long timeDiffInMillis = yourObjectTimestamp - currentTimeStamp;
     handler.postDelayed(r,timeDiffInMillis );
    }


                    viewholder.end.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(a, data.get(i).getName()+" Exited", Toast.LENGTH_LONG).show();
                        }
                    });



            return convertView;
        }
    }
Avinash Roy
  • 953
  • 1
  • 8
  • 25
  • see how to convert timestamps to hours,minutes and seconds http://stackoverflow.com/questions/11357945/java-convert-seconds-into-day-hour-minute-and-seconds-using-timeunit – Avinash Roy May 16 '17 at 07:02
  • Once your view is created and passed through your timestamp validation check, later on how will it enable the button upon time lapse ? – Saurabh7474 May 16 '17 at 07:09
  • I dont know how to convert but will see how to convert it on Net.Thanks :) – Neha May 16 '17 at 07:11
  • This code will not be able to enable the button upon time lapse. Roy- can you please update the answer accordingly ? – Saurabh7474 May 16 '17 at 07:14
  • What is yourObjectTimestamp and currentTimestamp? Where it must be converted in my Mainpage or Custom page – Neha May 16 '17 at 07:21
  • Object timestamp is the one which u get from the Model Object and the current timestamp is the one which u get from System.getCurrentTimeMillis() – Avinash Roy May 16 '17 at 07:22
  • U can convert in the getView method of ur adapter itself – Avinash Roy May 16 '17 at 07:25
  • First extact ur time string which is in the format 11:45:50 HH:MM:SS And using that value convert into timestamp,u can find it in stackoverflow just surf a bit – Avinash Roy May 16 '17 at 07:28
  • Timestamp currentTimeStamp=new Timestamp(System.currentTimeMillis()); Date d=new Date(currentTimeStamp.getTime()); is this a way for currentTimestamp – Neha May 16 '17 at 07:31
  • currentTimeStamp u can get by one Line i.e: System.getCurrentTimeMillis(); – Avinash Roy May 16 '17 at 07:33
  • I Just edited my answer and the code will work provided u convert the object time in millis and current time also in millis – Avinash Roy May 16 '17 at 07:35
  • u can refer this link http://stackoverflow.com/questions/8826270/how-to-convert-hhmmss-sss-to-milliseconds – Avinash Roy May 16 '17 at 07:37