检索微调器选择的项目以在条件语句中使用
Retrieve Spinner Selected Item To Use in Condition Statement
我可以保存微调器选择的项目状态。我只是在发送和检索要在下一个 activity.
的 if 语句中使用的选定项目时遇到问题
我知道我必须使用 sharedpreferences,但在涉及微调项时我遇到了一些麻烦。
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
LastSelectedItem = getSharedPreferences("PriorSelected", MODE_PRIVATE);
go_back_btn = (Button) findViewById(R.id.go_back_btn);
themeSpinner = (Spinner) findViewById(R.id.themeSpinner);
go_back_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LastSelectedItem = getSharedPreferences("PriorSelected", MODE_PRIVATE);
SharedPreferences.Editor editor = LastSelectedItem.edit();
editor.apply();
Intent intent = new Intent(getApplicationContext(), home.class);
startActivity(intent);
}
});
int LastSelection = LastSelectedItem.getInt("LastSelection", 0);
editor = LastSelectedItem.edit();
ArrayAdapter<CharSequence> themeAdapter = ArrayAdapter.createFromResource(settings.this, R.array.theme_array, android.R.layout.simple_spinner_item);
themeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
themeSpinner.setAdapter(themeAdapter);
themeSpinner.setSelection(LastSelection);
themeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
editor.putInt("LastSelection", position).apply();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
SharedPreferences settings = getSharedPreferences("app_pref", 0);
SharedPreferences.Editor editor = settings.edit();
themeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//To save
editor.putInt("position",position);
editor.commit();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//To retrieve in next activity
SharedPreferences settings = getSharedPreferences("app_pref", 0);
int snowDensity = settings.getInt("position", -1); //0 is the default value
您可以将值存储到 int 并将其传递给下一个 activity。
,而不是共享偏好
int position=-1;
themeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
position=position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
position=-1;
}
});
go_back_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), home.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
第二个Activity:
int position= getIntent().getIntExtra("position", -1);
很好,我找到了答案
// send data to next activity
go_back_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int Myposition = themeSpinner.getSelectedItemPosition();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("LastSelection", Myposition);
editor.apply();
Intent intent = new Intent(getApplicationContext(), home.class);
startActivity(intent);
下面是要在下一个 activity 上检索的代码:
// Retrieve and implement conditional statement
private void ToggleTheme() {
final SharedPreferences LastSelectedItem = getApplicationContext().getSharedPreferences("PriorSelected", Context.MODE_PRIVATE);
int LastSelection = LastSelectedItem.getInt("LastSelection", 0);
if (LastSelection == 1) {
homebutton.setBackgroundResource(R.drawable.light_theme_buttons);
我可以保存微调器选择的项目状态。我只是在发送和检索要在下一个 activity.
的 if 语句中使用的选定项目时遇到问题我知道我必须使用 sharedpreferences,但在涉及微调项时我遇到了一些麻烦。
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
LastSelectedItem = getSharedPreferences("PriorSelected", MODE_PRIVATE);
go_back_btn = (Button) findViewById(R.id.go_back_btn);
themeSpinner = (Spinner) findViewById(R.id.themeSpinner);
go_back_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LastSelectedItem = getSharedPreferences("PriorSelected", MODE_PRIVATE);
SharedPreferences.Editor editor = LastSelectedItem.edit();
editor.apply();
Intent intent = new Intent(getApplicationContext(), home.class);
startActivity(intent);
}
});
int LastSelection = LastSelectedItem.getInt("LastSelection", 0);
editor = LastSelectedItem.edit();
ArrayAdapter<CharSequence> themeAdapter = ArrayAdapter.createFromResource(settings.this, R.array.theme_array, android.R.layout.simple_spinner_item);
themeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
themeSpinner.setAdapter(themeAdapter);
themeSpinner.setSelection(LastSelection);
themeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
editor.putInt("LastSelection", position).apply();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
SharedPreferences settings = getSharedPreferences("app_pref", 0);
SharedPreferences.Editor editor = settings.edit();
themeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//To save
editor.putInt("position",position);
editor.commit();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//To retrieve in next activity
SharedPreferences settings = getSharedPreferences("app_pref", 0);
int snowDensity = settings.getInt("position", -1); //0 is the default value
您可以将值存储到 int 并将其传递给下一个 activity。
,而不是共享偏好 int position=-1;
themeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
position=position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
position=-1;
}
});
go_back_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), home.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
第二个Activity:
int position= getIntent().getIntExtra("position", -1);
很好,我找到了答案
// send data to next activity
go_back_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int Myposition = themeSpinner.getSelectedItemPosition();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("LastSelection", Myposition);
editor.apply();
Intent intent = new Intent(getApplicationContext(), home.class);
startActivity(intent);
下面是要在下一个 activity 上检索的代码:
// Retrieve and implement conditional statement
private void ToggleTheme() {
final SharedPreferences LastSelectedItem = getApplicationContext().getSharedPreferences("PriorSelected", Context.MODE_PRIVATE);
int LastSelection = LastSelectedItem.getInt("LastSelection", 0);
if (LastSelection == 1) {
homebutton.setBackgroundResource(R.drawable.light_theme_buttons);