2017-09-18 25 views
0

いくつかの助けがありがたいです。私には2つの活動があります。 MainActivityとSharedPrefsです。いくつかの保存された設定があるかどうかを調べるために、アプリを開いておきたい。そうでない場合は、詳細を求める別のアクティビティに移動する必要があります。送信ボタンを押すと、共有設定を保存してMainActivityに移動する必要があります。しかし、それはSharedPrefsアクティビティに戻るだけではありません。今、私の共有設定が保存されていないかどうかはわかりません(私は "editor.commit"が仕事をするだろうと思っています)、または私のループに何か問題があります。私は周りのループを変えましたが、それは別の方法で動作していますが、私はAndroidとJavaに全く新しいので、すべてを間違えることがあります。私が言ったように、いくつかの助けに感謝します。ここで共有設定が保存されていないか、ループが機能していません

は私のループと私のMainActivityです:

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     SharedPreferences sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 
     boolean check = sharedPreferences.getBoolean("Check",false); 
     if(!check){ 
      //Intent intent; 
      Intent SharedPrefsIntent = new Intent(MainActivity.this, SharedPrefs.class); 
      startActivity(SharedPrefsIntent); } 
     else { 
      setContentView(R.layout.activity_main); 
      TextView brands = (TextView) findViewById(R.id.brands); 
      brands.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent brandsIntent = new Intent(MainActivity.this, brands.class); 
       startActivity(brandsIntent); 
      } 
     }); 
    } 
} 

そして、ここでは、私はいくつかの情報が共有設定に追加する取得しようと私のSharedPrefsです:

public class SharedPrefs extends AppCompatActivity { 
//public static Context context; 
EditText ed1,ed2,ed3; 
Button b1; 

public static final String MyPREFERENCES = "MyPrefs" ; 
public static final String Name = "nameKey"; 
public static final String Phone = "phoneKey"; 
public static final String Email = "emailKey"; 

SharedPreferences sharedpreferences; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.shared_prefs); 

    ed1=(EditText)findViewById(R.id.editText); 
    ed2=(EditText)findViewById(R.id.editText2); 
    ed3=(EditText)findViewById(R.id.editText3); 

    b1=(Button)findViewById(R.id.button); 
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 

    b1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String n = ed1.getText().toString(); 
      String ph = ed2.getText().toString(); 
      String e = ed3.getText().toString(); 

      SharedPreferences.Editor editor = sharedpreferences.edit(); 

      editor.putString(Name, n); 
      editor.putString(Phone, ph); 
      editor.putString(Email, e); 
      editor.commit(); 

      Toast.makeText(SharedPrefs.this,"Thanks",Toast.LENGTH_LONG).show(); 
      Intent BackToMainIntent = new Intent(SharedPrefs.this, MainActivity.class); 
      startActivity(BackToMainIntent); 
     } 
    }); 
} 
+0

参照するループはどこですか? – codeMagic

+0

あなたが手伝っているループはありますか?そしてあなたは主活動をチェックしている共有優先度にチェック値を保存していません。 –

答えて

1

MainActivityで常にfalseを返すので、希望の値を設定した後にSharedPreferencesに「Check」キー値を追加してください。

.commit()の代わりに.apply()を使用することもできます。あなたのSharedPrefs活動に

+0

初めての方です。笑 –

0

それは見ていませんブール値Checkを2番目のアクティビティのどこにでも保存しているようです。したがって、最初のアクティビティでは、このブール値は存在せず、falseに設定したデフォルト値を取得しています。 2回目のアクティビティでtrueに設定するには、 `Check 'という設定が必要です。

0

は、あなたが望むようそして、それは動作します

 editor.putString(Name, n); 
     editor.putString(Phone, ph); 
     editor.putString(Email, e); 
     editor.putBooleon (Check,true); 
     editor.commit(); 

でコード

 editor.putString(Name, n); 
     editor.putString(Phone, ph); 
     editor.putString(Email, e); 
     editor.commit(); 

のあなたのこの作品を変更し 。

ハッピーコーディング:)

関連する問題