我对回收商的观点和服务有疑问。回收站视图的每一行都是计时器,我已经成功实现了这种结构。现在我的服务再次传递额外内容,以更新我的计时器,但它将它们带到每个示例视图持有者。我的意思是如果一个已经添加了两个计时器(例如一分钟和两分钟)我的两个计时器将开始闪烁。从两个计时器混乱都需要extas,倒计时看起来像(在每个计时器上)0:59 1:59 0:58 1:58所以这里是我的rv的代码。有没有办法传递特定项目的具体信息?请帮我解决这个问题
任务(模型类)
public class Task {
private String title;
private String time;
public String timeLeft;
public Task() {
}
public Task(String title, String description, String time) {
this.title = title;
this.time = time;
}
public String getTitle() {
return title;
}
public void setTitle(String name) {
this.title = name;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}}
我的适配器和视图持有者实现
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.MyViewHolder> {
private Context context;
private List<Task> taskList;
private int position;
private Task task;
private BroadcastReceiver broadcastReceiver;
private enum TimerStatus {
STARTED,
STOPPED
}
public TaskAdapter(List<Task> taskList, Context context) {
this.taskList = taskList;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cardview_layout, parent, false);
MyViewHolder holder = new MyViewHolder(itemView);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position)
{
task = taskList.get(position);
holder.title.setText(task.getTitle());
holder.textViewTimeCV.setText(task.getTime());
holder.startStop(task.getTime());
holder.startAlert(Long.parseLong(String.valueOf(task.getTime())));
this.position = position;
}
@Override
public int getItemCount() {
return taskList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private long timeCountInMilliSeconds = 1 * 60000;
public long timeLeft = 0;
public TextView title;
private TimerStatus timerStatus = TimerStatus.STOPPED;
private ProgressBar progressBarCV;
public TextView textViewTimeCV;
private CountDownTimer countDownTimer;
public MyViewHolder(View view) {
super(view);
initViews(view);
}
private void initViews(View view) {
title = (TextView) view.findViewById(R.id.title);
progressBarCV = (ProgressBar) view.findViewById(R.id.progressBarCV);
textViewTimeCV = (TextView) view.findViewById(R.id.textViewTimeCV);
}
public void startStop(String minutes) {
if (timerStatus == TimerStatus.STOPPED) {
// call to initialize the timer values
setTimerValues(minutes);
// call to initialize the progress bar values
setProgressBarValues();
// changing the timer status to started
timerStatus = TimerStatus.STARTED;
// call to start the count down timer
startCountDownTimer();
} else {
// changing the timer status to stopped
timerStatus = TimerStatus.STOPPED;
stopCountDownTimer();
}
}
private void setTimerValues(String minutes) {
int time = 0;
if (!minutes.isEmpty() || Integer.parseInt(minutes) != 0) {
time = Integer.parseInt(minutes);
}
// assigning values after converting to milliseconds
timeCountInMilliSeconds = time * 60 * 1000;
}
private void startCountDownTimer() {
Intent startServiceIntent = new Intent(context, TimerService.class);
startServiceIntent.putExtra(EXTRAS_TIME, timeCountInMilliSeconds);
context.startService(startServiceIntent);
Log.d("Service", "startService");
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long millisUntilFinished = intent.getLongExtra(EXTRAS_COUNTDOWN, 0L);
Log.d("Service", "Smthisreceived");
task.timeLeft = String.valueOf(timeLeft); // here add left time for timer
textViewTimeCV.setText(hmsTimeFormatter(millisUntilFinished)); // just handling status bar values
progressBarCV.setProgress((int) (millisUntilFinished / 1000));
}
};
context.registerReceiver(broadcastReceiver, new IntentFilter(COUNTDOWN_BROADCAST_RECEIVER));
Log.d("Service", "receiverRegistered");
}
private void stopCountDownTimer() {
countDownTimer.cancel();
}
private void setProgressBarValues() {
progressBarCV.setMax((int) timeCountInMilliSeconds / 1000);
progressBarCV.setProgress((int) timeCountInMilliSeconds / 1000);
}
private String hmsTimeFormatter(long milliSeconds) {
String hms = String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(milliSeconds),
TimeUnit.MILLISECONDS.toMinutes(milliSeconds) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliSeconds)),
TimeUnit.MILLISECONDS.toSeconds(milliSeconds) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliSeconds)));
return hms;
}
private void startAlert(long minutes)
{
Intent intent = new Intent(context, MyAlarmReceiver.class);
intent.putExtra(EXTRAS_NAME, task.getTitle());
intent.putExtra(EXTRAS_REQUEST_CODE, System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int)System.currentTimeMillis(), intent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(minutes*MINUTES_TO_MILLIS), pendingIntent);
Toast.makeText(context, "Alarm set in " + minutes, Toast.LENGTH_SHORT).show();
}
}}
我的服务类
public class TimerService extends Service {
private CountDownTimer ctd;
private Intent broadcastIntent;
private int requestCode;
private long timeCountInMillis;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {
@Override
public void run() {
}
}).start();
Intent notificationIntent = new Intent(this, MainActivity.class);
requestCode = (int)(intent.getLongExtra(EXTRAS_TIME, 0L));
timeCountInMillis = intent.getLongExtra(EXTRAS_TIME, 0L);
broadcastIntent = new Intent(COUNTDOWN_BROADCAST_RECEIVER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, notificationIntent, 0);
Notification.Builder notification = new Notification.Builder(this)
.setContentTitle(getTextFromResource(this, R.string.app_name))
.setContentText(getTextFromResource(this, R.string.your_timer) + " "
+ getTextFromResource(this, R.string.is_currently_running))
.setSmallIcon(ic_launcher)
.setContentIntent(pendingIntent);
startForeground(requestCode, notification.build());
ctd = new CountDownTimer(timeCountInMillis , 1000) {
@Override
public void onTick(long millisUntilFinished) {
broadcastIntent.putExtra(EXTRAS_COUNTDOWN, millisUntilFinished);
sendBroadcast(broadcastIntent);
}
@Override
public void onFinish() {
stopForeground(true);
}
}.start();
return START_STICKY;
}}