我在 Recyclerview 中使用了 Spinner 。我在Mainactivity中有一个按钮。当我单击该按钮时,我需要从recyclerview的每一行中获取所有选定的项目。 getSelectedItem()方法将从微调器中返回选定的项目,但我认为它仅适用于单个项目。 我认为无需使用 setOnItemSelectedListener 。
我的代码是:
public class MainActivity extends AppCompatActivity {
RecyclerView recycler_view;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recycler_view=findViewById(R.id.recycler_view);
button = findViewById(R.id.button);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recycler_view.setLayoutManager(mLayoutManager);
setAdapter();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
private void setAdapter( ) {
recycler_view.setVisibility(View.VISIBLE);
StudentsListAdapter studentsListAdapter = new StudentsListAdapter(this);
recycler_view.setAdapter(studentsListAdapter);
// runLayoutAnimation(recycler_view, mSelectedItem);
}
}
适配器:
public class StudentsListAdapter extends RecyclerView.Adapter<StudentsListAdapter.MyViewHolder> {
String[] list={"Microbiology","Pharmacology","Microbiology","Forensic Medicine"};
String[] studentsName={"Student1","Student2","Studen3","Student4"};
Context ctx;
private ArrayAdapter<String> dataAdapter;
public class MyViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.text_name)
TextView text_name;
@BindView(R.id.spinner1)
Spinner spinner1;
MyViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
public StudentsListAdapter(Context ctx ) {
this.ctx = ctx;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.student_list_item, parent, false);
return new MyViewHolder(itemView);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, @SuppressLint("RecyclerView") final int position) {
holder.text_name.setText(studentsName[position]);
dataAdapter = new ArrayAdapter<String>(ctx, R.layout.spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.spinner1.setAdapter(dataAdapter);
Log.d("Holder ",""+ holder.spinner1.getSelectedItem());
}
@Override
public int getItemCount() {
return studentsName.length;
}
}
答案 0 :(得分:0)
我想您需要添加一个复选框来知道选择了哪些项目。
Arraylist selectedStudent = new Arraylist();
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, @SuppressLint("RecyclerView") final int position) {
holder.text_name.setText(studentsName[position]);
dataAdapter = new ArrayAdapter<String>(ctx, R.layout.spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.spinner1.setAdapter(dataAdapter);
Log.d("Holder ",""+ holder.spinner1.getSelectedItem());
//checkbox listener
holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (isChecked){
selectedStudent.add(studentsName[position]);
}
}
});
}
在活动中,您可以访问您的变量selectedStudent
StudentsListAdapter studentListAdapter;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < studentListAdapter.selectedStudent.size()-1 ; i++) {
Log.d("studentSelected".""+studentListAdapter.selectedStudent.get(i));
}
}
});
答案 1 :(得分:0)
使用另一个具有Spinner列表大小的列表,并将每个Spinner的默认值添加到列表中。每当您更改微调器中的值时,也将更改“选定值”列表中的值。
现在在适配器中添加新方法以获取选定值列表。并在按钮上调用该方法,然后单击