0

I want to set separate alarms using cards in cardView with adapter and TimePickerFragment, it works with one card but over writes the alarm on the first card no matter what other card I select, would I need to add a method in my adapter class for the card using position from the card ArrayList? I am just trying to display what time I picked from the TimePickerFragment on each separate card

java

`public class myAdapter extends RecyclerView.Adapter {

private ArrayList<CardDataHolder> dataList;
private OnItemClickListener listener;

//put interface methods here for items on the card actions
public interface OnItemClickListener
{
    void onItemClicked(int pos);
    void Delete(int pos);
    void alarmSet(int pos);
}

public void setOnItemClickListener(OnItemClickListener listener)
{
    this.listener = listener;
}

//this class set values and defines the OnclickListener method for item click
public static class myViewHolder extends RecyclerView.ViewHolder
{
    public ImageView image;
    public TextView text;
    public ImageView imgAlarm;


    public myViewHolder(@NonNull View itemView, OnItemClickListener listener) {
        super(itemView);

        //put the findById method here to set inputs
        image = itemView.findViewById(R.id.cardDelete);
        text = itemView.findViewById(R.id.txtCard);
        imgAlarm = itemView.findViewById(R.id.imgAlarm);

        //when the who card is clicked
        itemView.setOnClickListener((v) -> {
            if(listener != null)
            {
                int pos = getAdapterPosition();
                if(pos != RecyclerView.NO_POSITION)
                {
                    listener.onItemClicked(pos);
                }
            }
        });

        //when the image X is clicked
        image.setOnClickListener((v)-> {
            if(listener != null)
            {
                int pos = getAdapterPosition();
                if(pos != RecyclerView.NO_POSITION) {
                    listener.Delete(pos);
                }
            }
        });

        imgAlarm.setOnClickListener((V)->{
            if(listener != null)
            {
                int pos = getAdapterPosition();
                if(pos != RecyclerView.NO_POSITION) {
                    listener.alarmSet(pos);
                }
            }
        });
    }
}

public myAdapter(ArrayList<CardDataHolder> data)
{
    dataList = data;
}

@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cards,

parent, false); myViewHolder holder = new myViewHolder(view, listener); return holder; }

@Override
public void onBindViewHolder(@NonNull myViewHolder ViewHolder, int pos) {

    //current item of the card
    CardDataHolder currentItem = dataList.get(pos);

    //sets the text view with the data on card so each card is matched with the correct
    //data
    ViewHolder.text.setText(currentItem.getText());
}

//returns he size of array of cards
@Override
public int getItemCount() {
    if(dataList == null) {
        return 0;
    }
    else
    {
        return dataList.size();
    }
}

//remove cards when the X is clicked on the card
public void removeAt(int position) {
    dataList.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, dataList.size());
}

}`

'public class TimePickerFragment extends DialogFragment { @NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

    Calendar calendar = Calendar.getInstance();
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int min  = calendar.get(Calendar.MINUTE);
    return new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            TextView viewAlarmTime = getActivity().findViewById(R.id.viewAlarmTime);
            //TODO only sets one entry need to make it set for all cards
            viewAlarmTime.setText(String.format("%02d:%02d", hourOfDay, minute));

        }
    }, hour, min, android.text.format.DateFormat.is24HourFormat(getActivity()));

}'

in main

' adapter.setOnItemClickListener(new myAdapter.OnItemClickListener() { @Override public void onItemClicked(int pos) { Toast.makeText(getApplicationContext(), String.valueOf(pos), Toast.LENGTH_LONG).show();

            dialogAlert(pos);
        }

        @Override
        public void Delete(int pos) {
            adapter.removeAt(pos);
            write(getApplicationContext(), cardData);
        }

        @Override
        public void alarmSet(int pos) {
            //TODO make this pop up a clock fragment setter and display it
            Toast.makeText(getApplication(), "Set Alarm" + pos, Toast.LENGTH_LONG).show();
            DialogFragment timePicker = new TimePickerFragment();
            timePicker.showNow(getSupportFragmentManager(), "time picker");
        }
    }); '
baric
  • 1
  • 3
  • Hi, your question is lacking in detail. No one will be able to answer your question without more information, like some code illustrating the issue. Please review: https://stackoverflow.com/help/how-to-ask and update your question so it's clearer what you need help with. – dominicoder Jun 16 '19 at 06:04
  • It would also be great to see improved code formatting and correct spelling. – Ross Jun 16 '19 at 06:49
  • I'm having trouble with the formatting on here sorry guys – baric Jun 16 '19 at 06:50

0 Answers0