我是android新手。我正在创建一个竞赛应用程序,其中包含一个包含该事件状态的ListView
。无论是否开始。当前时间超过事件开始时间时应自动更新。如何根据当前时间和事件开始时间更新listItem的textview
我使用的是AsyncTask
,但它不是很准确。
@Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater scheduleInflater = LayoutInflater.from(getContext());
final Holder h;
if(view == null){
view = scheduleInflater.inflate(R.layout.custom_list , parent , false);
h = new Holder();
h.eventName=(TextView) view.findViewById(R.id.eventNameCustomList);
h.eventStatus=(TextView) view.findViewById(R.id.eventStatus);
h.leftTime = (TextView) view.findViewById(R.id.leftTime);
h.rightTime= (TextView) view.findViewById(R.id.rightTime);
view.setTag(h);
}else{
h = (Holder) view.getTag();
}
final data d = getItem(position);
h.eventName.setText(d.getEventName());
Date startDate= d.getStartDate();
Date endDate = d.getEndDate();
long stTime = startDate.getTime();
long now = System.currentTimeMillis();
if(now >= stTime) {h.eventStatus.setText(Html.fromHtml(startedText));}
else {h.eventStatus.setText(Html.fromHtml(upcomingText));}
h.eventStatus.setTag(String.valueOf(position)); // Setting Tag of each TextView to its position to avoid incorrect ayncTask update
new go(h.eventStatus).execute(d.getStartDate()); // starting a thread to update the Text view
return view;
}
public class go extends AsyncTask<Date, Void , Boolean> {
String path;
TextView status;
public go(TextView eventStatus){
this.status=eventStatus;
this.path=eventStatus.getTag().toString();
}
@Override
protected Boolean doInBackground(Date... params) {
Date startDate = params[0];
while(true){
long curTime = System.currentTimeMillis();
long startTime = startDate.getTime();
if (curTime >= startTime) {return true;}
}
}
@Override
protected void onPostExecute(Boolean b) {
if(!status.getTag().toString().equals(path)) return;
if(b!=null && status!=null){
if(b==true) status.setText(Html.fromHtml(startedText));
else status.setText(Html.fromHtml(upcomingText));
}else{
status.setText(Html.fromHtml(upcomingText));
}
}
}
static class Holder{
TextView eventName , eventStatus;
TextView leftTime, rightTime;
}
如何更新在当前时间超过开始时间TextView
来启动。请帮助并提出更好的解决方案。
'其中包含event'的状态做你的意思是listview显示的是事件列表? '但它不是很准确'请在这里定义准确。另外,柜台是否继续滴答,例如20分钟等等? – Sufian
Listview有许多事件,它在每个显示状态的项目中都有一个textview。状态:即将到来或状态:已启动。 Asynctask让我的应用程序更慢 – Arun