选中或取消选中Android时在ListView中显示或隐藏小部件 [英] Show Or Hide Widget In ListView When Checkbox Is Checked Or Unchecked Android

查看:75
本文介绍了选中或取消选中Android时在ListView中显示或隐藏小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的ArrayAdapter类,

Here is my ArrayAdapter Class,

public class HAAR extends ArrayAdapter<HAM> {

Context context;
ArrayList<HAM> selectedhospital;
ViewHolder viewHolder = null;

public HAAR(Context context, int resourceId,
                                        ArrayList<HAM> selectedhospital) {
    super(context, resourceId, selectedhospital);
    this.context = context;
    this.selectedhospital = new ArrayList<HAM>();
    this.selectedhospital.addAll(selectedhospital);
}

// View lookup cache
private static class ViewHolder {
    TextView name;
    TextView home;
    CheckBox chk_hospitallist;
    RadioButton rbt_primarylocation;

}


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

    final ArrayList<String> hospitalid = new ArrayList<>();

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        // If there's no view to re-use, inflate a brand new view for row
        convertView = mInflater.inflate(R.layout.ha_list_item_name, null);
        viewHolder = new ViewHolder();

    viewHolder.name = (TextView) convertView.findViewById(R.id.txt_ha_name);
        viewHolder.home = (TextView) convertView.findViewById(R.id.txt_ha_town);
        viewHolder.chk_hospitallist = (CheckBox) convertView.findViewById(R.id.chk_box_ha_list);

        final View finalConvertView = convertView;
        viewHolder.chk_hospitallist.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                CheckBox cb = (CheckBox) v;
                viewHolder.rbt_primarylocation = (RadioButton) finalConvertView.findViewById(R.id.rbtn_pl);
                HAM _state = (HAM) cb.getTag();

                if(cb.isChecked())
                {
                    _state.setSelected(cb.isChecked());
                    hospitalid.add( _state.getId().toString());
                    viewHolder.rbt_primarylocation.setVisibility(View.VISIBLE);

                    Toast.makeText(
                            v.getContext(),
                            "Clicked on If: " + _state.getId().toString() + " is "
                                    + cb.isChecked(), Toast.LENGTH_LONG).show();
                }
                else
                {
                    _state.setSelected(false);
                    hospitalid.remove( _state.getId().toString());
                    viewHolder.rbt_primarylocation.setVisibility(View.INVISIBLE);

                    Toast.makeText(
                            v.getContext(),
                            "Clicked on Else: " + _state.getId().toString() + " is "
                                    + cb.isChecked(), Toast.LENGTH_LONG).show();
                }
            }
        });

        // Cache the viewHolder object inside the fresh view
        convertView.setTag(viewHolder);

    } else {
        // View is being recycled, retrieve the viewHolder object from tag
        viewHolder = (ViewHolder) convertView.getTag();
    }

    HAM state = selectedhospital.get(position);

    viewHolder.name.setText(state.getName());
    viewHolder.home.setText(state.getAddress1()+"\r\n"+ state.getCity()+", "+state.getState()+" "+state.getZip());

    viewHolder.chk_hospitallist.setChecked(state.isSelected());

    viewHolder.chk_hospitallist.setTag(state);



    return convertView;
}
}

在这段代码中,当试图选中或取消选中Checkbox时,我试图隐藏和显示RadioButton.Sametime,我也想从数组中添加或删除选中/未选中的Checkbox值.但是,当我运行此代码时,它无法正常工作.当我选择了一个复选框(从第一个视图)时,另一个视图的单选按钮变为可见,而不是第一个视图.还有一件事,我也只想选择一个单选按钮(当我选择了两个以上的复选框时).

In this code, I am trying to hide and show RadioButton when Checkbox is checked or unchecked.Same time I am also want to add or remove selected/unselected checkbox value from an array. But when I am run this code it's not working properly. When I have selected one checkbox (from first view) the other view's radio button is got visible instead of first view. One more thing I also want select only one radio button (when I have selected more than 2 checkboxes).

推荐答案

请尝试以下操作:

public class HAAR extends ArrayAdapter<HAM> {

Context context;
ArrayList<HAM> selectedhospital;
ArrayList<String> hospitalid = new ArrayList<String>();
int clickedRadioButtonPosition = -1;

ViewHolder viewHolder = null;

public HAAR(Context context, int resourceId, ArrayList<HAM> selectedhospital) {
    super(context, resourceId, selectedhospital);
    this.context = context;
    this.selectedhospital = new ArrayList<HAM>();
    this.selectedhospital.addAll(selectedhospital);
}

// View lookup cache
private static class ViewHolder {
    TextView name;
    TextView home;
    CheckBox chk_hospitallist;
    RadioButton rbt_primarylocation;

}

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

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        // If there's no view to re-use, inflate a brand new view for row
        convertView = mInflater.inflate(R.layout.ha_list_item_name, null);
        viewHolder = new ViewHolder();

