TextView と RadioGroup を持ち、各行に 2 つの RadioButton を子として持つ ListView があります。これで、各行で RadioButton を選択できるようになりました。しかし、ListView をスクロールすると、選択範囲がなくなるか、正しく表示されません。たとえば、最初の行で RadioButton A を選択し、ListView をスクロールして最初の行に戻ると、A ではなく B がチェックされます。この問題を解決するにはどうすればよいですか? 誰でも私を助けることができますか?私はそれを非常に感謝します。
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;
}
}