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;
}
}