Spinner checkbox box state get false while scroll down item and not updating adapter
<ol>
<li><p>i have custom <code>Spinner</code> with one <code>TextView</code> and one <code>CheckBox</code>. i am adding item to spinner through adapter. while i checked checkbox again it get uncheck or checked position got change. </p></li>
<li><p>I have two <code>Array</code> from the data coming from i.e product_30 or product_80</p></li>
<li>from both <code>Array</code> what ever the item selected by spinner should be display in <code>EditText</code>.</li>
</ol>

<p>code</p>

<p><strong>Array :</strong></p>

<pre><code> &lt;string-array name="product_2_30"&gt;

        &lt;item&gt;A&lt;/item&gt;
        &lt;item&gt;B&lt;/item&gt;
        &lt;item&gt;C&lt;/item&gt;
        &lt;item&gt;D&lt;/item&gt;
        &lt;item&gt;E&lt;/item&gt;
        &lt;item&gt;N&lt;/item&gt;
&lt;/string-array&gt;

&lt;string-array name="product_2_80"&gt;        
        &lt;item&gt;A1&lt;/item&gt;
        &lt;item&gt;B2&lt;/item&gt;
        &lt;item&gt;C3&lt;/item&gt;
        &lt;item&gt;D4&lt;/item&gt;
        &lt;item&gt;F5&lt;/item&gt;
        &lt;item&gt;NN&lt;/item&gt;
&lt;/string-array&gt;
</code></pre>

<p><strong>activity_main.xml</strong></p>

<pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.pankajkumar.adaptertest.MainActivity"&gt;


    &lt;RadioGroup
        android:id="@+id/rgGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"&gt;

        &lt;RadioButton
            android:id="@+id/item_2_30"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="30" /&gt;

        &lt;RadioButton
            android:id="@+id/item_2_80"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="80" /&gt;
    &lt;/RadioGroup&gt;


    &lt;LinearLayout
        android:id="@+id/spinerlayout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@+id/rgGroup"
        android:background="@drawable/insect_test"
        android:orientation="horizontal"&gt;

        &lt;Spinner
            android:id="@+id/myspinner"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="4"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:spinnerMode="dropdown" /&gt;

        &lt;ImageView
            android:id="@+id/image_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:onClick="showDropDown"
            android:paddingRight="5dp"
            android:src="@drawable/drop_down_arrow" /&gt;

    &lt;/LinearLayout&gt;

    &lt;EditText
        android:id="@+id/edt_itemList"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@+id/spinerlayout"
        android:layout_marginTop="@dimen/activity_horizontal_margin" /&gt;

&lt;/RelativeLayout&gt;
</code></pre>

<p><strong>MainActivity.java</strong></p>

<pre><code>    public class MainActivity extends AppCompatActivity {
    private String[] item_2_30, item_2_80;
    private Spinner mSpinner;
    private MyBaseAdapter adapter_30, adapter_80;
    public static ArrayList&lt;Modal&gt; modals_2_30, modals_2_80;
    private RadioGroup group;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSpinner = (Spinner) findViewById(R.id.myspinner);
        item_2_30 = getResources().getStringArray(R.array.product_2_30);
        item_2_80 = getResources().getStringArray(R.array.product_2_80);
        group = (RadioGroup) findViewById(R.id.rgGroup);
        editText = (EditText) findViewById(R.id.edt_itemList);

        modals_2_30 = AddItemToSpinner.addItem(item_2_30);
        modals_2_80 = AddItemToSpinner.addItem(item_2_80);

        adapter_30 = new MyBaseAdapter(this, modals_2_30, editText);
        adapter_80 = new MyBaseAdapter(this, modals_2_80, editText);

        mSpinner.setAdapter(adapter_30);


        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.item_2_30:
                        mSpinner.setAdapter(adapter_30);
                        break;
                    case R.id.item_2_80:
                        mSpinner.setAdapter(adapter_80);
                        break;
                }
            }
        });

    }

    public void showDropDown(View view) {
        mSpinner.performClick();
    }


}
</code></pre>

<p><strong>MyBaseAdapter.java</strong></p>

<pre><code>    public class MyBaseAdapter extends BaseAdapter implements SpinnerAdapter {
    private static final String TAG = "MyBaseAdapter";
    private Context mContext;
    private ArrayList&lt;Modal&gt; modals;
    private LayoutInflater inflater;
    private ViewHolder holder;
    private EditText editText;
    private int count = 1;
    private int po;

    public MyBaseAdapter(MainActivity mContext, ArrayList&lt;Modal&gt; modals, EditText editText) {
        this.mContext = mContext;
        this.modals = modals;
        this.editText = editText;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        Log.e(TAG, "unregisterDataSetObserver: ");
        addSelectedItem(MainActivity.modals_2_30);
        addSelectedItem(MainActivity.modals_2_80);
    }

    @Override
    public View getDropDownView(int position, View view, ViewGroup parent) {
        po = position;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.spinner_row, parent, false);
            holder.txt_item_name = (TextView) view.findViewById(R.id.txt_item_name);
            holder.chk_check_item = (CheckBox) view.findViewById(R.id.chk_item_check);
            view.setTag(holder);
            view.setTag(R.id.txt_item_name, holder.txt_item_name);
            view.setTag(R.id.chk_item_check, holder.chk_check_item);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.txt_item_name.setText(modals.get(position).getListItem());
        holder.chk_check_item.setChecked(modals.get(position).isCheck());

        holder.chk_check_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                modals.get(po).setCheck(b);
            }
        });

        holder.txt_item_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //int getposition1 = (Integer) view.getTag();
                if (modals.get(po).isCheck()) {
                    modals.get(po).setCheck(false);
                } else {
                    modals.get(po).setCheck(true);
                }

            }
        });
       // notifyDataSetChanged();
        return view;
    }

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

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        po = position;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.spinner_row, viewGroup, false);
            holder.txt_item_name = (TextView) view.findViewById(R.id.txt_item_name);
            holder.chk_check_item = (CheckBox) view.findViewById(R.id.chk_item_check);
            view.setTag(holder);
            view.setTag(R.id.txt_item_name, holder.txt_item_name);
            view.setTag(R.id.chk_item_check, holder.chk_check_item);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.txt_item_name.setText(modals.get(position).getListItem());
        holder.chk_check_item.setChecked(modals.get(position).isCheck());

//        holder.chk_check_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                modals.get(po).setCheck(b);
//            }
//        });
//
//        holder.txt_item_name.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                //int getposition1 = (Integer) view.getTag();
//                if (modals.get(po).isCheck()) {
//                    modals.get(po).setCheck(false);
//                } else {
//                    modals.get(po).setCheck(true);
//                }
//
//            }
//        });

       // notifyDataSetChanged();
        return view;
    }

    class ViewHolder {
        TextView txt_item_name;
        CheckBox chk_check_item;
    }

    public void addSelectedItem(ArrayList&lt;Modal&gt; modals) {
        for (Modal m : modals) {
            if (m.isCheck()) {
                editText.setText(m.getListItem() + "\n");
            }
        }
    }
}
</code></pre>