Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 回收者视图将特定附加内容传递给特定项目_Java_Android_Service_Timer_Android Recyclerview - Fatal编程技术网

Java 回收者视图将特定附加内容传递给特定项目

Java 回收者视图将特定附加内容传递给特定项目,java,android,service,timer,android-recyclerview,Java,Android,Service,Timer,Android Recyclerview,我对回收商的观点和服务有意见。每一行recycler视图都是计时器,我已经成功地实现了这个构造。现在,我的服务将多余的内容传递回去,以更新我的计时器,但它会将它们带到每个示例视图持有者。我的意思是,如果a添加了两个计时器(例如一分钟和两分钟),我的两个计时器都会开始闪烁。它将从两个计时器中无序地使用extas,并且倒计时将看起来像(在每个计时器上)0:59 1:59 0:58 1:58,依此类推,这是我的rv的代码。是否有方法传递特定项目的特定信息?请帮我解决这个问题 任务(模型类) 我的适配器

我对回收商的观点和服务有意见。每一行recycler视图都是计时器,我已经成功地实现了这个构造。现在,我的服务将多余的内容传递回去,以更新我的计时器,但它会将它们带到每个示例视图持有者。我的意思是,如果a添加了两个计时器(例如一分钟和两分钟),我的两个计时器都会开始闪烁。它将从两个计时器中无序地使用extas,并且倒计时将看起来像(在每个计时器上)0:59 1:59 0:58 1:58,依此类推,这是我的rv的代码。是否有方法传递特定项目的特定信息?请帮我解决这个问题

任务(模型类)

我的适配器和视图保持器实现

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