title: Saving the states of a checkbox in a list view
body: <p>I'm fairly new to Android development and I need some help.</p>
  
  <p>What I want to do is place some <code>checkboxes</code> next to items in my listview and save those selections when I close &amp; restart the app, I've tried and been confused by the <code>sharedPreferences</code> and recycler view parts and was hoping someone could help me with this, here is the code I'm using:</p>
  
  <p>MainActivity:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  Toolbar mToolbar;
  ListView mListView;
  TextView mTextView;
  
  String[] countryNames = {"Canberra, Australia", "Sydney, Australia" 
  ,"Brazil", "China", "France", "Germany", "India", "Italy"
              , "Mexico", "Russia", "Spain", "US"};
  
      int[] countryFlags = {R.drawable.flag_australia,
              R.drawable.flag_australia,
              R.drawable.flag_brazil,
              R.drawable.flag_china,
              R.drawable.flag_france,
              R.drawable.flag_germany,
              R.drawable.flag_india,
              R.drawable.flag_italy,
              R.drawable.flag_mexico,
              R.drawable.flag_russia,
              R.drawable.flag_spain,
              R.drawable.flag_us};
  
      String [] countryDetails = {// Canberra, Australia
  
  "Capital of Australia" + System.lineSeparator()+"Population - 
  356,585" + " " + System.lineSeparator() +"Nearest Airport : Canberra 
  Airport"+ System.lineSeparator(),
  
  //Sydney Australia
  "Major City in Australia" + 
  System.lineSeparator()+"Population: 4.02 Million" + System.lineSeparator() 
  +" Nearest Airport: Western Sydney Airport",
              "Brazil Here",
              "Chine Here",
              "France Here",
              "Germany Here",
              "India Here",
              "Italy Here",
              "Mexico",
              "Russia",
              "Spain",
              "US" };
  
      @Override
  protected void onCreate(Bundle savedInstanceState) {
  
  
  super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
  
              mToolbar = (Toolbar) findViewById(R.id.toolbar);
              mToolbar.setTitle(getResources().getString(R.string.app_name));
  
              mListView = (ListView) findViewById(R.id.listview);
              mTextView = (TextView) findViewById(R.id.textView);
  
              MyAdapter myAdapter = new MyAdapter(MainActivity.this, 
              countryNames, countryFlags, countryDetails);
  
              mListView.setAdapter(myAdapter);
              mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
                      Intent mIntent = new Intent(MainActivity.this, DetailActivity.class);
                      mIntent.putExtra("countryName", countryNames[i]);
                      mIntent.putExtra("countryFlag", countryFlags[i]);
                      mIntent.putExtra("countryDetails", countryDetails[i]);
                      startActivity(mIntent);
                  }
              });
          }
      }
  </code></pre>
  
  <p>My XML for this layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal"
      android:descendantFocusability="blocksDescendants"&gt;
  
      &lt;ImageView
          android:id="@+id/imageView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:padding="5dp"
          app:srcCompat="@mipmap/ic_launcher" /&gt;
  
      &lt;TextView
          android:id="@+id/textView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="TextView"
          android:textAppearance="@style/TextAppearance.AppCompat.Title"
         /&gt;
  
      &lt;CheckBox
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:focusable="false"
          android:text="Favorite" /&gt;
  
  &lt;/LinearLayout&gt;             
  </code></pre>
  
  <p>Not sure what else to include at this point, any assistance will be appreciated</p>
  
  <p>Alright I managed to get to this point for the Custom Adapter</p>
  
  <pre><code>  public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder mViewHolder = new ViewHolder();
  
          if (convertView == null) {
              LayoutInflater mInflater = (LayoutInflater) mContext.
              getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = mInflater.inflate(R.layout.listview_item, 
  parent, false);
              mViewHolder.mFlag = (ImageView) 
  convertView.findViewById(R.id.imageView);
              mViewHolder.mName = (TextView) 
  convertView.findViewById(R.id.textView);
              mViewHolder.mCheckBox = 
  convertView.findViewById(R.id.check_Box);
  
          } else {
              mViewHolder = (ViewHolder) convertView.getTag();
          }
          mContext.getSharedPreferences("FreshStart",0);
          mViewHolder.mFlag.setImageResource(flags[position]);
          mViewHolder.mName.setText(names[position]);
  
          SharedPreferences sp = mContext.getSharedPreferences("FreshStart", 0);
          boolean shouldBeChecked = sp.getBoolean(names[position], false);
          mViewHolder.mCheckBox.setTag(names[position]);
          convertView.setTag(R.layout.listview_item);
          mViewHolder.mCheckBox.setChecked(shouldBeChecked);
  
  
          mViewHolder.mCheckBox.setOnCheckedChangeListener(new 
          CompoundButton.OnCheckedChangeListener() {
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  
                  if (isChecked) {
                      buttonView.setChecked(true);
                      Toast.makeText(mContext, "Saved as Favorite", Toast.LENGTH_SHORT).show();
                  }
                  else{
                      Toast.makeText(mContext, "No Longer set as Favorite", Toast.LENGTH_SHORT).show();
                  }
  
              }
          });
  
          return convertView;
      }
  static class ViewHolder {
      ImageView mFlag;
      TextView mName;
      CheckBox mCheckBox;
  }
  </code></pre>
  
  <p>But now I have an issue where I cannot save the checkbox states when I scroll or change activity, unsure of how to do that part now in the if(isChecked) section</p>