2

共有設定を使用していますが、コードが機能しません。何が悪いのかわからない。何か間違ったことを実装していますか?

私の主なJavaアクティビティ(関連するコードのビット)は次のとおりです。

public class MainActivity extends AppCompatActivity {
    private String s;
    public static final String subjectKey = "SubjectID";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final SharedPreferences sharedPreferences = getSharedPreferences(subjectKey, Context.MODE_PRIVATE);

    TextView calc_monday = (TextView) findViewById(R.id.monday_calc);


    calc_monday.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View v){



                    CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
                    cdd.show();
                    TextView text1 = (TextView) cdd.findViewById(R.id.Subject_ID);

                    String text = sharedPreferences.getString(subjectKey, " ");


                    if(text != " ")
                    {
                       text1.setText(text); /* Edit the value here*/
                    }

                    TextView text2 = (TextView) cdd.findViewById(R.id.Room_ID);
                    text2.setText("6 (SEECS)");
                    TextView text3 = (TextView) cdd.findViewById(R.id.Time_ID);
                    text3.setText("09:00am  - 09:50am");


                }
            }
    );

    calc_monday.setOnLongClickListener(
            new Button.OnLongClickListener() {
                public boolean onLongClick(View v) {

                    SettingDialogClass SDC = new SettingDialogClass(MainActivity.this);
                    SDC.show();

                    EditText texii = (EditText) SDC.findViewById(R.id.set_Subject_ID);






                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString(subjectKey, texii.getText().toString());
                    editor.apply();


                    return true;


                }
            }
    );

基本的には、テキスト ボックス (calc_monday) をロングクリックすると、ダイアログ ボックスが表示されるようにしたいと考えています。これで、このダイアログ ボックスに表示される EditText フィールドに何かを書き込むことができるはずです。私が書いたものはすべて保存され、同じテキストボックスをシングルクリックすると表示されます(calc_monday)

理解するには、onClick と onLongClick の 2 つのメソッドを参照してください。

コードが機能していません。つまり、テキストボックスをシングルクリックしても、onLongCLick ダイアログボックスの EditText フィールドに書いたテキストが表示されません。

コードの何が問題なのですか

4

4 に答える 4

0
//create and initialize the intance of shared preference
SharedPreferences sharedPreferences = getSharedPreferences("Session", MODE_PRIVATE);


//save a string
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString(subjectKey , texii.getText().toString());
edit.commit();


//retrieve the string
String subject = sharedPreferences.getString(subjectKey, "");
于 2015-12-28T20:08:06.810 に答える
0

設定を使用して文字列を保存およびロードするには、次の方法を試してください。

//save prefs
public void savePrefs(String key, String value){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
}

//get prefs
public String loadPrefs(String key, String value){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    String data = sharedPreferences.getString(key, value);
    return data;
}

次のように load メソッドを呼び出すことができます。

subjectKey = loadPrefs("YouCanNameThisAnything", subjectKey);

そして、次のように save メソッドを呼び出すことができます:

savePrefs("YouCanNameThisAnything", subjectKey);

共有設定の使用方法の詳細については、こちらのドキュメントでもご覧いただけます: http://developer.android.com/reference/android/content/SharedPreferences.html

于 2015-12-28T19:28:26.433 に答える