保存具有不同用途的单个微调器的状态

时间:2017-04-07 15:58:42

标签: java android

请在CustomAdapterClass中有一个微调器,它扩展了ArrayAdapter。即使活动被破坏,我也希望保存微调器的状态。它只有一个微调器出现在列表视图中。我已经搜索了但是我所看到的只是当有多个微调器时,每个都有它自己的id。我该如何实现这一目标。这是我的代码

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

1 个答案:

答案 0 :(得分:0)

在纯Java中,您可以将状态保存为静态字段。该活动被销毁后该值将持续存在,因为它属于Class,而不是实例。它应该在Android中运行相同。