1

My last question hopefully for this week.. I have set up a RecyclerView with an Array that iterates through numbers 0 - 350 in increments of 10. This pops up as an overlay over the activity by clicking a TextView (dailyGoal). Once a value has been selected, the chosen value should be set to the TextView.

This is all working fine, the value does set, however it sets the value as the first value, which is 10 mins. I think this is because the onItemClicked is pointing to the RecyclerView as a whole and not the Recycler TextView (time), there is a method included for it, however whenever I try to use it, the onClick function does nothing..

I think this one is a simple solution but I've been messing around with it for a while and can't find a working solution..

Activity

ArrayList<String> dailyGoalTime;
int[] timeArray = new int[360];

    dailyGoal.setText("60");
    dailyGoalRecycler.setVisibility(View.GONE);

    dailyGoalTime = new ArrayList<>();
    for (int i = 0; i < timeArray.length; i++) {
        timeArray[i] = i;
    }

    for (int i = 0; i < timeArray.length; i++) {
        if (i % 10 == 0 && i != 0) {
            dailyGoalTime.add(i + " mins");
        }
    }

    ItemClickSupport.addTo(dailyGoalRecycler).setOnItemClickListener(
            new ItemClickSupport.OnItemClickListener() {
                @Override
                public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                    dailyGoal.setText(timeArray[i]);
                    dailyGoalRecycler.setVisibility(View.GONE);
                }

                @Override
                public void onItemClicked(TextView time, int position, View view) {
                    // this does nothing
                    
                    // dailyGoal.setText(dailyGoalTime.get(timeArray[i]));
                    // dailyGoalRecycler.setVisibility(View.GONE);
                    }
                }
        );

Interface

public interface OnItemClickListener {

    void onItemClicked(RecyclerView recyclerView, int position, View v);

    void onItemClicked(TextView time, int position, View view);
}

Adapter

ArrayList<String> dailyGoalTime;
ItemClickSupport.OnItemClickListener listener;

public DailyGoalAdapter(ArrayList<String> dailyGoalTime) {
    this.dailyGoalTime = dailyGoalTime;
}

@NonNull
@Override
public DailyGoalAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull DailyGoalAdapter.ViewHolder holder, int position) {
    holder.time.setText(dailyGoalTime.get(position));
}

@Override
public int getItemCount() {
    return dailyGoalTime.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView time;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        time = itemView.findViewById(tvTime);
    }

}
Caitlin
  • 531
  • 5
  • 19
  • I'm not sure if I'm following everything correctly, but this might be the issue: `dailyGoal.setText(timeArray[i]);`. Seems like you meant to set `timeArray[position]` instead. I can't see where that `i` is declared, at least. Those in the loops are out of scope at that point, and wouldn't really make sense there anyway. – Mike M. Jul 29 '21 at 22:01
  • @MikeM. I tried that before sending this, should have added that sorry. The app crashes `onClick` with error `android.content.res.Resources$NotFoundException: String resource ID #0x3` at `setText` method. I have seen answers where the for loop was inside the button click but like you say, I don't think that makes sense? – Caitlin Jul 29 '21 at 22:06
  • Right, 'cause `timeArray` is `int`s. I didn't see that. However, the given code should still be throwing that same Exception, I would think. In any case, for that specific Exception, it's because you can't set arbitrary integer values with that method; it's expecting a resource ID. To set a random integer value, you just need to convert it to a `String` first; e.g., `dailyGoal.setText(String.valueOf(timeArray[i]));`. I'm not sure if that's the only issue you currently have, though. – Mike M. Jul 29 '21 at 22:10
  • This is what I had earlier but was showing me 10 mins (first value), now it is setting the `dailyGoal` to 0 instead. No errors however. – Caitlin Jul 29 '21 at 22:13
  • 1
    Sorry, not `i` there. You want `position`: `dailyGoal.setText(String.valueOf(timeArray[position]));` – Mike M. Jul 29 '21 at 22:14
  • Wait, that is because I had decared `i` as an integer, it would display null. – Caitlin Jul 29 '21 at 22:14
  • Okay, thats doing something! It is displaying the array position of the textView (recycler): 0, 1, 2, 3 etc – Caitlin Jul 29 '21 at 22:15
  • Well, that's how you're filling that array: `timeArray[i] = i;`. You might want to log its contents while you're fixing this, to make sure it contains what you mean for it to. – Mike M. Jul 29 '21 at 22:16
  • I thought I was filling it like so: `dailyGoalTime.add(i + " mins");` – Caitlin Jul 29 '21 at 22:18
  • 1
    Did you mean to do this instead: `dailyGoal.setText(dailyGoalTime.get(position));`? – Mike M. Jul 29 '21 at 22:19
  • 1
    Thats the one! I could have sworn I tried that already.. my tired brain must have decieved me. Thank you so much! – Caitlin Jul 29 '21 at 22:21

1 Answers1

0

You forgot the most important part. You need to call the interface method on item click like this:

holder.itemView.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        listener.onItemClicked(holder.time, position, holder.itemView);
    }
)};
Dev4Life
  • 2,328
  • 8
  • 22