2017-02-23 1 views
0

자바 코딩에 익숙하지 않지만 자체 앱을 만들고 싶습니다.여러 클래스의 SharedPreferences

SharedPreferences 여러 학습서 및이 사이트의 질문에 표시된 것처럼 여러 활동/클래스에 일부 값을 저장하려고하지만 다른 클래스의 다른 활동에서 읽으려고하면 임의로 생성됩니다.

사용자는 direction_1000textview 필드를 작성해야하므로 GetWind 클래스를 통해 풍향을 1000 피트로 얻습니다.

나는 모든 잘 작동 SharedPreferenceint이 값을 저장하지만 클래스 InputNavData이 값을 전달하고 intPHIStringphi를 통해 저기를 표시하려면 나는이 이상한 값을 얻을합니다.

이 문제를 해결할 수있는 사람이 있습니까? 지저분한 코드와 질문을 미리 드려 죄송합니다. 당신의 01에서

MainActivity

package com.example.navigationcalculator; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 

public class MainActivity extends Activity { 
public final static String EXTRA_MESSAGE = "com.example.navigationcalculator.MESSAGE"; 

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

/** Called when the user clicks the Agree button */ 
public void wind_input(View view) { 
    Intent intent = new Intent(this, GetWind.class); 
    startActivity(intent); 
} 

/** Called when the user clicks the Input Navigation Data button */ 
public void input_nav_data(View view) { 
    Intent intent = new Intent(this, InputNavData.class); 
    startActivity(intent); 
} 

/** Called when the user clicks the Calculate Data button */ 
public void calculate_data(View view) { 
    Intent intent = new Intent(this, CalculateData.class); 
    startActivity(intent); 
} 

} 

GetWind

package com.example.navigationcalculator; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.EditText; 

import static android.content.ContentValues.TAG; 
import static com.example.navigationcalculator.R.id.direction_1000; 

public class GetWind extends Activity { 

public static final String PREFS_NAME = "com.example.myfirstapp.sharedPref"; 

public final static String DIR_1000 = "com.example.myfirstapp.DIR_1000"; 
public final static String SPEED_1000 = "com.example.myfirstapp.SPEED_1000"; 



SharedPreferences sharedpreferences; 

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

} 

//Read all wind speeds and directions and put them in a shared preferences file 
public void input_nav_data(View view) { 
    Intent intent = new Intent(this, InputNavData.class); 

    sharedpreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 

    startActivity(intent); 

    // 1000 feet 
    EditText dir_1000 = (EditText)findViewById(R.id.direction_1000); 
    int direction_1000 = Integer.parseInt(dir_1000.getText().toString()); 

    SharedPreferences.Editor editor = sharedpreferences.edit(); 

    editor.putInt(DIR_1000,direction_1000); 
    editor.commit(); 

    int shared_dir_1000 = sharedpreferences.getInt(PREFS_NAME,direction_1000); 
    String SD1000 = String.valueOf(shared_dir_1000); 
    Log.d("direction 1000",SD1000); 

} 
} 

InputNavData

package com.example.navigationcalculator; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.EditText; 

import static com.example.navigationcalculator.GetWind.PREFS_NAME; 
import static com.example.navigationcalculator.R.id.direction_1000; 



public class InputNavData extends Activity { 

public final static String altitude_static_string = "com.example.myfirstapp.altitude"; 

SharedPreferences sharedpreferences; 

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

} 

//Read all navigation data and pass them through as a string to activity CalculateData 
public void calculate_data(View view) { 
    Intent intent = new Intent(this, CalculateData.class); 

    sharedpreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 

    startActivity(intent); 

    // Read the actual altitude 
    EditText alt = (EditText) findViewById(R.id.altitude); 
    int altitude = Integer.parseInt(alt.getText().toString()); 

    SharedPreferences.Editor editor = sharedpreferences.edit(); 

    editor.putInt(altitude_static_string,altitude); 
    editor.commit(); 

    int shared_altitude = sharedpreferences.getInt(PREFS_NAME,altitude); 
    String SALT = String.valueOf(shared_altitude); 
    Log.d("altitude",SALT); 

    int PHI = sharedpreferences.getInt(PREFS_NAME,direction_1000); 
    String phi = String.valueOf(PHI); 
    Log.d("dir 1000 in inputnavdata",phi); 
} 

} 
+0

당신이 있습니까 통과하려고 애쓰다 일부 '활동'데이터를 다른 활동으로? –

+0

네, 실제로 InputNavData에서 공유 참조를 통해 Activity CalculateData로 데이터 (direction_1000)를 전달하려고합니다. – Phil

+0

'SharedPreferences'를 사용하여 활동에서 다른 것으로 데이터를 전달하지 말고, 당신이 어떻게 도와 줄 수 있는지 모른다면'intent'를 사용하여 데이터를 보내십시오. –

답변

0

봐및 calculate_data 방법.

그것은 왜냐하면 흐름이 있어야 할 것 :

  1. 하는 환경을 공유 선호 내부
  2. 스토어 값을 공유하기.
  3. 은 당신이하고있는 것은
    1. 이 선호에게 공유 선호 내부
    2. 시작 활동
    3. 스토어 값을 공유하기입니다
    4. 시작 활동

    을 커밋합니다.

    int x = 0; 
    intent.putExtra("xx", x); 
    

    을 그리고 그때까지 다른 클래스에서 그것을 얻을 :

  4. 당신이 사용할 수

calculate_data

public void calculate_data(View view) { 
Intent intent = new Intent(this, CalculateData.class); 
sharedpreferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); // Read the actual altitude 
EditText alt = (EditText) findViewById(R.id.altitude); 
int altitude = Integer.parseInt(alt.getText().toString()); 
SharedPreferences.Editor editor = sharedpreferences.edit(); 
editor.putInt(altitude_static_string, altitude); 
editor.commit(); 
int shared_altitude = sharedpreferences.getInt(PREFS_NAME, altitude); 
String SALT = String.valueOf(shared_altitude); 
Log.d("altitude", SALT); 
int PHI = sharedpreferences.getInt(PREFS_NAME, direction_1000); 
String phi = String.valueOf(PHI); 
Log.d("dir 1000 in inputnavdata", phi); 
} 

startActivity(intent); 
+0

끝 부분에 startActivity (intent)를 넣었지만 그 결과는 불행히도 동일하게 남아 있습니다 ... 'import static com.example.navigationcalculator.R.id.direction_1000; '나는 잘 모르지만 ... – Phil

0

그냥 데이터를 전달하기 위해 커밋

Intent intent = getIntent(); 
xy= intent.getIntExtra("xx",0);