Question

I want to set a RadioGroup of ToggleButtons in my ArrayAdapter. I have two options in my list so when I select a ToggleButton to turn on I want the other to be turned off.

Here is my ArrayAdapter code:

public class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public View getDropDownView(int position, View convertView,ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final ViewHolder holder = new ViewHolder();
        View row = inflater.inflate(R.layout.psip_settings_callquality_listitem, parent, false);

        holder.itemTitle = (TextView) row.findViewById(R.id.txtCQViewItem);
        holder.itemDesc = (TextView) row.findViewById(R.id.txtCQViewDesc);
        holder.itemToggle = (ToggleButton) row.findViewById(R.id.togCQViewItem);
        holder.itemToggle.setTag(position);

        holder.itemToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton togButton, boolean isChecked) {

            }

        });

        holder.itemTitle.setText(sSettingTitles.get(position));
        holder.itemDesc.setText(sSettingDesc.get(position));

        return row;
    }

    private class ViewHolder {
        TextView itemTitle;
        TextView itemDesc;
        ToggleButton itemToggle;
    }
}

XML

<TextView
    android:id="@+id/txtCQViewItem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textColor="@color/MiscBtnClick"
    android:layout_margin="5dp" />

<TextView
    android:id="@+id/txtCQViewDesc"
    android:layout_width="wrap_content"
    android:layout_below="@+id/txtCQViewItem"
    android:layout_height="wrap_content"
    android:textColor="@color/MiscBtnClick"
    android:layout_toLeftOf="@+id/togCQViewItem"
    android:layout_margin="5dp" />

<ToggleButton
    android:id="@+id/togCQViewItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtCQViewItem"
    android:layout_alignParentRight="true" />

Any help would be great. Thanks

Was it helpful?

Solution

private int selection = -1;
ViewHolder holder = null;
if(convertView == null){
    holder = new ViewHolder()
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View row = inflater.inflate(R.layout.psip_settings_callquality_listitem, parent, false);
    holder.itemTitle = (TextView) row.findViewById(R.id.txtCQViewItem);
    holder.itemDesc = (TextView) row.findViewById(R.id.txtCQViewDesc);
    holder.itemToggle = (ToggleButton) row.findViewById(R.id.togCQViewItem);
    convertView = row;
    convertView.setTag(holder);
}else{
    holder = (ViewHolder)convertView.getTag();
}

holder.itemToggle.setTag(position);
holder.itemToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton togButton, boolean isChecked) {
        if(isChecked && position != selection){
            selection = position;
            notifyDataSetChnaged();
        }else if(!isChecked && position == selection){
            selection = -1
        }
    }
});
holder.itemToggle.setChecked(position == selection);
holder.itemTitle.setText(sSettingTitles.get(position));
holder.itemDesc.setText(sSettingDesc.get(position));
return convertView;

You don't need to create views each and every time since ListView re uses the view. Follow the code as I suggested and let me know it is working or not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top