gpt4 book ai didi

android-recyclerview - 如何迭代 RecyclerView 项目

转载 作者:行者123 更新时间:2023-12-04 08:21:55 27 4
gpt4 key购买 nike

在我的应用程序中,我有一个 RecyclerView,其中包含一个简单的项目 View ,每行包含一个 TextView 和一个 Spinner。

用户从操作栏中单击“保存”后,我需要遍历所有项目并获取 Spinner 选择的值。

有什么想法可以或应该如何实现?

更新:

我已经设法使用以下循环进行迭代,但是对于每一行调用 findViewById() 是否是一个很好的解决方案,因为它是一个调用性能明智的繁重函数?

for (int i = 0; i < rvAttendanceItems.getChildCount(); i++) {
Spinner spinner = (Spinner) rvAttendanceItems.findViewHolderForAdapterPosition(i).itemView.findViewById(R.id.attendance_list_status);
spinner.getSelectedItemId();
}

我的完整 RecyclerView 代码:
public class AttendanceAdapter extends RecyclerView.Adapter<AttendanceAdapter.AttendanceViewHolder> {

private List<Student> studentItems;
private Fragment frag;
ArrayList<Status> statuses = new ArrayList<>();

public AttendanceAdapter(List<Student> studentItems, Fragment frag) {
this.studentItems = studentItems;
this.frag = frag;

DbHelper db = DbHelper.getInstance(frag.getContext());
Cursor c = db.getStatuses("attendance");

if (c != null) {
while (c.moveToNext()) {
Status tempStatus = new Status(c.getInt(c.getColumnIndex(DbHelper.STATUS_ID)), c.getInt(c.getColumnIndex(DbHelper.STATUS_COLOR)), c.getString(c.getColumnIndex(DbHelper.STATUS_DESC)), c.getString(c.getColumnIndex(DbHelper.STATUS_CATEGORY)));
statuses.add(tempStatus);
}
c.close();
} else {
Toast.makeText(frag.getContext(), "Could not retrieve status list", Toast.LENGTH_SHORT).show();
}
}

@Override
public AttendanceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_attendance, parent, false);
return new AttendanceViewHolder(itemView);
}

@Override
public void onBindViewHolder(final AttendanceViewHolder holder, int position) {

final Student current = studentItems.get(position);

SpinnerAdapter adapter = new SpinnerAdapter(frag.getContext(), R.layout.item_spinner_status, statuses);

holder.sStatus.setAdapter(adapter);
holder.tvName.setText(current.getFullName());
}

@Override
public int getItemCount() {
return studentItems.size();
}

public class AttendanceViewHolder extends RecyclerView.ViewHolder {

private final TextView tvName;
private final Spinner sStatus;

public AttendanceViewHolder(View itemView) {
super(itemView);

tvName = (TextView) itemView.findViewById(R.id.attendance_list_name);
sStatus = (Spinner) itemView.findViewById(R.id.attendance_list_status);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

sStatus.performClick();
}
});
}
}
}

最佳答案

您已经调用了findViewById()在 View 持有者中,不需要再做一次:

for (int i = 0; i < rvAttendanceItems.getChildCount(); i++) {
AttendanceViewHolder holder = (AttendanceViewHolder) rv.findViewHolderForAdapterPosition(i);
holder.sStatus.getSelectedItemId();
}

关于android-recyclerview - 如何迭代 RecyclerView 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32811156/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com