【问题标题】:Dynamically add checkboxes to a listview将复选框动态添加到列表视图
【发布时间】:2021-11-17 02:37:14
【问题描述】:

我正在尝试制作一个应用程序,该应用程序从用户那里获取组数和每组的代表数,并添加行(针对每个组)和复选框(针对每个代表)

对于代表:我做了一个方法,从 SQLite 服务器获取代表的数量,并循环通过代表的数量来制作复选框。

对于集合:我制作了列表适配器来制作每一行。

如何使代表包含在行内?谢谢。

recyclerview 适配器:

''' 包 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 );

            
        }
    }



}}

'''

【问题讨论】:

标签: java android listview checkbox


【解决方案1】:

尝试创建一个自定义布局,它将包含所有必要的视图,并通过 layout inflater 在适配器类的 onCreateView 中传递布局>.

【讨论】:

  • 嘿,我尝试创建新的自定义布局文件,并使用 recyclerview。
  • 如果您觉得有用,请为答案投票。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多