0

我有一个 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;
}

}

4

1 回答 1

1

希望我的回复不会太晚。

我有几天同样的问题。具有文本视图和 2 个单选按钮的自定义适配器。

滚动后,所选状态出错。

在尝试了很多方法后,我解决了这个问题。

GIST:将 RadioGroup 对象添加到您的视图持有者。确保在加载单选按钮状态之前清除单选组。不用担心 item.getType(),这是我项目的自定义类。只需查看 ((OKFHolder)holder).radioGroup.clearCheck();

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    ChecklistItemHolder holder = null;

    ChecklistItem item = objects.get(position);

    if(row == null) {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(R.layout.temp_okf_listview_item, parent, false);

        holder = new OKFHolder();
        holder.position = position;
        ((OKFHolder)holder).radioGroup = (RadioGroup)row.findViewById(R.id.temp_okf_radio_group);
        ((OKFHolder)holder).okButton = (RadioButton)row.findViewById(R.id.temp_okf_radio_ok);
        ((OKFHolder)holder).okButton.setOnClickListener(this);
        ((OKFHolder)holder).failButton = (RadioButton)row.findViewById(R.id.temp_okf_radio_fail);
        ((OKFHolder)holder).failButton.setOnClickListener(this);
        holder.titleText = (TextView)row.findViewById(R.id.temp_okf_text);
        row.setTag(holder);
    }

    else {
        holder = (ChecklistItemHolder)row.getTag();
    }

    if(item.getType() == ChecklistItem.OKF_ONLY) {
        Option option = item.getOptions().get(0);

        ((OKFHolder)holder).radioGroup.clearCheck();

        if(option.getValue() == Integer.valueOf(Option.OKF_F)) {
            ((OKFHolder)holder).failButton.setChecked(true);
        }

        else if (option.getValue() == Integer.valueOf(Option.OKF_OK)) {
            ((OKFHolder)holder).okButton.setChecked(true);
        }
    }

    holder.titleText.setText(item.getTitle());

    return row;
}

static class ChecklistItemHolder {
    int position;
    TextView titleText;
}

static class OKFHolder extends ChecklistItemHolder {
    RadioGroup radioGroup;
    RadioButton okButton;
    RadioButton failButton;
}
于 2012-11-06T05:22:40.273 回答