java radiobutton重置_Checked RadioButton Recycling

I'm adding radiogroups programatically to my recyclerview and it's working fine. But when I check it and scroll the recyclerview it loses checked radios.

I've seen many ways and examples of solutions, but I cannot achieve it. It's been some days in a row.

I'm saving the checked radio in model as you can see in code below.

Adapter:

@Override

public void onBindViewHolder(final NROptionLineHolder holder, int position) {

holder.priceGroup.removeAllViews();

holder.priceGroup.setOnCheckedChangeListener(null);

int id = (position+1)*100;

checklistModel = mChecklists.get(position);

holder.packageName.setText(checklistModel.getTitle());

for(String price : checklistModel.getQuestions()){

RadioButton rb = new RadioButton(NROptionLineAdapter.this.context);

rb.setId(id++);

rb.setText(price);

holder.priceGroup.addView(rb);

}

holder.priceGroup.check(checklistModel.getSelectedId());

holder.priceGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

checklistModel.setSelectedId(checkedId);

Log.d(TAG, "onCheckedChanged: " + checkedId);

}

});

}

Holder

OnNROptionListener onNROptionListener;

public NROptionLineHolder(View itemView, OnNROptionListener onNROptionListener) {

super(itemView);

packageName = itemView.findViewById(R.id.package_name);

priceGroup = itemView.findViewById(R.id.price_grp);

// priceGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

// @Override

// public void onCheckedChanged(RadioGroup radioGroup, int i) {

//

// Log.d(TAG, "onCheckedChanged: " + radioGroup.getCheckedRadioButtonId() + " " + i);

// }

// });

this.onNROptionListener = onNROptionListener;

itemView.setOnClickListener(this);

}

@Override

public void onClick(View v) {

onNROptionListener.onNROptionClick(getAdapterPosition());

}

public interface OnNROptionListener {

void onNROptionClick(int position);

}

}

EDIT 1 - Radio Group

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/package_name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintTop_toTopOf="parent" />

android:id="@+id/price_grp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

app:layout_constraintTop_toBottomOf="@+id/package_name"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintEnd_toEndOf="parent"

android:orientation="horizontal"/>

EDIT 2

As requested, here is the important code from my ChecklistActivity

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_checklist);

intent = getIntent();

size = intent.getIntExtra("size", 0);

nr = intent.getIntExtra("nr", 0);

Log.d(TAG, "Checklist Activity - Qtd Questões: " + size);

Log.d(TAG, "Checklist Activity - NR: " + nr);

btnSaveCheck = findViewById(R.id.btnSaveChecklist);

mRecyclerView = findViewById(R.id.package_lst);

setupRecycler();

btnSaveCheck.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(getApplicationContext(), "Sucesso", Toast.LENGTH_SHORT).show();

}

});

}

private void setupRecycler() {

LinearLayoutManager layoutManager = new LinearLayoutManager(this);

mRecyclerView.setLayoutManager(layoutManager);

setupList();

mAdapter = new NROptionLineAdapter(data, this, getApplication());

mRecyclerView.setAdapter(mAdapter);

}

private void setupList(){

data = new ArrayList<>();

class setupList extends AsyncTask> {

@Override

protected void onPreExecute() {

super.onPreExecute();

}

@Override

protected List doInBackground(Void... voids) {

list = DatabaseClient

.getInstance(getApplicationContext())

.getAppDatabase()

.mrNrOptionDAO()

.loadAllByNRId(nr);

return list;

}

@Override

protected void onPostExecute(List list) {

super.onPostExecute(list);

List priceList = new ArrayList<>();

priceList.add("Sim");

priceList.add("Não");

priceList.add("Não se Aplica");

for (int i=0; i

Log.d(TAG, "NRs Activity - Adding To List: " + list.get(i).getTitle());

data.add(new Checklist(

list.get(i).getTitle(),

priceList)

);

mAdapter.notifyDataSetChanged();

}

}

}

setupList lm = new setupList();

lm.execute();

}

deb3334178902b653afb296b4ed9f9e2.png

EDIT 3 - Important

The RadioGroups and RadioButtons are programatically generated because I'm getting all questions from server, the number of questions are different depending on previous selections made by user, that's why I need it this way.

EDIT 4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值