        viewHolder.name = (TextView) convertView.findViewById(R.id.txt_ha_name);
        viewHolder.home = (TextView) convertView.findViewById(R.id.txt_ha_town);
        viewHolder.chk_hospitallist = (CheckBox) convertView.findViewById(R.id.chk_box_ha_list);
        viewHolder.rbt_primarylocation = (RadioButton) convertView.findViewById(R.id.rbtn_pl);

        viewHolder.chk_hospitallist.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                CheckBox cb = (CheckBox) v;
                View finalConvertView = (View) v.getParent();
                RadioButton rbt_primarylocation = (RadioButton) finalConvertView.findViewById(R.id.rbtn_pl);
                int pos = (Integer) v.getTag();
                HAM _state = (HAM) selectedhospital.get(pos);

                if(cb.isChecked())
                {
                    _state.setSelected(cb.isChecked());
                    // Update ArrayList
                    selectedhospital.remove(pos);
                    selectedhospital.add(pos, _state);

                    hospitalid.add( _state.getId().toString());
                    rbt_primarylocation.setVisibility(View.VISIBLE);

                    Toast.makeText(
                            v.getContext(),
                            "Clicked on If: " + _state.getId().toString() + " is "
                                    + cb.isChecked(), Toast.LENGTH_LONG).show();
                }
                else
                {
                    _state.setSelected(false);
                    hospitalid.remove( _state.getId().toString());

                    // When unchecked the one with radio button selected
                    if(rbt_primarylocation.isChecked()){
                        if(hospitalid.size() > 0){
                            Toast.makeText(
                                    v.getContext(),
                                    "Please select an radio button", Toast.LENGTH_LONG).show();
                        }
                    }

                    rbt_primarylocation.setVisibility(View.INVISIBLE);

                    Toast.makeText(
                            v.getContext(),
                            "Clicked on Else: " + _state.getId().toString() + " is "
                                    + cb.isChecked(), Toast.LENGTH_LONG).show();
                }
            }
        });
        viewHolder.rbt_primarylocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickedRadioButtonPosition = (Integer) v.getTag();
                notifyDataSetChanged();
            }
        });
        // Cache the viewHolder object inside the fresh view
        convertView.setTag(viewHolder);
    } else {
        // View is being recycled, retrieve the viewHolder object from tag
        viewHolder = (ViewHolder) convertView.getTag();
    }

    HAM state = selectedhospital.get(position);

    viewHolder.name.setText(state.getName());
    viewHolder.home.setText(state.getAddress1()+"\r\n"+ state.getCity()+", "+state.getState()+" "+state.getZip());
    viewHolder.chk_hospitallist.setChecked(state.isSelected());

    // Set views for view recycling on scrolling
    if(viewHolder.chk_hospitallist.isChecked()){
        viewHolder.rbt_primarylocation.setVisibility(View.VISIBLE);
    }else{
        viewHolder.rbt_primarylocation.setVisibility(View.INVISIBLE);
    }
    if(position == clickedRadioButtonPosition){
        viewHolder.rbt_primarylocation.setChecked(true);
    }else{
        viewHolder.rbt_primarylocation.setChecked(false);
    }

    viewHolder.chk_hospitallist.setTag(position);
    viewHolder.rbt_primarylocation.setTag(position);
    return convertView;
}

public String getRBClickedItem(){
    // Not sure your list will change or not (e.g. from server)
    // Instead of saving position, save something unique (e.g. Id).
    return selectedhospital.get(clickedRadioButtonPosition).getId();
}

public ArrayList<String> getCheckedList(){
    return hospitalid;
}

public void updateWithSavedList(ArrayList<String> savedList, String savedRBItemId){
    hospitalid.clear();
    hospitalid.addAll(savedList);
    // Not sure your list will change or not (e.g. from server)
    // Saved Id, so now search for its position.
    if(!savedRBItemId.equals("")){
        int tmpSearchPosition = -1;
        for(int i=0; i<selectedhospital.size(); i++){
            if(selectedhospital.get(i).getId().equals(savedRBItemId)){
                tmpSearchPosition = i;
            }
        }
        if(tmpSearchPosition != -1) clickedRadioButtonPosition = tmpSearchPosition;
    }
    notifyDataSetChanged();
}
}

希望获得帮助!

这篇关于选中或取消选中Android时在ListView中显示或隐藏小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