【发布时间】:2018-01-27 07:24:30
【问题描述】:
我正在开发一个应用程序。带有计时器的每个网格中显示的令牌编号。我有 5 个状态 1.未启动、2.启动、3.暂停、4.停止、5.取消任何时候我可以通过广播接收器启动、暂停和停止计时器,我使用 notifydatasetchange() 来刷新网格。一旦我开始,暂停和停止计时器在有时(15 分钟)运行计时器秒后工作正常,会跳过 3 到 5 秒。任何人都可以指导我,无论我是做对的还是任何错误都可以帮助我。在此先感谢适配器,类和布局下方的附加代码段
public class CountdownAdapter extends ArrayAdapter<Orders> {
private LayoutInflater lf;
public Context ctx;
private List<Runnable> updateTimerThread;
public CountdownAdapter(Context context, List<Orders> objects) {
super(context, 0, objects);
ctx = context;
lf = LayoutInflater.from(context);
updateTimerThread = new ArrayList<Runnable>();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = lf.inflate(R.layout.list_item3, parent, false);
holder.status = (TextView) convertView.findViewById(R.id.status);
holder.tvProduct = (TextView) convertView.findViewById(R.id.tvProduct);
holder.tvRunningTime = (TextView) convertView.findViewById(R.id.tvTimeRemaining);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ViewHolder finalHolder = holder;
Runnable singlTimerThread = new Runnable() {
public void run() {
getItem(position).setTimeInMilliseconds(SystemClock.uptimeMillis() - getItem(position).getStartTime());
getItem(position).setUpdatedTime(getItem(position).getTimeSwapBuff() + getItem(position).getTimeInMilliseconds());
int secs = (int) (getItem(position).getUpdatedTime() / 1000);
int mins = secs / 60;
secs = secs % 60;
finalHolder.tvRunningTime.setText("" + mins + ":" + String.format("%02d", secs));// + ":"+ String.format("%03d", milliseconds));
getItem(position).setRate((long) (mins * getItem(position).getIntervaltime()));
MeterActivity.mHandler.postDelayed(this, 1000);
}
};
holder.tvProduct.setText("Token : " + getItem(position).getTableno());
updateTimerThread.add(singlTimerThread);
//0 means not started
if (getItem(position).getStatus() == 0) {
holder.status.setText("Status : Not Started");
}
//1 means intialized
else if (getItem(position).getStatus() == 1) {
holder.status.setText("Status : Ready to Start");
}
//2 means start
else if (getItem(position).getStatus() == 2) {
getItem(position).setStartTime(SystemClock.uptimeMillis() - getItem(position).getUpdatedTime());
MeterActivity.mHandler.postDelayed(updateTimerThread.get(position + 1), 1000);
holder.status.setText("Status : Started");
}
//3 means pause
else if (getItem(position).getStatus() == 3) {
holder.status.setText("Status : Paused");
MeterActivity.mHandler.removeCallbacks(updateTimerThread.get(position + 1));
}
//4 means stop
else if (getItem(position).getStatus() == 4) {
MeterActivity.mHandler.removeCallbacks(updateTimerThread.get(position + 1));
holder.status.setText("Status : Stopped");
}
return convertView;
}
class ViewHolder {
TextView tvProduct, status;
TextView tvRunningTime;
}}
`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#565555"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvProduct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:paddingRight="10dp"
android:gravity="right|center"
android:background="@color/gray"
android:textColor="#203020"
android:text="Token"
android:visibility="visible"
android:layout_gravity="right"
android:textStyle="bold"
android:textSize="15sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center"
android:textStyle="bold|italic"
android:text="Status"
android:id="@+id/status"
android:padding="5dp"
android:textColor="@color/white"
android:textSize="13sp" /><FrameLayout
android:layout_width="match_parent"
android:layout_height="85dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/cardbackground"
android:elevation="4dp"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:background="@color/cardbackground"
android:id="@+id/itemlayout"
android:layout_height="wrap_content">
<View
android:layout_width="match_parent"
android:layout_height="2dp"/>
<TextView
android:id="@+id/tvTimeRemaining"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:textSize="40sp"
android:layout_margin="5dp"
android:textStyle="bold|italic"
android:textColor="@color/redglow"
android:gravity="center"
android:text="00:00:00 " />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
`
我也参考了下面的stackoverflow链接 How to handle multiple countdown timers in ListView?
【问题讨论】:
标签: android gridview handler runnable countdowntimer