1

そのため、 Displaymetrics のおかげでポップアップのように見える 1 つのアクティビティがあり、そこにはいくつかのチェックボックス ( checkBox1、checkBox2 など) があります。

このアクティビティを終了するときに、チェック済みまたはチェックなしのステータスを保存したいと思います (さらにアクションを展開する: サウンドの再生、文字列の追加など) 。 SharedPreferencesを使用して行いました。

これまでのところ、動作するコードは

        final checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (checkBox1.isChecked()) {
                buttonSound.start();
            } else {
                buttonSound.start();
            }
            SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
            SharedPreferences.Editor editor = pref.edit();
            editor.putBoolean("checkbox", checkBox1.isChecked());
            editor.commit();
            if (pref.getBoolean("checkbox", false) == true){
                checkBox1.setChecked(true);
            } else{
                checkBox1.setChecked(false);
            }
        }
    });

    final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
    checkBox2.setChecked(false);

    checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (checkBox2.isChecked()) {
                buttonSound.start();
            } else {
                buttonSound.start();
            }

            SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
            SharedPreferences.Editor editor = pref.edit();
            editor.putBoolean("checkbox", checkBox2.isChecked());
            editor.commit();
            if (pref.getBoolean("checkbox", false) == true){
                checkBox2.setChecked(true);
            } else{
                checkBox2.setChecked(false);
            }
        }
    }

そしてそれは完璧でした!

次に、このまったく同じコードを実際のプロジェクトにアップロードしました。このコードでは、Fragmentが checkBox がある Popup アクティビティを開始します。

しかし

ポップアップ領域の外側をクリックすると (はい、前のプロジェクトでもアクティビティのスタイルを処理することでそれを行いましたが、うまくいきました)、ポップアップ アクティビティに再度入ると、ステータスが保存されていないことがわかります!

これらのプロジェクトは両方とも同じスタイル、チェックボックスの ID、要素などを持っています。これは純粋なコピー アンド ペーストでした。その理由は、Popup アクティビティの前にあるアクティビティがFragmentであるためだと思います。

要するに、フラグメント-->クリック--->ポップアップアクティビティを開く--->チェックボックスにフラグを付ける-->外側をクリックするか、物理デバイスを押し戻す-->ポップアップアクティビティと持っていたチェックボックスを再度開くチェックを入れたままにすると、もうチェックされません。

なんでそうなの?Fragment は何を変更し、コードをどのように適応させる必要がありますか?

編集:

それが POPUP アクティビティの元のコードです

public class Popup1 extends Activity {

private TextView textViewResult;
private TextView textView2;
private TextView textView3;
private TextView textView4;
MediaPlayer buttonSound;
public String newString;
public String newString2;
public String newString3;
public String newString4;
public String risultato;
final ArrayList<String> builder = new ArrayList<String>();


private ProgressDialog loading;

@Override
protected void onCreate(Bundle savedInstanceState) {

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

    textViewResult = (TextView) findViewById(R.id.textViewResult);
    textView2 = (TextView) findViewById(R.id.textView2);
    textView3 = (TextView) findViewById(R.id.textView3);
    textView4 = (TextView) findViewById(R.id.textView4);

    buttonSound = MediaPlayer.create(Popup1.this, R.raw.scribble);

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;
    getWindow().setLayout((int) (width * 1.), (int) (height * .4));



    final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox);
    checkBox1.setChecked(false);

    checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (checkBox1.isChecked()) {
                builder.add((String.valueOf(newString)));
                builder.add("\n");
                LayoutInflater inflater = getLayoutInflater();
                View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
                ((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Yippie, a custom toast");
                Toast mytoast = new Toast(getBaseContext());
                mytoast.setView(layouttoast);
                mytoast.setDuration(Toast.LENGTH_LONG);
                mytoast.show();
                buttonSound.start();
            } else {
                builder.remove(String.valueOf(newString));
                builder.remove("\n");

                LayoutInflater inflater = getLayoutInflater();
                View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
                ((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item deselected");
                Toast mytoast = new Toast(getBaseContext());
                mytoast.setView(layouttoast);
                mytoast.setDuration(Toast.LENGTH_LONG);
                mytoast.show();
                buttonSound.start();
            }
            SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
            SharedPreferences.Editor editor1 = pref.edit();
            editor1.putBoolean("checkbox", checkBox1.isChecked());
            editor1.commit();
            if (pref.getBoolean("checkbox", false) == true) {
                checkBox1.setChecked(true);
            } else {
                checkBox1.setChecked(false);
            }


        }
    });



    final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
    checkBox2.setChecked(false);

    checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (checkBox2.isChecked()) {
                builder.add((String.valueOf(newString2)));
                builder.add("\n");
                LayoutInflater inflater = getLayoutInflater();
                View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
                ((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Yippie, a custom toast");
                Toast mytoast = new Toast(getBaseContext());
                mytoast.setView(layouttoast);
                mytoast.setDuration(Toast.LENGTH_LONG);
                mytoast.show();
                buttonSound.start();
            } else {
                builder.remove(String.valueOf(newString2));
                builder.remove("\n");

                LayoutInflater inflater = getLayoutInflater();
                View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
                ((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item deselected");
                Toast mytoast = new Toast(getBaseContext());
                mytoast.setView(layouttoast);
                mytoast.setDuration(Toast.LENGTH_LONG);
                mytoast.show();
                buttonSound.start();
            }
            SharedPreferences pref2 = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
            SharedPreferences.Editor editor2 = pref2.edit();
            editor2.putBoolean("checkbox", checkBox2.isChecked());
            editor2.commit();
            if (pref2.getBoolean("checkbox", false) == true){
                checkBox2.setChecked(true);
            } else{
                checkBox2.setChecked(false);
            }
        }
    });



    final CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkBox2);
    checkBox3.setChecked(false);

    checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (checkBox3.isChecked()) {
                builder.add((String.valueOf(newString3)));
                builder.add("\n");
                LayoutInflater inflater = getLayoutInflater();
                View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
                ((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item selected");
                Toast mytoast = new Toast(getBaseContext());
                mytoast.setView(layouttoast);
                mytoast.setDuration(Toast.LENGTH_LONG);
                mytoast.show();
                buttonSound.start();
            } else {
                builder.remove(String.valueOf(newString3));
                builder.remove("\n");

                LayoutInflater inflater = getLayoutInflater();
                View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
                ((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item deselected");
                Toast mytoast = new Toast(getBaseContext());
                mytoast.setView(layouttoast);
                mytoast.setDuration(Toast.LENGTH_LONG);
                mytoast.show();
                buttonSound.start();
            }
            SharedPreferences pref3 = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
            SharedPreferences.Editor editor3 = pref3.edit();
            editor3.putBoolean("checkbox", checkBox3.isChecked());
            editor3.commit();
            if (pref3.getBoolean("checkbox", false) == true){
                checkBox3.setChecked(true);
            } else{
                checkBox3.setChecked(false);
            }
        }
    });


    final CheckBox checkBox4 = (CheckBox) findViewById(R.id.checkBox2);
    checkBox4.setChecked(false);

    checkBox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (checkBox4.isChecked()) {
                builder.add((String.valueOf(newString4)));
                builder.add("\n");
                LayoutInflater inflater = getLayoutInflater();
                View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
                ((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item selected");
                Toast mytoast = new Toast(getBaseContext());
                mytoast.setView(layouttoast);
                mytoast.setDuration(Toast.LENGTH_LONG);
                mytoast.show();
                buttonSound.start();
            } else {
                builder.remove(String.valueOf(newString4));
                builder.remove("\n");

                LayoutInflater inflater = getLayoutInflater();
                View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
                ((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item deselected");
                Toast mytoast = new Toast(getBaseContext());
                mytoast.setView(layouttoast);
                mytoast.setDuration(Toast.LENGTH_LONG);
                mytoast.show();
                buttonSound.start();
            }
            SharedPreferences pref4 = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
            SharedPreferences.Editor editor4 = pref4.edit();
            editor4.putBoolean("checkbox", checkBox4.isChecked());
            editor4.commit();
            if (pref4.getBoolean("checkbox", false) == true){
                checkBox4.setChecked(true);
            } else{
                checkBox4.setChecked(false);
            }
        }
    });



    final CheckBox checkBox5 = (CheckBox) findViewById(R.id.checkBox2);
    checkBox5.setChecked(false);

    checkBox5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (checkBox5.isChecked()) {
                LayoutInflater inflater = getLayoutInflater();
                View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
                ((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item selected");
                Toast mytoast = new Toast(getBaseContext());
                mytoast.setView(layouttoast);
                mytoast.setDuration(Toast.LENGTH_LONG);
                mytoast.show();
                buttonSound.start();
            } else {
                LayoutInflater inflater = getLayoutInflater();
                View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
                ((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item deselected");
                Toast mytoast = new Toast(getBaseContext());
                mytoast.setView(layouttoast);
                mytoast.setDuration(Toast.LENGTH_LONG);
                mytoast.show();
                buttonSound.start();
            }
            SharedPreferences pref5 = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
            SharedPreferences.Editor editor5 = pref5.edit();
            editor5.putBoolean("checkbox", checkBox5.isChecked());
            editor5.commit();
            if (pref5.getBoolean("checkbox", false) == true){
                checkBox5.setChecked(true);
            } else{
                checkBox5.setChecked(false);
            }
        }
    });



    final CheckBox checkBox6 = (CheckBox) findViewById(R.id.checkBox2);
    checkBox6.setChecked(false);

    checkBox6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (checkBox6.isChecked()) {
                LayoutInflater inflater = getLayoutInflater();
                View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
                ((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item selected");
                Toast mytoast = new Toast(getBaseContext());
                mytoast.setView(layouttoast);
                mytoast.setDuration(Toast.LENGTH_LONG);
                mytoast.show();
                buttonSound.start();
            } else {

                LayoutInflater inflater = getLayoutInflater();
                View layouttoast = inflater.inflate(R.layout.toastattempt, (ViewGroup) findViewById(R.id.toastcustom));
                ((TextView) layouttoast.findViewById(R.id.texttoast)).setText("Item deselected");
                Toast mytoast = new Toast(getBaseContext());
                mytoast.setView(layouttoast);
                mytoast.setDuration(Toast.LENGTH_LONG);
                mytoast.show();
                buttonSound.start();
            }

            SharedPreferences pref6 = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
            SharedPreferences.Editor editor6 = pref6.edit();
            editor6.putBoolean("checkbox", checkBox6.isChecked());
            editor6.commit();
            if (pref6.getBoolean("checkbox", false) == true){
                checkBox6.setChecked(true);
            } else{
                checkBox6.setChecked(false);
            }


        }
    });


    getData();





}




private void getData() {

    loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);

    StringRequest stringRequest = new StringRequest(Config.DATA_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            loading.dismiss();


            showJSON(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(Popup1.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

    StringRequest stringRequest2 = new StringRequest(Config.DATA_URL2, new Response.Listener<String>() {
        @Override
        public void onResponse(String response2) {
            loading.dismiss();


            showJSON2(response2);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(Popup1.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue2 = Volley.newRequestQueue(this);
    requestQueue2.add(stringRequest2);

    StringRequest stringRequest3 = new StringRequest(Config.DATA_URL3, new Response.Listener<String>() {
        @Override
        public void onResponse(String response3) {
            loading.dismiss();


            showJSON3(response3);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(Popup1.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue3 = Volley.newRequestQueue(this);
    requestQueue3.add(stringRequest3);

    StringRequest stringRequest4 = new StringRequest(Config.DATA_URL4, new Response.Listener<String>() {
        @Override
        public void onResponse(String response4) {
            loading.dismiss();


            showJSON4(response4);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(Popup1.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue4 = Volley.newRequestQueue(this);
    requestQueue4.add(stringRequest4);

}

private void showJSON(String response){
    String ID="";
    String Price="";
    String WeightUnits = "";
    String PWU = "";
    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        JSONObject collegeData = result.getJSONObject(0);
        ID = collegeData.getString(Config.KEY_ID);
        Price = collegeData.getString(Config.KEY_Price);
        WeightUnits = collegeData.getString(Config.KEY_WeightUnits);
        PWU = collegeData.getString(Config.KEY_PWU);


    } catch (JSONException e) {
        e.printStackTrace();
    }
    textViewResult.setText("ID:\t" + ID + "\nPrice:\t" + Price + "\nWeight/Units:\t" + WeightUnits + "\nPrice per Weight/Units:\t" + PWU);

    String newString= (String)("ID:\t"+ID+"\nPrice:\t" +Price+ "\nWeight/Units:\t"+ WeightUnits+"\nPrice per Weight/Units:\t"+PWU).toString();
}

private void showJSON2(String response2){
    String ID="";
    String Price="";
    String WeightUnits = "";
    String PWU = "";
    try {
        JSONObject jsonObject = new JSONObject(response2);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY2);
        JSONObject collegeData = result.getJSONObject(0);
        ID = collegeData.getString(Config.KEY_ID);
        Price = collegeData.getString(Config.KEY_Price);
        WeightUnits = collegeData.getString(Config.KEY_WeightUnits);
        PWU = collegeData.getString(Config.KEY_PWU);


    } catch (JSONException e) {
        e.printStackTrace();
    }
    textView2.setText("ID:\t"+ID+"\nPrice:\t" +Price+ "\nWeight/Units:  "+ WeightUnits+"\nPrice per Weight/Units:\t"+PWU);

    String newString2= (String)("ID:\t"+ID+"\nPrice:\t" +Price+ "\nWeight/Units:\t"+ WeightUnits+"\nPrice per Weight/Units:\t"+PWU).toString();

}

private void showJSON3(String response3){
    String ID="";
    String Price="";
    String WeightUnits = "";
    String PWU = "";
    try {
        JSONObject jsonObject = new JSONObject(response3);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY3);
        JSONObject collegeData = result.getJSONObject(0);
        ID = collegeData.getString(Config.KEY_ID);
        Price = collegeData.getString(Config.KEY_Price);
        WeightUnits = collegeData.getString(Config.KEY_WeightUnits);
        PWU = collegeData.getString(Config.KEY_PWU);


    } catch (JSONException e) {
        e.printStackTrace();
    }
    textView3.setText("ID:\t"+ID+"\nPrice:\t" +Price+ "\nWeight/Units:  "+ WeightUnits+"\nPrice per Weight/Units:\t"+PWU);

    String newString3= (String)("ID:\t"+ID+"\nPrice:\t" +Price+ "\nWeight/Units:\t"+ WeightUnits+"\nPrice per Weight/Units:\t"+PWU).toString();

}

private void showJSON4(String response4){
    String ID="";
    String Price="";
    String WeightUnits = "";
    String PWU = "";
    try {
        JSONObject jsonObject = new JSONObject(response4);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY4);
        JSONObject collegeData = result.getJSONObject(0);
        ID = collegeData.getString(Config.KEY_ID);
        Price = collegeData.getString(Config.KEY_Price);
        WeightUnits = collegeData.getString(Config.KEY_WeightUnits);
        PWU = collegeData.getString(Config.KEY_PWU);


    } catch (JSONException e) {
        e.printStackTrace();
    }
    textView4.setText("ID:\t"+ID+"\nPrice:\t" +Price+ "\nWeight/Units:  "+ WeightUnits+"\nPrice per Weight/Units:\t"+PWU);

    String newString4= (String)("ID:\t"+ID+"\nPrice:\t" +Price+ "\nWeight/Units:\t"+ WeightUnits+"\nPrice per Weight/Units:\t"+PWU).toString();

}

@Override
public void finish() {
    String risultato = builder.toString().replace("[", "").replace("]", "");
    // Prepare data intent
    Intent returnIntent = new Intent();
    returnIntent.putExtra("result", "papparapa");
    setResult(RESULT_OK, returnIntent );
    super.finish();
}

}
4

0 に答える 0