我无法在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));
}
最佳答案
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
关于android - 如何在Listview中运行计时器?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40588763/