列表视图滚动期间单选按钮更改状态

时间:2012-08-21 18:04:10

标签: listview radio-button radio-group

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

}

1 个答案:

答案 0 :(得分:1)

希望我的回复还为时不晚。

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

滚动后,所选状态出错。

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

GIST:将RadioGroup对象添加到View Holder。在加载单选按钮状态之前,请确保清除单选按钮组。不要担心item.getType(),这是我项目的自定义类。只需看看((OKFHolder)持有人).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;
}