【发布时间】: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 );
}
}
}}
'''
【问题讨论】:
-
如果您希望复选框进入您的 ListView,您需要为您的列表视图项创建自定义布局。您可以查看stackoverflow.com/questions/19615766/…
标签: java android listview checkbox