Get ArrayList value and set it to a TextView on Button Click Not Working - RecyclerView

217 Views Asked by At

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

}
1

There are 1 best solutions below

0
Dev4Life On

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