我在list-view中有单选按钮,它可以与我的自定义适配器一起使用。 问题是如果答案已存在于我的模型中,我想设置单选按钮。 以下是适配器类:
public class options_data_adapter extends ArrayAdapter<ChildListDataModel> {
private Activity context;
private List<ChildListDataModel> data;
private int resId;
MySharedPreferences mpref;
private RadioButton mSelectedRB;
private int mSelectedPosition = -1;
boolean[] itemChecked;
public options_data_adapter(Context context, int resource, List<ChildListDataModel> objects) {
super(context, resource, objects);
this.context = (Activity) context;
this.resId = resource;
this.data = objects;
mpref = new MySharedPreferences(context);
itemChecked = new boolean[objects.size()];
}
@Override
public View getView(final int position, View v, ViewGroup parent) {
final Holder h;// = null;
if (v == null) {
h = new Holder();
LayoutInflater inflater = context.getLayoutInflater();
v = inflater.inflate(resId, parent, false);
h.check = (CheckBox) v.findViewById(R.id.c1);
h.radio=(RadioButton)v.findViewById(R.id.r1);
h.spinner=(Spinner)v.findViewById(R.id.s1);
h.btn=(Button)v.findViewById(R.id.btn_spiner);
v.setTag(h);
} else {
h = (Holder) v.getTag();
}
Log.d("soh_answer", data.get(position).answere);
if(mpref.GetquestionType().equals("Radio")){
h.check.setVisibility(View.GONE);
h.radio.setVisibility(View.VISIBLE);
h.spinner.setVisibility(View.GONE);
h.btn.setVisibility(View.GONE);
h.radio.setText(data.get(position).answere);
if(data.get(position).checked.equals("1")){
h.radio.setBackgroundResource(R.drawable.button_background);
h.radio.setChecked(true);
}else{
h.radio.setBackgroundResource(R.drawable.button_ackgroun1);
h.radio.setChecked(false);
}
h.radio.setTag(position);
h.radio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Log.i("ID of radiobutton","Order Edit @ position : " + pos);
if (position != mSelectedPosition && mSelectedRB != null) {
mSelectedRB.setChecked(false);
((VasNBIActivity) context).removeChecked(data.get(mSelectedPosition).id);
mSelectedRB.setBackgroundResource(R.drawable.button_ackgroun1);
/*data.get(mSelectedPosition).checked="0";*/
}
mSelectedPosition = position;
((VasNBIActivity) context).addChecked(data.get(mSelectedPosition).id);
h.radio.setBackgroundResource(R.drawable.button_background);
/* data.get(mSelectedPosition).checked="1";*/
mSelectedRB = (RadioButton) v;
}
});
if (mSelectedPosition != position) {
h.radio.setChecked(false);
}else {
h.radio.setChecked(true);
if(mSelectedRB != null && h.radio != mSelectedRB){
mSelectedRB = h.radio;
}
}
}else if(mpref.GetquestionType().equals("Checkbox")){
h.check.setVisibility(View.VISIBLE);
h.radio.setVisibility(View.GONE);
h.check.setText(data.get(position).answere);
h.btn.setVisibility(View.GONE);
//=============================================================================
if(data.get(position).checked.equals("1")) {
itemChecked[position] = true;
}else{
itemChecked[position] = false;
}
if (itemChecked[position]) {
h.check.setChecked(true);
((VasNBIActivity) context).addChecked(data.get(position).id);
// h.check.setBackgroundResource(R.drawable.button_background);
}else {
h.check.setChecked(false);
}
h.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (h.check.isChecked()&& data.get(position).checked.equals("0")) {
itemChecked[position] = true;
((VasNBIActivity) context).addChecked(data.get(position).id);
// h.check.setBackgroundResource(R.drawable.button_background);
data.get(position).checked = "1";
}
else {
itemChecked[position] = false;
((VasNBIActivity) context).removeChecked(data.get(position).id);
// h.check.setBackgroundResource(R.drawable.button_ackgroun1);
data.get(position).checked="0";
}
}
});
//for spinner type questions
}else {
h.check.setVisibility(View.GONE);
h.radio.setVisibility(View.GONE);
h.spinner.setVisibility(View.VISIBLE);
h.btn.setVisibility(View.VISIBLE);
h.btn.setText(data.get(position).answere);
h.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
data.get(position).spinner_value = parent.getItemAtPosition(pos).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
return v;
}
private class Holder {
CheckBox check;
RadioButton radio;
Spinner spinner;
Button btn;
}
}
以下是单选按钮的代码:
if(mpref.GetquestionType().equals("Radio")){
h.check.setVisibility(View.GONE);
h.radio.setVisibility(View.VISIBLE);
h.spinner.setVisibility(View.GONE);
h.btn.setVisibility(View.GONE);
h.radio.setText(data.get(position).answere);
if(data.get(position).checked.equals("1")){
h.radio.setBackgroundResource(R.drawable.button_background);
h.radio.setChecked(true);
}else{
h.radio.setBackgroundResource(R.drawable.button_ackgroun1);
h.radio.setChecked(false);
}
h.radio.setTag(position);
h.radio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Log.i("ID of radiobutton","Order Edit @ position : " + pos);
if (position != mSelectedPosition && mSelectedRB != null) {
mSelectedRB.setChecked(false);
((VasNBIActivity) context).removeChecked(data.get(mSelectedPosition).id);
mSelectedRB.setBackgroundResource(R.drawable.button_ackgroun1);
/*data.get(mSelectedPosition).checked="0";*/
}
mSelectedPosition = position;
((VasNBIActivity) context).addChecked(data.get(mSelectedPosition).id);
h.radio.setBackgroundResource(R.drawable.button_background);
/* data.get(mSelectedPosition).checked="1";*/
mSelectedRB = (RadioButton) v;
}
});
if (mSelectedPosition != position) {
h.radio.setChecked(false);
}else {
h.radio.setChecked(true);
if(mSelectedRB != null && h.radio != mSelectedRB){
mSelectedRB = h.radio;
}
}
我正在尝试检查按钮,如果列表模型中的检查值= 1。
addChecked并删除已检查的功能:
public void addChecked(String val) {
checkedValue.add(val);
}
public void removeChecked(String val) {
checkedValue.remove(val);
}
我怎样才能实现这个目标?
答案 0 :(得分:0)
如果此代码中的检查值= 1,则检查按钮。
// #1
if(data.get(position).checked.equals("1")){
h.radio.setBackgroundResource(R.drawable.button_background);
h.radio.setChecked(true);
}else{
h.radio.setBackgroundResource(R.drawable.button_ackgroun1);
h.radio.setChecked(false);
}
但是,你再次改变状态这段代码
// #2
if (mSelectedPosition != position) {
h.radio.setChecked(false);
}else {
h.radio.setChecked(true);
if(mSelectedRB != null && h.radio != mSelectedRB){
mSelectedRB = h.radio;
}
}
因此,删除您的代码(#2)并再试一次