【问题标题】:how to get the state of each spinner in a custom adapter如何在自定义适配器中获取每个微调器的状态
【发布时间】:2017-05-12 03:04:37
【问题描述】:

我有一个自定义适配器,用于显示文本和微调器。我试图存储每个微调器的状态,以便在活动再次打开时显示相同的项目我尝试了不同的东西,但我没有成功。请我如何完成这个谢谢。这是我最近尝试过的东西

    CustomListAdapter(Context context, ArrayList<String> subjects) {
    super(context, R.layout.custom_list_view, subjects);
}



@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    LayoutInflater layoutInflater = LayoutInflater.from(getContext());
    View customView = layoutInflater.inflate(R.layout.custom_list_view, parent, false);

    singleSubject = getItem(position);


    TextView singleText = (TextView) customView.findViewById(R.id.listSubjectsMyCourses);
    colorLayout = (LinearLayout)customView.findViewById(R.id.colorForSubjects);
    relativeLayout = (RelativeLayout) 
   customView.findViewById(R.id.relativeForView);
    parentLayout = (RelativeLayout) 
   customView.findViewById(R.id.parentLayout);
    points = new ArrayList<>();
    selected = new ArrayList<>();
    selectedsttring = new ArrayList<>();
 customView.findViewById(R.id.textViewForGrades);
    tinyDB = new TinyDB(getContext());



         spinnerForGradePoints = (Spinner)customView.findViewById(R.id.spinnerForGrades);
    final ArrayAdapter<String> gradePointAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_dropdown_item_1line, UserCourseSelection2.userSubjectGradePoint);
    spinnerForGradePoints.setAdapter(gradePointAdapter);

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

            tinyDB.putInt("selected", selectedItempos);


        }



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

        }
    });

  singleText.setText(singleSubject);
    colorLayout.setBackgroundColor(UserCourseSelection2.userSubjectsListColor.get(position));
    int getSelected = tinyDB.getInt("selected");
    spinnerForGradePoints.setSelection(getSelected);

   return customView;

}

【问题讨论】:

    标签: java android listview


    【解决方案1】:

    SharedPreferences 是如果您不想在应用程序级别使用依赖注入(如 dagger)或某种自定义本地缓存的方式。

    对于微调器中的 SharedPreferences,请检查以下答案:

    https://stackoverflow.com/a/13431729/5577679

    【讨论】:

    • 我之前使用过 sharedpreferences,我不知道如何将每个微调器保存在 sharedpreferences 中,因为微调器不仅仅是一个微调器,它是从布局生成的,我正在使用自定义适配器来显示它
    • 查看我当时发布的链接。这就是 Spinner 的 SharedPrefs 的答案。
    • 我以前使用过共享首选项。它有效,但我遇到的问题是它带回了在我的情况下选择的最后一个的状态,换句话说,它采用了最后一个选择的微调器的状态并将其用于所有微调器,因此所有微调器都具有相同的项目。我希望他们有不同的项目
    • 我刚刚编辑了代码,这就是我使用 sharedPrefrences 的方式,它是 tinyDB 只是将 tinyDB 作为 sharedprefrences,它们是一样的。我尝试使用循环来查看是否可以使它们具有不同的值,但无济于事。我愿意接受建议
    • 在这件事上我可以同意你的看法,但恐怕不能再帮助你了。在每种情况下,您都应该有某种缓存,这是肯定的。您决定使用哪一个。
    猜你喜欢
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多