jeudi 23 septembre 2021

Dynamically add checkboxes to a listview

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.

Java class:

package com.example.workouttracker;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import java.nio.charset.CharsetEncoder;
import java.util.ArrayList;

public class WorkoutInterface extends AppCompatActivity implements View.OnClickListener {

    String workoutName;
    int workoutSets;
    int workoutReps;
    LinearLayout linearLayout;
    ListView listView;
    String TAG = "Test";
    ArrayList<String> sets;// array for each set, so we can show list of sets, and the reps for each set.
    ArrayAdapter<String> adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.workout_interface);
        linearLayout=findViewById(R.id.ll1);
        sets=new ArrayList<>();
        sets.add("Set Number: ");
        listView=findViewById(R.id.listview);
        adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1,sets);
        listView.setAdapter(adapter);
        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if (extras == null) {
                workoutName = null;
                workoutSets = 0;
                workoutReps=0;
            } else {
                workoutName = extras.getString("workout_name");
                workoutSets = extras.getInt("workout_sets");
                workoutReps=extras.getInt("workout_reps");
            }
        } else {
            workoutName = (String) savedInstanceState.getSerializable("workout_name");
            workoutSets = (int) savedInstanceState.getSerializable("workout_sets");
            workoutReps = (int) savedInstanceState.getSerializable("workout_reps");

        }
        System.out.println("workout name is - " + workoutName + ", and workout sets number is - " + workoutSets + " NOTICCEEEE MEEEEEEEE!!@#TYU&^%$#$%");

        int numberOfSets = workoutSets;// the TOTAL number of sets.
        int numberOfReps =workoutReps;

//        addRepsCheckBox(numberOfReps);
        addSet(numberOfSets,numberOfReps);

    }

private void addSet(int numberOfSets,int numberOfReps){
        for (int j=1;j<=numberOfSets;j++){
            sets.add("Set Number: "+ j);
            addRepsCheckBox(numberOfReps);
            listView.setAdapter(adapter);


        }
}

    private void addRepsCheckBox(int numberOfReps) {
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        for (int i = 1; i <= numberOfReps; i++) {
            CheckBox checkBox=new CheckBox(this);
            checkBox.setId(View.generateViewId());
            checkBox.setText("Rep: "+checkBox.getId());
            checkBox.setOnClickListener(this);
            listView.addFooterView(checkBox);

        }
        }

    @Override
    public void onClick(View v) {
        Log.d(TAG, " Name " + ((CheckBox)v).getText() +" Id is "+v.getId());

    }

}

XML:

'''

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >



    <LinearLayout
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/ll1">

      <ListView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/listview"
          tools:ignore="InvalidId"/>





    </LinearLayout>

</RelativeLayout> '''



Aucun commentaire:

Enregistrer un commentaire