Android:具有处理程序和runnable的ListView中的多个计时器。 2个问题

时间:2013-10-06 20:02:58

标签: android listview timer handler runnable

我正在创建一个包含带有2列的ListView的应用。在第一列上应显示倒计时,在第二列上显示一个附加文本,用于说明倒计时的内容。 下面你看到我的代码有效......或多或少。 我有一个包含多行的列表视图,计时器正在滴答作响。 一个问题是: 我的runnable中的set.Text()似乎覆盖了所有行。例如。第1行的runnable也设置为第2行和第3行,第2行的runnable也设置为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;
}
}

My Runnable:

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

2 个答案:

答案 0 :(得分:5)

我会这样做:

  1. 使用计时器 (http://developer.android.com/reference/java/util/Timer.html)和 让它每秒钟或其他任何时间定期运行。
  2. 在Activities onCreate()中创建Timer对象,并在Activities onDestroy()中取消()更新计时器(或者更好地在onResume中启动它并在onPause()中取消它)。这就是如何避免内存泄漏的最简单方法,即使活动已关闭,您的计时器也在运行。
  3. 从正在运行的计时器更新ListView。有两个选项:

    • 只需从计时器中拨打adapter.notifyDatasetChanged()即可。但这可能导致眨眼&#34;列表显示。但是,您可以尝试检查这是否可以在您的应用程序中工作,因为这是最简单的实现。
    • 直接更新ListView中当前可见的TextView

      private void updateTime(){
      
      int firstVisibleItemIndex = listView.getFirstVisiblePosition();
      
      for (int i = 0; i < listView.getChildCount(); i++) {
          View v = listView.getChildAt(i);
      
              YourItem item = (YourItem)adapter
                      .getItem(firstVisibleItemIndex + i));
      
              ViewHolder vh = (ViewHolder) v.getTag();
                                  // Calculate the time somehow, i.e. call a methot on your data item
                                  vh.tvTimer.setText(item.getElapsedTime());
      
      
          }
      
      }
      

      }

  4. 所以我猜第二种选择是最好的。您可能想知道ViewHolder是什么。 ViewHolder是一种提高ListView性能的模式。 http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder 适配器的问题是,每次用户滚动时都会调用findViewById()。 findViewById()是一种昂贵的方法调用,因为它必须遍历所有视图子节点以找到具有给定id的节点。在您的简单适配器中,它可能不会产生太大影响(因为您只有一个子视图,TextView)。但ViewHolder是您应该在Adapter实现中始终使用的东西

答案 1 :(得分:1)

所以这里我的代码有变化:

主要活动:

public class TimerActivity extends ListActivity {

MyTimerAdapter myTimerAdapter = null;
ArrayList<Long> timerList = new ArrayList<Long>();
ArrayList<String> textList = new ArrayList<String>();
ViewHolder vh = new ViewHolder();
ListView lv = null;
View v = null;
Timer t = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listactivity);
}

@Override
protected void onResume() {
    super.onResume();

    vh.tvTimer = (TextView) findViewById(R.id.tv_timer);
    vh.tvText = (TextView) findViewById(R.id.tv_text);
    lv = (ListView) findViewById(android.R.id.list);
    lv.setTag(vh);
    t = new Timer();

    myTimerAdapter = new MyTimerAdapter(this, R.layout.timer_row, lv, vh);
    setListAdapter(myTimerAdapter);

    refreshView();
}

@Override
protected void onPause() {
    myTimerAdapter.clear();
    myTimerAdapter.notifyDataSetChanged();
    super.onPause();
}

@Override
protected void onDestroy() {
    t.cancel();
    super.onDestroy();
}

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

    updateTime();
}

private void updateTime() {

        t.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    long sec;
                    long timerLine;
                    Calendar cal;
                    ViewHolder rvh;
                    View v;

                    @Override
                    public void run() {
                        if (lv.getChildCount() > 0) {
                        for (int i = 0; i < lv.getChildCount(); i++) {
                            v = lv.getChildAt(i);
                            rvh = (ViewHolder) v.getTag();
                            timerLine = timerList.get(i);
                            cal = Calendar.getInstance();
                            sec = timerLine - (cal.getTimeInMillis() / 1000);
                            if (sec >= 0) {

                                // some code formatting the time in seconds to something like hh:mm:ss (var String txt)

                                rvh.tvTimer.setText(txt);
                            }
                        }
                        }
                    }

                });
            }

        }, 0, 1000);
}
}

适配器:

public class MyTimerAdapter extends ArrayAdapter<String> {
private Activity mContext;
private ArrayList<Long> mTimer;
private ArrayList<String> mText;
private int mViewId;
private ViewHolder mViewHolder;
private ListView mListView;
private int listSize;

public MyTimerAdapter(Activity context, int textViewResId, ListView lv, ViewHolder vh) {
    super(context, textViewResId);
    mContext = context;
    mViewId = textViewResId;
    mViewHolder = vh; //(ViewHolder) lv.getTag();
    mListView = lv;
    listSize = 0;
}

public void add(ArrayList<Long> timer, ArrayList<String> text) {
    mTimer = timer;
    mText = text;
    listSize = mText.size();
}

@Override
public void clear() {
    super.clear();
}

@Override
public int getCount() { // wird benötigt, wenn die Daten nicht schon im Konstruktor mitgeliefert werden. Ansonsten wird getView nicht aufgerufen!
    return listSize;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = mContext.getLayoutInflater();//LayoutInflater.from(mContext);
        v = vi.inflate(mViewId, null);
    }

    long timerLine = mTimer.get(position);
    if (timerLine != 0) {
        TextView tvTimer = mViewHolder.tvTimer;
        if (tvTimer != null) {
            tvTimer.setText("00:00:00");
        }
    }

    String textLine = mText.get(position);
    if (textLine != null) {
        TextView tvText = mViewHolder.tvText;
        if (tvText != null) {
            tvText.setText(textLine);
        }
    }

    return v;
}
}

ViewHolder类:

public class ViewHolder {
TextView tvTimer;
TextView tvText;
int position;
}

如上所述,这不起作用。我列表中的两行都是空的。