我在列表视图中使用 RadioGroup 作为行项。RadioGroup 有 5 个单选按钮,我有 1000 行。我无法管理单选按钮的状态。我已经尝试使用地图来存储位置和检查状态。我什至尝试过使用 setTag() 和 getTag() 创建类并保存选中状态和单选按钮 ID。还没有运气。有什么建议么?
代码
适配器类
package com.example.adapters;
import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.R;
import com.example.models.AnswerStateModel;
public class AnswerKeyAdapter extends ArrayAdapter<AnswerStateModel> {
private Activity activity;
private List<AnswerStateModel> test;
Toast t = null;
public AnswerKeyAdapter(Activity activity, List<AnswerStateModel> temp) {
super(activity, R.layout.row_item_answer_key, temp);
this.activity = activity;
this.test = temp;
}
@Override
public int getCount() {
return test.size();
}
@Override
public AnswerStateModel getItem(int position) {
return test.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int currentPosition=position;
View view=null;
if (convertView == null) {
final ViewHolder holder = new ViewHolder();
view = LayoutInflater.from(activity).inflate(
R.layout.row_item_answer_key, parent,false);
holder.lblAnswerId = (TextView) view.findViewById(R.id.lblAnswerId);
holder.rdGroup = (RadioGroup)view.findViewById(R.id.rdGroup);
holder.btnA = (RadioButton) view.findViewById(R.id.btnA);
holder.btnB = (RadioButton) view.findViewById(R.id.btnB);
holder.btnC = (RadioButton) view.findViewById(R.id.btnC);
holder.btnD = (RadioButton) view.findViewById(R.id.btnD);
holder.btnE = (RadioButton) view.findViewById(R.id.btnE);
holder.btnWhat = (Button) view.findViewById(R.id.btnWhat);
view.setTag(holder);
holder.rdGroup.setTag(test.get(position));
holder.rdGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group,
int checkedId) {
AnswerStateModel model=(AnswerStateModel) holder.rdGroup.getTag();
switch (checkedId) {
case R.id.btnA:
model.setChecked(holder.btnA.isChecked());
model.setBtnId(holder.btnA.getId());
break;
case R.id.btnB:
model.setChecked(holder.btnB.isChecked());
model.setBtnId(holder.btnB.getId());
break;
case R.id.btnC:
model.setChecked(holder.btnC.isChecked());
model.setBtnId(holder.btnC.getId());
break;
case R.id.btnD:
model.setChecked(holder.btnD.isChecked());
model.setBtnId(holder.btnD.getId());
break;
case R.id.btnE:
model.setChecked(holder.btnE.isChecked());
model.setBtnId(holder.btnE.getId());
break;
default:
break;
}
}
});
} else{
view=convertView;
((ViewHolder) view.getTag()).rdGroup.setTag(test.get(position));
}
final ViewHolder holder = (ViewHolder) view.getTag();
AnswerStateModel model=(AnswerStateModel)getItem(currentPosition);
holder.btnA.setChecked(false);
holder.btnB.setChecked(false);
holder.btnC.setChecked(false);
holder.btnD.setChecked(false);
holder.btnE.setChecked(false);
switch (model.getBtnId()) {
case R.id.btnA:
holder.btnA.setChecked(test.get(position).isChecked());
break;
case R.id.btnB:
holder.btnB.setChecked(test.get(position).isChecked());
break;
case R.id.btnC:
holder.btnC.setChecked(test.get(position).isChecked());
break;
case R.id.btnD:
holder.btnD.setChecked(test.get(position).isChecked());
break;
case R.id.btnE:
holder.btnE.setChecked(test.get(position).isChecked());
break;
default:
break;
}
holder.lblAnswerId.setText(position+1+"");
holder.btnWhat.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (v.isSelected())
v.setSelected(false);
else
v.setSelected(true);
}
});
return view;
}
private static class ViewHolder {
private TextView lblAnswerId;
private RadioButton btnA;
private RadioButton btnB;
private RadioButton btnC;
private RadioButton btnD;
private RadioButton btnE;
private RadioGroup rdGroup;
private Button btnWhat;
}
void showToast(String text) {
if (t != null)
t.cancel();
t = Toast.makeText(activity, text, Toast.LENGTH_SHORT);
t.show();
}
}
Pojo类
package com.example.models;
public class AnswerStateModel {
private boolean isChecked=false;
private int btnId=0;
private int currentPosition=0;
private String correctAns="";
private int btnPosition=0;
public int getBtnPosition() {
return btnPosition;
}
public void setBtnPosition(int btnPosition) {
this.btnPosition = btnPosition;
}
public String getCorrectAns() {
return correctAns;
}
public void setCorrectAns(String correctAns) {
this.correctAns = correctAns;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public int getBtnId() {
return btnId;
}
public void setBtnId(int btnId) {
this.btnId = btnId;
}
public int getCurrentPosition() {
return currentPosition;
}
public void setCurrentPosition(int currentPosition) {
this.currentPosition = currentPosition;
}
}