1

I am using a simple adapter for showing list, but switch changes its state while scrolling listview:

  lv = (ListView) findViewById(R.id.listView);
    String[] from = {"CompanyName", "AppointmenTime", "Status"};
    // view id's to which data to be binded
    int[] to = {R.id.clientname, R.id.time, R.id.togglebtn};

    //Creating Adapter
    android.widget.SimpleAdapter k = new android.widget.SimpleAdapter(ClientListActivity.this, clientData, R.layout.client_list, from, to) {
        public View getView(int position, View convertView, ViewGroup parent) {

    View v = super.getView(position, convertView, parent);
            Switch switchcompact=(Switch) v.findViewById(R.id.togglebtn);
            switchcompact.setTag(position);

       switchcompact.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                        {
 ////some code
    }}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mayuri
  • 121
  • 10
  • use Custom Adapter and store the `toggle value` into `boolean array` ........ – sushildlh Oct 14 '16 at 07:11
  • Possible duplicate of [checkbox unchecked when i scroll listview in android](http://stackoverflow.com/questions/10895763/checkbox-unchecked-when-i-scroll-listview-in-android) – hrskrs Oct 14 '16 at 07:13
  • try to put the default value if togglebtn and set values in adapter as during scroll the valued my interchange – Himank shah Oct 14 '16 at 07:15
  • is it not possible using simple adapter ,i have to use custom adapter for this. – Mayuri Oct 14 '16 at 07:21

2 Answers2

0

If you are scrolling, the list items will be create and destroyed if they are not visible. So you have to save the state of the Switch:

private Map<Integer, Boolean> states = new Hashmap<>();

public View getView(final int position, View convertView, ViewGroup parent) {
    View v = super.getView(position, convertView, parent);
    Switch switchcompact = (Switch) v.findViewById(R.id.togglebtn);
    switchcompact.setChecked(Boolean.TRUE.equals(states.get(position));
    switchcompact.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            states.put(position, isChecked);
        }
    });
}

But, this is just pseudocode. I strongly recommend you to use SharedPreferences to save and get the selection state.

alex
  • 8,904
  • 6
  • 49
  • 75
0

You have to update the value of particular Item in ArrayList(or dataset provided to listview) after switch state changed.

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.Switch;

import java.util.List;

import java.util.Map;

public class SimpleListViewAdaptor extends BaseAdapter{

Context context;
List<Map<String, Object>> data;
public SimpleListViewAdaptor(Context context, List<Map<String, Object>> data) {
    super();
    this.context=context;
    this.data=data;

}

@Override
public Map<String,Object> getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getCount() {
    return data.size();
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
      HolderView holderView;
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.customr_item_list,parent, false);
        holderView = new HolderView();
        holderView.aSwitch = (Switch) convertView.findViewById(R.id.togglebtn);
        convertView.setTag(holderView);
    } else {
        // View recycled !
        // no need to inflate
        // no need to findViews by id
        holderView = (HolderView) convertView.getTag();
    }

        holderView.aSwitch.setChecked((Boolean)data.get(position).get("Status"));
    holderView.aSwitch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            if(((Switch)v).isChecked()){
                ((Switch)v).setChecked(true);
                data.get(position).put("Status",true);//this is imp to update the value in dataset which is provided to listview

            }else{
                ((Switch)v).setChecked(false);
                data.get(position).put("Status",false);

            }
        }
    });
    return convertView;
}

static class HolderView{
    Switch aSwitch;
}

}

Prashant
  • 66
  • 9