m.v.n.kalyani
m.v.n.kalyani

Reputation: 770

How to run the timer in Listview?

I unable to run the timer in Listview selected item.Timer is always running on Listview last item only.Suppose if i click the second item on list view i have to run the timer in textview of second item only.But i am always getting the timer at last item.But i am getting position properly.Problem is in timer thread only.can you please tell how to run the timer on particular clicked item? Here is my code

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
  if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null)
            convertView = inflater.inflate(R.layout.activity_schedule, null);
        punchin = (Button) convertView.findViewById(R.id.punchin);



        resultp = data.get(position);
         title = (TextView) convertView.findViewById(R.id.title1);
        TextView location = (TextView) convertView.findViewById(R.id.location);
        TextView sheduledate = (TextView) convertView.findViewById(R.id.sheduledate);
        TextView starttime = (TextView) convertView.findViewById(R.id.startendtime);


        totalworked = (TextView) convertView.findViewById(R.id.totaltimeworked);
 if(buttonclicked.equals("1")){


            startTime = SystemClock.uptimeMillis();
              //  customHandler.postDelayed(updateTimerThread, 0);
            customHandler.postDelayed(updateTimerThread,0);




        }else{

            totalworked.setText(resultp.get(MyScheduleFragment.TAG_PUNCDURATION));


        }

}

and my runnable timer code is:

public  Runnable updateTimerThread = new Runnable() {
            //Thread mUpdate = new Thread() {



            public void run() {



                timeInMilliseconds = SystemClock.uptimeMillis() - startTime;




                updatedTime = timeSwapBuff + timeInMilliseconds;

                int secs = (int) (updatedTime / 1000);
                int mins = secs / 60;
                int hrs = mins / 60;
                secs = secs % 60;
                //  int milliseconds = (int) (updatedTime % 1000);



   totalworked.setText("" + String.format("%02d", hrs) + ":"
            + String.format("%02d", mins)
            + ":"
            + String.format("%02d", secs));
}

Upvotes: 2

Views: 191

Answers (1)

Ramesh Kanuganti
Ramesh Kanuganti

Reputation: 285

Hope this might help        

 @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
          if (inflater == null)
                    inflater = (LayoutInflater) activity
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                if (convertView == null)
                    convertView = inflater.inflate(R.layout.activity_schedule, null);
                punchin = (Button) convertView.findViewById(R.id.punchin);

                resultp = data.get(position);
                 title = (TextView) convertView.findViewById(R.id.title1);
                TextView location = (TextView) convertView.findViewById(R.id.location);
                TextView sheduledate = (TextView) convertView.findViewById(R.id.sheduledate);
                TextView starttime = (TextView) convertView.findViewById(R.id.startendtime);
                totalworked = (TextView) convertView.findViewById(R.id.totaltimeworked);
    // Add Click Listener to the TextView 
    totalworked.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                // write your Stuff when he cicks

                    startTime = SystemClock.uptimeMillis();
                    //  customHandler.postDelayed(updateTimerThread, 0);
                    customHandler.postDelayed(updateTimerThread, 0);
                }
            });
         // code if he dont click on particular Item

Upvotes: 3

Related Questions