我有一个 RecyclerView,它在 CardViews 上加载 CheckBoxes。这是适配器。加载时很好。但是当我故意检查 RecyclerView 上的项目时,CheckBox 会在某个地方随机检查。我怎样才能摆脱这个问题?这是我的适配器。提前致谢。
public class QuestionsAdapter extends RecyclerView.Adapter<QuestionsAdapter.QuestionsViewHolder> {
List<QuestionModel> Questions;
public Context context;
public QuestionsAdapter(Context context, List<QuestionModel> questions) {
this.Questions = questions;
this.context = context;
}
@Override
public QuestionsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.questions_card_view, parent, false);
return new QuestionsViewHolder(v);
}
@Override
public void onBindViewHolder(QuestionsViewHolder holder, final int position) {
holder.txtQuestionText.setText(Questions.get(position).QuestionText);
}
@Override
public int getItemCount() {
return Questions.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public class QuestionsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public RadioButton rbLike;
public RadioButton rbdisLike;
public RadioButton rbnoComment;
public TextView txtQuestionText;
public QuestionsViewHolder(View itemView) {
super(itemView);
rbLike = (RadioButton) itemView.findViewById(R.id.like);
rbdisLike = (RadioButton) itemView.findViewById(R.id.Dislike);
txtQuestionText = (TextView) itemView.findViewById(R.id.txtCardQuestion);
rbnoComment = (RadioButton) itemView.findViewById(R.id.NoComment);
rbnoComment.setOnClickListener(this);
rbdisLike.setOnClickListener(this);
rbLike.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.like:
rbLike.setChecked(true);
rbnoComment.setChecked(false);
rbdisLike.setChecked(false);
break;
case R.id.Dislike:
rbdisLike.setChecked(true);
rbnoComment.setChecked(false);
rbLike.setChecked(false);
break;
case R.id.NoComment:
rbnoComment.setChecked(true);
rbLike.setChecked(false);
rbdisLike.setChecked(false);
//do something
break;
default:
}
}
}
}
最佳答案
您的 onBindViewHolder() 方法有问题... RecyclerView 将在滚动期间重用 ViewHolder。因此,您应该为 ViewHolder 中的所有 View 设置状态,以查看一致的数据。现在您只为“txtQuestionText”设置文本。在这种情况下,您将看到列表项的一致文本和单选按钮的随机状态。
简而言之,你应该有这样的东西:
@Override
public void onBindViewHolder(QuestionsViewHolder holder, final int position) {
holder.setModel(Questions.get(position));
}
...
public class QuestionsViewHolder ...
...
public void setModel(QuestionModel model){
rbLike.setChecked(model.like);
rbdisLike.setChecked(model.dislike);
rbnoComment.setChecked(model.comment);
txtQuestionText.setText(model.text);
}
关于Android RecyclerView Checkbox 随机勾选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34235049/
华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o
我想在ruby中生成一个64位整数。我知道在Java中你有很多渴望,但我不确定你会如何在Ruby中做到这一点。另外,64位数字中有多少个字符?这是我正在谈论的示例......123456789999。@num=Random.rand(9000)+Random.rand(9000)+Random.rand(9000)但我认为这是非常低效的,必须有一种更简单、更简洁的方法来做到这一点。谢谢! 最佳答案 rand可以将范围作为参数:pa=rand(2**32..2**64-1)#=>11093913376345012184putsa.
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:HowdoIgeneratealistofnuniquerandomnumbersinRuby?我想做的事:Random.rand(0..10).timesdoputsRandom.rand(0..10)end但如果随机数已经显示过,则无法再次显示。如何最轻松地做到这一点?
我试图在每次运行时以随机顺序将一个名称数组拆分为多个数组。我知道如何拆分它们:name_array=["bob","john","rob","nate","nelly","michael"]array=name_array.each_slice(2).to_a=>[["bob","john"],["rob","nate"],["nelly","michael"]]但是,如果我希望它每次都以随机顺序吐出它们怎么办? 最佳答案 在做同样的事情之前,打乱数组。(Array#shuffle)name_array.shuffle.each_s
有没有办法在ruby中生成介于1-100但不包括20、30和40之间的随机数?我可以做类似的事情defrandom_numberrandom_number=rand(100)whilerandom_number==20||30||40random_number=rand(100)endreturnrandom_numberend...但这似乎不是很有效(再加上那个特定的例子可能根本行不通)。有没有更简单的方法?任何帮助深表感谢! 最佳答案 创建一个1到100的数组。从该数组中删除不需要的元素。然后从数组中选择一个随机数。([*1
我想生成一个包含数字、字母和特殊字符的给定(长度可能不同)长度的完全随机的“唯一”(我将确保使用我的模型)标识符例如:161551960578281|2.AQAIPhEcKsDLOVJZ.3600.1310065200.0-514191032|有人可以建议在RubyonRails中最有效的方法吗?编辑:重要:如果可能,请评论您提出的解决方案的效率,因为每次用户进入网站时都会使用它!谢谢 最佳答案 将其用于访问token与UUID不同。您不仅需要伪随机性,而且还需要加密安全PRNG.如果您真的不关心您使用的是什么字符(它们不会增加任何
所以基本上是为了好玩,我试图生成一列数字(7位数字只有0和1)我的代码很短:a=rand(0000000-1111111)b=220a1=rand(0000000-1111111)a2=rand(0000000-1111111)a3=rand(0000000-1111111)a4=rand(0000000-1111111)a5=rand(0000000-1111111)whileb!=0putsaputsa2putsa3putsa4putsa5end我的问题是,不是生成随机的0和1列,而是所有,而是使用了数字。 最佳答案 这是惯用的
我正在尝试为我的gem编写规范,它生成otp并将其保存在数据库中。现在我正在为它编写规范。所以基本上我有三种方法generate_otp!、regenerate_otp!、verify_otp(otp)。generate_otp!的作用是调用包含三个变量的方法generate_otpotp_code-基本上是使用secure_random生成的随机值otp_verified-一个bool值,用于设置otp是否已验证的状态otp_expiry_time-设置otp的到期时间,可以由Rails应用在配置中设置。这三个也是我的数据库的列。在generate_otp之后,我正在调用active
尝试使用SecureRandom类Rails生成随机数字。我们能否使用SecureRandom.hex创建一个仅包含数字而不含字母的随机数。例如:代替SecureRandom.hex(4)=>"95bf7267"应该给SecureRandom.hex(4)=>"95237267" 最佳答案 查看SecureRandom的API:http://rails.rubyonrails.org/classes/ActiveSupport/SecureRandom.html我相信您正在寻找一种不同的方法:#random_number。Secur
这是设计使然吗?代码如下:classFileRenamerdefRenameFiles(folder_path)files=Dir.glob(folder_path+"/*")endendputs"Renamingfiles..."renamer=FileRenamer.new()files=renamer.RenameFiles("/home/papuccino1/Desktop/Test")putsfilesputs"Renamingcomplete."获取文件的顺序似乎是随机的,而不是它们在Nautilus中显示的那样。这是设计使然吗?我只是好奇。 最