如何在Listview中运行计时器?

时间:2016-11-14 12:26:31

标签: android listview timer

我无法在Listview选择的项目中运行计时器.Timer始终在Listview最后一项上运行。如果我单击列表视图上的第二项,我必须在第二项的textview中运行计时器。但我是总是把计时器放在最后一个项目。但我正确地获得了位置。问题只在计时器线程中。你能告诉我如何在特定的点击项目上运行计时器吗? 这是我的代码

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


        }

}
  

我的可运行计时器代码是:

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

1 个答案:

答案 0 :(得分:2)

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