我正在创建一个包含具有 2 列的 ListView 的应用程序。在第一列应显示倒计时,在第二列显示附加文本,说明倒计时的用途。下面你会看到我的代码……或多或少。我有一个包含多行的列表视图,计时器正在滴答作响。一个问题是:我的可运行文件中的 set.Text() 似乎覆盖了所有行。例如,第 1 行的可运行将文本也设置为第 2 行和第 3 行,第 2 行的可运行也将文本设置为第 1 和第 3 行,依此类推。这具有第一列闪烁的效果(具有正确的值和其他行的值)。如何为列表视图中的特定行设置文本?
下一个问题:即使我从处理程序中删除回调,runnable 也会继续运行。但是当活动在后台或关闭时,不需要计时器计时,我不想浪费系统资源。
我的活动:
public class TimerActivity extends ListActivity {
MyTimerAdapter myTimerAdapter = null;
ArrayList<Long> timerList = new ArrayList<Long>();
ArrayList<String> textList = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listactivity);
myTimerAdapter = new MyTimerAdapter(this, R.layout.row, R.id.tv_timer, R.id.tv_text);
setListAdapter(myTimerAdapter);
}
@Override
protected void onResume() {
super.onResume();
refreshView();
}
@Override
protected void onPause() {
myTimerAdapter.clear();
myTimerAdapter.notifyDataSetChanged();
super.onPause();
}
private void refreshView() {
myTimerAdapter.clear();
timerList.clear();
textList.clear();
// some code to read database and fill
// array timerList with a long value (used for displaying a countdown)
// and textList with some additional text
myTimerAdapter.add(timerList, textList);
myTimerAdapter.notifyDataSetChanged();
}
}
我的适配器:
public class MyTimerAdapter extends ArrayAdapter<String> {
private Activity mContext;
private ArrayList<Long> mTimer;
private ArrayList<String> mText;
private int mViewId;
private int mViewIdFieldTimer;
private int mViewIdFieldText;
private int listSize;
private ArrayList<Handler> handlerList = new ArrayList<Handler>();
private ArrayList<TimerRunnable> runList = new ArrayList<TimerRunnable>();
public MyTimerAdapter(Activity context, int textViewResId, int tv1, int tv2) {
super(context, textViewResId);
mContext = context;
mViewId = textViewResId;
mViewIdFieldTimer = tv1;
mViewIdFieldText = tv2;
listSize = 0;
}
public void add(ArrayList<Long> timer, ArrayList<String> text) {
mTimer = timer;
mText = text;
listSize = mText.size();
handlerList.clear();
runList.clear();
}
@Override
public void clear() {
super.clear();
int i;
for (i=0; i<listSize; i++) {
handlerList.get(i).removeCallbacksAndMessages(runList.get(i));
runList.get(i).stopHandler();
}
}
@Override
public int getCount() {
return listSize;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = mContext.getLayoutInflater();
v = vi.inflate(mViewId, null);
}
long timerLine = mTimer.get(position);
if (timerLine != 0) {
TextView tvTimer = (TextView) v.findViewById(mViewIdFieldTimer);
if (tvTimer != null) {
tvTimer.setTag(position);
final Handler mTimerHandler = new Handler();
TimerRunnable timerTask = new TimerRunnable(tvTimer, tvTimer.getTag().toString(), timerLine);
mTimerHandler.post(timerTask);
// save in array to stop later
handlerList.add(mTimerHandler);
runList.add(timerTask);
}
}
String textLine = mText.get(position);
if (textLine != null) {
TextView tvText = (TextView) v.findViewById(mViewIdFieldText);
if (tvText != null) {
tvText.setText(textLine);
}
}
return v;
}
}
我的可运行:
public class TimerRunnable implements Runnable {
private TextView tv;
final Handler mTimerHandler = new Handler();
String tag;
long endtime;
long sec;
public TimerRunnable (TextView tv, String tag, long endtime) {
this.tv = tv;
this.tag = tag;
this.endtime = endtime;
}
public void run() {
if (tv.getTag().toString().equals(tag)) {
Calendar cal = Calendar.getInstance();
sec = endtime - (cal.getTimeInMillis() / 1000); //endtime - aktuelle Zeit
if (sec >= 0) {
// some code formatting the time in seconds to something like hh:mm:ss (var String txt)
tv.setText(txt);
System.out.println(txt); // only for tests; so I could see that runnable is still running
mTimerHandler.postDelayed(this, 1000);
}
}
}
public void stopHandler() {
mTimerHandler.removeCallbacksAndMessages(null);
}
}