首页 > 解决方案 > ListView 中的多个秒表计时器在删除/添加时重置

问题描述

我正在使用 ListView 制作一个多秒表 android 应用程序,我想从 ListView 添加/删除秒表,而不影响之前添加的秒表“倒计时”。但是,每次我添加/删除时,它都会将 ListView 中的前一个秒表活动重置为 0。下面是我的主要活动的代码:

public class MainActivity extends AppCompatActivity {

    ListView watchList;
    ArrayList<Stopwatch> stopWatches = new ArrayList<>();
    StopwatchListAdapter sAdapter;
    Stopwatch watch = new Stopwatch();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FloatingActionButton addB = (FloatingActionButton) findViewById(R.id.addWatch);
        watchList = (ListView)findViewById(R.id.stopList);

        sAdapter = new StopwatchListAdapter(this, R.layout.stop_watch, stopWatches);
        watchList.setAdapter(sAdapter);

    }
    public void addItems(View v) {
        sAdapter.add(watch);
    }

}

And below is the code for my adapter:

    public class StopwatchListAdapter extends ArrayAdapter<Stopwatch>{

    private Context sContext;
    private int sResource;
    ArrayList<Stopwatch> stopwatches;

    public StopwatchListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Stopwatch> objects) {
        super(context, resource, objects);
        this.sContext = context;
        this.sResource = resource;
        this.stopwatches = objects;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

       LayoutInflater inflater = LayoutInflater.from(sContext);
       convertView = inflater.inflate(sResource, parent, false);

        Stopwatch watch = new Stopwatch();
        Chronometer chronometer = convertView.findViewById(R.id.chron);
        watch.setChronometer(chronometer);


       Button start = (Button)convertView.findViewById(R.id.start);
       Button pause = (Button)convertView.findViewById(R.id.pause);
       Button reset = (Button)convertView.findViewById(R.id.reset);
       Button remove = (Button)convertView.findViewById(R.id.remove);

       start.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
              watch.startChronometer(view);
           }
       });
      pause.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               watch.pauseChronometer(view);
           }
       });
      reset.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               watch.resetChronometer(view);
           }
       });
      remove.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              stopwatches.remove(position);
              notifyDataSetChanged();
          }
      });

        return convertView;
    }

}

My stopwatch class is below:

    public class Stopwatch {

    private boolean running = false;
    private Chronometer chronometer;
    private long pauseOffset;

    public void setChronometer(Chronometer chronometer) {
        this.chronometer = chronometer;
    }

    public void startChronometer(View v) {
        if(!running) {
            chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
            chronometer.start();
            running = true;
        }
    }
    public void pauseChronometer(View v) {
        if(running) {
            chronometer.stop();
            pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
            running = false;
        }
    }
    public void resetChronometer(View v) {
        chronometer.setBase(SystemClock.elapsedRealtime());
        pauseOffset = 0;
        chronometer.stop();
        running = false;
    }
    
}

标签: androidlistview

解决方案


推荐阅读