1. Are you ready for the Galaxy S20? Here is everything we know so far!

Need help with saving the states of checkboxes

Discussion in 'Android Development' started by SCB360, Aug 3, 2018.

  1. SCB360

    SCB360 Lurker
    Thread Starter

    I'm trying to save the selections of checkboxes when I open the app or go to a different activity, it is a simple program that shows a list of countries and flags that you can set as favorites, I just cannot see where I've gone wrong here:

    Code (Text):
    1.  
    2. public class MyAdapter extends ArrayAdapter<String> {
    3.  
    4. String[] names;
    5. int[] flags;
    6. Context mContext;
    7.  
    8. public MyAdapter(Context context, String[] countryNames, int[] countryFlags, String[] countryDetails) {
    9. super(context, R.layout.listview_item);
    10. this.names = countryNames;
    11. this.flags = countryFlags;
    12. this.mContext = context;
    13. }
    14.  
    15. [USER=1021285]@override[/USER]
    16. public int getCount() {
    17.    return names.length;
    18. }
    19.  
    20. @NonNull
    21. [USER=1021285]@override[/USER]
    22. public View getView(int position, View convertView, ViewGroup parent) {
    23.    ViewHolder mViewHolder = new ViewHolder();
    24.    final SharedPreferences sp = mContext.getSharedPreferences("FreshStart", 0);
    25.    boolean shouldBeChecked = sp.getBoolean(names[position], false);
    26.  
    27. if (convertView == null) {
    28.    LayoutInflater mInflater = (LayoutInflater) mContext.
    29.            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    30.    convertView = mInflater.inflate(R.layout.listview_item, parent, false);
    31.    mViewHolder.mFlag = (ImageView) convertView.findViewById(R.id.imageView);
    32.    mViewHolder.mName = (TextView) convertView.findViewById(R.id.textView);
    33.    mViewHolder.mCheckBox = convertView.findViewById(R.id.check_Box);
    34.  
    35. } else {
    36.    mViewHolder = (ViewHolder) convertView.getTag();
    37. }
    38. mContext.getSharedPreferences("FreshStart",0);
    39. mViewHolder.mFlag.setImageResource(flags[position]);
    40. mViewHolder.mName.setText(names[position]);
    41.  
    42. mViewHolder.mCheckBox.setTag(names[position]);
    43. convertView.setTag(mViewHolder);
    44. mViewHolder.mCheckBox.setChecked(shouldBeChecked);
    45.  
    46.  
    47. mViewHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    48.    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    49.        sp.setBoolean(names[position], isChecked);
    50.        buttonView.setSelected(isChecked);
    51.        if (isChecked)
    52.            Toast.makeText(mContext, "Saved as Favorite", Toast.LENGTH_SHORT).show();
    53.        else
    54.            Toast.makeText(mContext, "No Longer set as Favorite", Toast.LENGTH_SHORT).show();
    55.  
    56.    }
    57. });
    58.  
    59. return convertView;
    60. }
    61.  
    62. static class ViewHolder {
    63. ImageView mFlag;
    64. TextView mName;
    65. CheckBox mCheckBox;
    66. }
    67.  
    68.  
     

    Advertisement

    #1 SCB360, Aug 3, 2018
    Last edited by a moderator: Aug 3, 2018
  2. Deleted User

    Deleted User Guest

    The problem is, there is no code to save the state of the checkboxes.
     
  3. SCB360

    SCB360 Lurker
    Thread Starter

    is that not what the Boolean is doing? It seems to be line 49 thats a problem
     
  4. Deleted User

    Deleted User Guest

  5. SCB360

    SCB360 Lurker
    Thread Starter

    hmm ok, I'll take a look at that, aside from this, as a quick look is there anything in my code that doesn't seem right?
     
  6. Deleted User

    Deleted User Guest

    Only that you might want to look at RecyclerView, which has the ViewHolder pattern baked into it.
     
  7. SCB360

    SCB360 Lurker
    Thread Starter

    yea that actually was what I was going to next once I got this up and functioning
     
  8. SCB360

    SCB360 Lurker
    Thread Starter

    Hmm, I cannot work out where to put these
     
Loading...

Share This Page

Loading...