我有一个 ListView,它有一个 TextView 和一个 RadioGroup,每行有 2 个 RadioButtons 作为子项。现在我可以在每一行中选择一个 RadioButton。但是如果我滚动 ListView,我的选择就消失了,或者它不能正确显示。例如,我在第一行选择 RadioButton A,如果我滚动 ListView 然后再次返回第一行 B 被选中而不是 A。我该如何解决这个问题?有谁能够帮我?我会很感激的。
package com.rextart.org.mico.view.adapter;
import java.sql.SQLException;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.TextView;
import com.rextart.org.mico.R;
import com.rextart.org.mico.dao.entity.TabNonConf;
import com.rextart.org.mico.dao.entity.TabSopralluoghi;
import com.rextart.org.mico.dao.service.TabNonConfService;
import com.rextart.org.mico.dao.service.TabSopralluoghiService;
import com.rextart.org.mico.utils.StoreForArrTabNonConf;
public class ListaNonConformitaAdapter extends ArrayAdapter<TabNonConf> {
private ArrayList<TabNonConf> arrNonConf;
private LayoutInflater mInflater;
private Context context;
private boolean visible;
private TabSopralluoghi objSop;
protected ViewHolder holder;
public ListaNonConformitaAdapter(Context context, TabSopralluoghi objSop, int resource, ArrayList<TabNonConf> arrNonConf,boolean visible) {
super(context, resource, arrNonConf);
this.context = context;
this.arrNonConf = arrNonConf;
this.visible = visible;
this.objSop = objSop;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final TabNonConf tab = arrNonConf.get(position);
if (convertView == null){
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.adapter_item_non_conformita, null);
}
holder = new ViewHolder();
holder.conforme = (RadioButton)convertView.findViewById(R.id.conf_si);
holder.nonConforme = (RadioButton)convertView.findViewById(R.id.conf_no);
holder.nomeNonConf = (TextView)convertView.findViewById(R.id.nome_non_conformita);
holder.indice = (TextView)convertView.findViewById(R.id.numero_non_conformita);
holder.elimina = (ImageButton)convertView.findViewById(R.id.elimina_non_conf);
setclickableCheck(holder);
if(visible){
holder.elimina.setVisibility(View.VISIBLE);
}else{
holder.elimina.setVisibility(View.INVISIBLE);
}
if(tab.getDescNonConf().length()>25){
holder.nomeNonConf.setText(tab.getDescNonConf().substring(0, 25)+" ...");
}else{
holder.nomeNonConf.setText(tab.getDescNonConf());
}
int posizione = position+1;
holder.indice.setText(saveString(""+posizione));
if(tab.getFlagOk()==1){
holder.conforme.setChecked(true);
holder.nonConforme.setChecked(false);
}else{
holder.conforme.setChecked(false);
holder.nonConforme.setChecked(true);
}
holder.conforme.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
tab.setFlagOk(1);
}else{
tab.setFlagOk(0);
}
StoreForArrTabNonConf.getInstance().add(tab);
}
});
holder.elimina.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder((Activity)context);
alert.setTitle("");
alert.setCancelable(true);
alert.setMessage(R.string.eliminazione);
alert.setPositiveButton(R.string.label_ok,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
try {
TabNonConfService objConfService = new TabNonConfService(((Activity)context).getApplication());
objConfService.delete(tab);
ArrayList<TabNonConf> arr = objConfService.getList(""+objSop.getId());
if(arr.isEmpty()){
TabSopralluoghiService objService = new TabSopralluoghiService(((Activity)context).getApplication());
objService.delete(objSop);
}
ListaNonConformitaAdapter.this.remove(ListaNonConformitaAdapter.this.getItem(position));
notifyDataSetChanged();
} catch (SQLException e) {}
}
});
alert.setNegativeButton(R.string.label_annulla,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alert.show();
}
});
return convertView;
}
/**
* @description method to set if the radiobuttons are clickable or not
* @param holder
*/
protected void setclickableCheck(ViewHolder holder) {
holder.conforme.setClickable(true);
holder.nonConforme.setClickable(true);
}
/**
* @description method to return the value of a object's field or an empty string
* @param field
* @return field from the object or space
*/
private String saveString(String field){
if (field == null)
return "";
else
return field;
}
static class ViewHolder{
RadioButton conforme;
RadioButton nonConforme;
TextView nomeNonConf;
TextView indice;
ImageButton elimina;
}
}