Skip to content Skip to sidebar Skip to footer

Listview Toggle Switch Works Only Last Row

i'm facing a problem which is, when i toggle switch it's works for only last item of listview. It doesn't matter which switch i toggle. I research other asked questions but i could

Solution 1:

The solution to the problem is you have to instead of making your data as a String, you should instead change it into something that would represent each row.

For example you can set this class to represent your row:

classItem {
    String data;
    boolean toggled;

    publicItem(String data) {
       this.data = data;
    }

    publicvoidsetToggled(boolean toggle) {
        toggled = toggle;
    }
}

Your data source now must not be Strings, so you should instead have your List as List and you can easily add your strings to the list by passing on new Item("THE STRING DATA HERE"); when adding data to the list. So instead of

StringallData= getItem(position); 

you would use

Itemitem= getItem(position);

then on the getView would look something like this:

@Overridepublic View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflaterinflater= LayoutInflater.from(getContext());
    ViewcustomView= inflater.inflate(R.layout.row, parent, false);
    Itemitem= getItem(position);


    finalSwitchstatus= (Switch) customView.findViewById(R.id.switchStatus);

    status.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
        @OverridepublicvoidonCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            // here we change the value of the item which is referring to the specific index in the array returned by getItems()
            item.setToggled(isChecked);
            Toast.makeText(getContext(),"selectid",Toast.LENGTH_LONG).show();

        }
    });

    // here we get what the latest value of the switch is
    status.setChecked(item.toggled);
    return customView;

}

Make sure you also change your data source into a data structure/list that represents somewhat like List or anything similar on which you decide to use.

BONUS TIP: It is recommended to use a ViewHolder to hold your View and for efficient recycling of instances.

classViewHolder{
    Switch statusSwitch;
    View customView;
}

Then to use that in the getView() part you can easily. You actually don't have to create a new instance of the view since convertView or the second parameter of the method represents the view that could be reused or instantiated if null.

Heterogeneous lists can specify their number of view types, so that this View is always of the right type, so you are recommended to use that.

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

    LayoutInflaterinflater= LayoutInflater.from(getContext());
    ViewHolderholder=null;
    Itemitem= getItem(position);
    if (convertView == null) { // instantiate if not yet instantiated
        convertView = inflater.inflate(R.layout.supplies_list_item, null);
        holder.statusSwitch = (Switch) convertView.findViewById(R.id.switchStatus);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }
    // you could set the values/ listeners  here
    holder.statusSwitch.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
        @OverridepublicvoidonCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            item.setToggled(isChecked);
            Toast.makeText(getContext(),"selectid",Toast.LENGTH_LONG).show();

        }
    });
    holder.statusSwitch.setChecked(item.toggled);

    return convertView;
}

Post a Comment for "Listview Toggle Switch Works Only Last Row"