0

Please i have a spinner in a CustomAdapterClass which extends ArrayAdapter. I want to save the state of the spinner even when the activity is destroyed. It is only one spinner that appears in a listview. I have searched but all i see is when there are more than one spinner, each having it's own id. How do i achieve this please. Here is my code

public class CustomListAdapterForCgpa extends ArrayAdapter<String> implements AdapterView.OnItemSelectedListener {

LinearLayout colorLayout;
TextView userGpScoreUneditable;
Spinner spinnerForGradePoints;
public String mathSubject= "Mathematics";
public String chemSubject = "Chemistry";
public String physicsSubject = "Physics";
public String biologySubject = "Biology";
public String gsSubject = "General Studies";
public String cscSubject = "CSC";
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;


public CustomListAdapterForCgpa(Context context, ArrayList<String> subjects) {
    super(context, R.layout.custom_list_view_cgpa, subjects);
}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater layoutInflater = LayoutInflater.from(getContext());
    View customView = layoutInflater.inflate( R.layout.custom_list_view_cgpa,parent,false);

    String singleSubject = getItem(position);


    TextView singleText = (TextView) customView.findViewById(R.id.listSubjectsMyCoursesCgpa);
    colorLayout = (LinearLayout)customView.findViewById(R.id.colorForSubjectsCgpa);
    userGpScoreUneditable = (TextView)customView.findViewById(R.id.userGpScoreUneditable);
    spinnerForGradePoints = (Spinner)customView.findViewById(R.id.spinnerForGradePointCgpa);

    ArrayAdapter<String> gradePointAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, TabFirstSemesterMyCGPA.userSubjectGrade);
    spinnerForGradePoints.setAdapter(gradePointAdapter);

    spinnerForGradePoints.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            String selectedItem = adapterView.getItemAtPosition(i).toString();


            if (selectedItem.equals("A"))
            {
                Toast.makeText(getContext(), "You Clicked A", Toast.LENGTH_SHORT).show();
            }else if ( selectedItem.equals("B"))
            {
                Toast.makeText(getContext(), "You Clicked B", Toast.LENGTH_SHORT).show();

            }else if ( selectedItem.equals("C"))
            {
                Toast.makeText(getContext(), "You Clicked C", Toast.LENGTH_SHORT).show();

            }else if ( selectedItem.equals("D"))
            {
                Toast.makeText(getContext(), "You Clicked D", Toast.LENGTH_SHORT).show();


            }else if ( selectedItem.equals("E"))
            {
                Toast.makeText(getContext(), "You Clicked E", Toast.LENGTH_SHORT).show();

            }else if ( selectedItem.equals("F"))
            {
                Toast.makeText(getContext(), "You Clicked F", Toast.LENGTH_SHORT).show();

            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });


    singleText.setText(singleSubject);
    colorLayout.setBackgroundColor(UserCourseSelection2.userSubjectsListColor.get(position));



    return customView;
   }
  }
Emmanuel Okocha
  • 377
  • 2
  • 8
  • 17

1 Answers1

0

In pure Java you could save the state as a static field. The value will persist after the activity is destroyed because it belongs to the Class, not an instance. It should work the same in Android.

BinaryDigit09
  • 110
  • 10
  • the right way to do this on Android is using `SharedPreferences`. http://stackoverflow.com/questions/2475978/using-static-variables-in-android explains that static variables are associated with the ClassLoader, which is reclaimed when the app is finally destroyed - in other words if you use statics this way, you'll still need to persist and restore those values somehow. – trooper Apr 07 '17 at 16:39
  • So does anyone have any idea how i will go about this – Emmanuel Okocha Apr 08 '17 at 08:06