gpt4 book ai didi

java - 如何从 fragment 中的RecyclerView获取有关复选框的信息

转载 作者:行者123 更新时间:2023-12-01 18:03:44 25 4
gpt4 key购买 nike

我需要从 recyclerView 获取有关项目的信息。我尝试做类似的事情来进入,但这没有用,有什么想法吗?我认为调用 CheckBox 是没有用的,我不需要它。但现在我不知道该怎么做。

private void handlerForChannels() {
list = ChannelsManager.getInstance().getChannelList();
mAdapter = new SettingsCustomAdapter(context, posit, list);
verticalGridView.setAdapter(mAdapter);
textView.setText(R.string.title_channels);
button.setText(R.string.select_all);
final SparseBooleanArray mChecked = new SparseBooleanArray();

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAdapter.getItemViewType(SETTINGS_CHANNELS);
mAdapter.getItemId(getId());
CheckBox cb;
int count = list.size();
cb = (CheckBox) view.findViewById(R.id.checkBox_for_recycle);
for (int i = 0; i < count; i++) {
mChecked.put(i, cb.isChecked());
}


mAdapter.notifyDataSetChanged();

}
});
button.setNextFocusLeftId(R.id.select_all);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});
button1.setText(R.string.cancel_text);
button1.setNextFocusLeftId(R.id.select_all);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

((MainActivity) activity).closeSettingsDrawerFragment();
}
});
button2.setText(R.string.ok_text);
button2.setNextFocusLeftId(R.id.ok_button);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});
}

最佳答案

使用 boolean 值将新数组添加到适配器中,以设置复选框 true 或 false:

    //...
public ArrayList<Boolean> checkBoxesState;
//...

public mAdapter(Context context, ArrayList<String> posit, ArrayList<String> list, ArrayList<Boolean> checkBoxesState /* <- add this one*/) {
//...
this.checkBoxesState = checkBoxesState;
}

比您设置的是否检查它们:

    @Override
public void onBindViewHolder(final NotaViewHolder viewHolder, final int position) {

//...
viewHolder.checkBox.setChecked(checkBoxesState.get(position));
}

现在您可能已经知道如何检查它们,但对于那些也遇到这种情况的人,方法如下:

    //create adapter
mAdapter = new mAdapter(context, posit, list, checkBoxesStates /* <- fill with false booleans */ );
recyclerView.setAdapter(mAdapter);

//check them all
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i <= mAdapter.getItemCount(); i++) {
checkBoxesStates.add(i, true);
mAdapter.notifyItemChanged(0); //this updates the adapter
}
}
});

编辑:

您必须在 ViewHolder 中创建复选框,如下所示:

        public class NotaViewHolder extends RecyclerView.ViewHolder {

//...
CheckBox checkBox;


public NotaViewHolder(final View itemView) {
super(itemView);

//...

checkBox = (CheckBox) itemView.findViewById(R.id.rowCheckBox);

//...
}
}

关于java - 如何从 fragment 中的RecyclerView获取有关复选框的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38566339/

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