0

I am trying to make an app that takes the number of sets, and number of reps for each set from the user, and adds rows ( for each set) and checkboxes (for each rep) that is what is should look like:

for the reps: i made a method that takes the number of reps from the SQLite server, and loops through the number of reps to make the checkboxes.

for the sets: i made list adapter to make each row.

the final results for 2 sets, and 2 reps each look like this: enter image description here

how can i make the reps be contained inside the rows? thanks.

recyclerview adaptor:

''' package com.example.workouttracker;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class WorkoutInterfacesAdapter extends RecyclerView.Adapter<WorkoutInterfacesAdapter.WorkoutInterfaceViewHolder> {

    Context mContext;
    private ArrayList<WorkoutInterfaceClass> sets;
    private LinearLayout linearLayout;

    public WorkoutInterfacesAdapter(Context mContext , ArrayList<WorkoutInterfaceClass> sets) {

        this.sets = sets;
        this.mContext = mContext;
    }


    @NonNull
    @Override
    public WorkoutInterfaceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.workout_interface_item,parent,false);
        return new WorkoutInterfacesAdapter.WorkoutInterfaceViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull  WorkoutInterfacesAdapter.WorkoutInterfaceViewHolder holder, int position) {
        holder.bind(sets.get(position));
        //new code:

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext,"Set number: " + sets.get(position).numberOfSets + " ,Rep Number: "+sets.get(position).numberOfReps,Toast.LENGTH_LONG).show();
                Intent intent = new Intent(mContext,WorkoutInterface.class);
                intent.putExtra("workout_sets",sets.get(position).numberOfSets);
                intent.putExtra("workout_reps",sets.get(position).numberOfReps);
                mContext.startActivity(intent);

            }
        });




    }

    @Override
    public int getItemCount() {
        return sets.size();
    }
    static class WorkoutInterfaceViewHolder extends RecyclerView.ViewHolder{

        TextView numberOfSets;
        int numberOfReps;
        CheckBox checkBox;
        LinearLayout linearLayout;



        public WorkoutInterfaceViewHolder(@NonNull View itemView) {
            super(itemView);
             numberOfSets =itemView.findViewById(R.id.edit_text_number_of_sets_item);
             checkBox=itemView.findViewById(R.id.checkbox_number_of_reps_item);
             linearLayout=itemView.findViewById(R.id.ll_checkbox_mother);
        }

        public void bind(WorkoutInterfaceClass sets) {
            // 
           numberOfSets.setText("Set number:" + sets.numberOfSets); ;
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            numberOfReps=sets.numberOfReps;
            for (int i = 1; i <= numberOfReps; i++) {
                CheckBox checkBox = new CheckBox(linearLayout.getContext());
                checkBox.setId(View.generateViewId());
                checkBox.setText("Rep: "+checkBox.getId());
                checkBox.setOnClickListener((View.OnClickListener) linearLayout.getContext());
                System.out.println("Total Number Of Sets:"+numberOfReps + "And the index is : "+ i );

            
        }
    }



}}

'''

levi
  • 43
  • 1
  • 6
  • If you want the checkboxes to go inside your ListView you need to create a custom layout for your list view items. You can check https://stackoverflow.com/questions/19615766/android-custom-layout-for-listview – gioravered Sep 23 '21 at 17:37

1 Answers1

1

Try creating a custom layout that will contain all the necessary views and pass the layout in the onCreateView of you adapter class with a layout inflater.