ウェブサイトとその言語をsharedpreferencesに読み込んで保存する必要があります。終了してアプリを開くと、ウェブサイトと言語の負荷が保存されます。私はいろいろな方法を使いましたが、達成できません。お互いに互換性がないString
とint
があります。このような私の簡単なコード:AlertDialogでウェブサイトと言語を選択して保存する
private SharedPreferences prefs;
private static final String SELECTED_ITEM = "SelectedItem";
private SharedPreferences.Editor sharedPrefEditor;
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
else if (id == R.id.web)
final CharSequence[] items={"English","Arabic","Russian"};
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle("Choose Website");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
});
builder.setSingleChoiceItems(items, getSelectedItem(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
webView = (WebView) findViewById(R.id.webView);
int website=current_page;
if("English".equals(items[which]))
{
webView.loadUrl("https://english.com");
website=("https://english.com");
}
else if("Arabic".equals(items[which]))
{
webView.loadUrl("https://arabic.com");
website=("https://arabic.com");
}
else if("Russian".equals(items[which]))
{
webView.loadUrl("https://russian.com");
website=("https://russian.com");
}
saveSelectedItem(website);
}
});
builder.show();
}
private int getSelectedItem() {
if (prefs == null) {
prefs = PreferenceManager
.getDefaultSharedPreferences(this);
}
return prefs.getInt(SELECTED_ITEM, -1);
}
private void saveSelectedItem(int which) {
if (prefs == null) {
prefs = PreferenceManager
.getDefaultSharedPreferences(this);
}
sharedPrefEditor = prefs.edit();
sharedPrefEditor.putInt(SELECTED_ITEM, which);
sharedPrefEditor.commit();
}
回答ありがとうございます、私は試してみます – alizulfuqar