lundi 28 novembre 2016

Show Or Hide Widget In ListView When Checkbox Is Checked Or Unchecked Android

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;
}
}

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).




Aucun commentaire:

Enregistrer un commentaire