3

我正在创建一个包含具有 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);
}
}
4

2 回答 2

5

我会这样做:

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

    • 只需adapter.notifyDatasetChanged()从您的计时器调用。但这可能会导致“闪烁”的列表视图。但是,您可以尝试检查这是否可以在您的应用程序中使用,因为这是最简单的实现。
    • 直接更新当前在 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());
      
      
          }
      
      }
      

      }

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

于 2013-10-06T22:29:28.367 回答
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;
}

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

于 2013-10-14T19:36:10.490 回答