1
我有一個自定義適配器,我顯示文本和微調器。我試圖存儲每個微調器的狀態,以便在活動再次打開時顯示相同的項目。我嘗試過不同的事情,但我沒有成功。請如何完成此感謝。這是我曾嘗試如何獲得自定義適配器中每個微調器的狀態
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;
}
我之前使用sharedpreferences,我不知道我怎麼會保存在sharedpreferences每個微調,因爲它不只是一個微調微調從一產生布局和我使用自定義適配器來顯示它 –
看看我發佈的鏈接然後。這是Spinner的SharedPrefs的答案。 – tompadre
我以前使用過sharedpreferences。它的工作原理,但我與它的問題是,它帶回了我的情況下選擇的最後一個狀態換句話說,它選擇了最後一個旋轉器的狀態,並將其用於所有旋轉器,因此所有旋轉器都具有相同的項目。我希望他們有不同的項目 –