2017-11-16 11 views
0

ウェブサイトとその言語をsharedpreferencesに読み込んで保存する必要があります。終了してアプリを開くと、ウェブサイトと言語の負荷が保存されます。私はいろいろな方法を使いましたが、達成できません。お互いに互換性がないStringintがあります。このような私の簡単なコード: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(); 
} 

答えて

0

は、なぜあなたはint型の変数(ウェブサイト)に文字列を設定している

おそらく好み(文字列)などの言語を保存する必要があります。アプリを読み込んだら言語の設定を読んでそこからウェブサイトを決めてください。

以下のようになります。

public String getWebsite(){ 

    SharedPreferences prefs; 
    if (prefs == null) { 
     prefs = PreferenceManager 
      .getDefaultSharedPreferences(this); 
    } 

    String language = prefs.getString("Language", "English"); 

    String website = ""; 

    switch(language){ 
     case "Russian": 
      website = "russian.com"; 
      break; 
     default: 
      website = "english.com"; 
    } 

    return website; 
} 

private void saveSelectedItem(String language) { 
    if (prefs == null) { 
     prefs = PreferenceManager 
      .getDefaultSharedPreferences(this); 
    } 
    sharedPrefEditor = prefs.edit(); 
    sharedPrefEditor.putInt("language", language); 
    sharedPrefEditor.commit(); 
} 

あなたが選択したアイテムを保存呼び出すだけのアイテムにsaveSelectedItem(website); で[]の代わり

0

あなたは文字列を渡していると方法が節約のためにint型

private void saveSelectedItem(int which) { 
    if (prefs == null) { 
     prefs = PreferenceManager 
       .getDefaultSharedPreferences(this); 
    } 
    sharedPrefEditor = prefs.edit(); 
    sharedPrefEditor.putInt(SELECTED_ITEM, which); 
    sharedPrefEditor.commit(); 

を必要に合格あなたのデータは共有の環境設定で 私は使用することをお勧めしますGSON 私はクラスを作ることをお勧めします例えば

012ユーザーの選択言語が新しいオブジェクトを作成し、選択した言語で言語を設定
public class WebsiteData 
{ 
    @SerializedName("lang") 
    @Expose 
    private String lang ; 
    @SerializedName("website") 
    @Expose 
    private String website; 

    public String getLang() { 
     return lang; 
    } 

    public void setLang(String lang) { 
     this.lang= lang; 
    } 

    public String getWebsite() { 
     return website; 
    } 

    public void setWebsite(String website) { 
     this.website= website; 
    } 
} 

し、次のようにGsonで文字列にオブジェクトを回しそれに対応する値 でウェブサイトを設定:

Gson gson = new Gson(); 
String dataToSave = gson.toJson(WebsiteData); 

String loadedString = getUserData(context); 
WebsiteData loadedData = gson.fromJson(loadedString, WebsiteData.class); 

を保存し、データをロードするには、次のメソッドを使用します。

public void saveUserData(Context context, String data) { 
    SharedPreferences sharedpreferences; 
    sharedpreferences = context.getSharedPreferences(PREF, Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedpreferences.edit(); 
    editor.putString(DATA_KEY, data); 
    editor.apply(); 
} 

public String getUserData(Context context) { 
    SharedPreferences sharedpreferences = context.getSharedPreferences(PREF, Context.MODE_PRIVATE); 
    return sharedpreferences.getString(DATA_KEY, null); 
} 
+0

回答ありがとうございます、私は試してみます – alizulfuqar

関連する問題