SharedPreference未更新

时间:2017-06-07 15:25:05

标签: java android

SharedPreference未更新(更改)。请帮忙。我是首发,所以我无法理解。请解释,而不仅仅是代码。这是MainActivity.java,我需要首选项和colorPicker.java,我选择颜色。

MainActivity.java:

package com.example.alexsoft.tictactoe;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    boolean turn = true;
    int color = 0;
    Button A1, B1, C1, A2, B2, C2, A3, B3, C3;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        A1 = (Button) findViewById(R.id.A1);
        B1 = (Button) findViewById(R.id.B1);
        C1 = (Button) findViewById(R.id.C1);
        A2 = (Button) findViewById(R.id.A2);
        B2 = (Button) findViewById(R.id.B2);
        C2 = (Button) findViewById(R.id.C2);
        A3 = (Button) findViewById(R.id.A3);
        B3 = (Button) findViewById(R.id.B3);
        C3 = (Button) findViewById(R.id.C3);
        SharedPreferences prefs = this.getSharedPreferences(
                "com.example.alexsoft.tictactoe", Context.MODE_PRIVATE);
        color = prefs.getInt("com.example.alexsoft.tictactoe", R.color.colorAccent);
        changeAspect(color);
    }

    @Override
    public void onResume() {
        super.onResume();
        SharedPreferences prefs = this.getSharedPreferences(
                "com.example.alexsoft.tictactoe", Context.MODE_PRIVATE);
        color = prefs.getInt("com.example.alexsoft.tictactoe", R.color.colorAccent);
        changeAspect(color);
    }

    @Override
    public void onStop() {
        super.onStop();

        SharedPreferences prefs = this.getSharedPreferences(
                "com.example.alexsoft.tictactoe", Context.MODE_PRIVATE);
        prefs.edit().putInt("com.example.alexsoft.tictactoe", color).apply();
    }

    @Override
    public void onBackPressed() {
        // super.onBackPressed();

        //Creating an alert dialog to logout
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("Do you want to exit?");
        alertDialogBuilder.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_HOME);
                        startActivity(intent);
                    }
                });

        alertDialogBuilder.setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {

                    }
                });

        //Showing the alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        menu.getItem(1).setIcon(R.mipmap.ic_cached_white_24dp);
        switch(Color.rgb(Color.red(color), Color.green(color), Color.blue(color))) {
            case R.color.colorPrimary:
                menu.getItem(1).setIcon(R.mipmap.ic_cached_black_24dp);
            case R.color.yellow:
                menu.getItem(1).setIcon(R.mipmap.ic_cached_black_24dp);
            case R.color.lightBlue:
                menu.getItem(1).setIcon(R.mipmap.ic_cached_black_24dp);
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.resGame:
                restartBtn();
                return true;
            case R.id.setAsp:
                Intent it = new Intent(getBaseContext(), colorPicker.class);
                startActivity(it);
                return true;
            case R.id.aboutBtn:
                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                alertDialog.setTitle("About");
                alertDialog.setMessage("Tic Tac Toe, verison 1.0, created by Alex Sandulescu");
                alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                alertDialog.show();
                return true;
            case R.id.resStats:
                TextView xCount = (TextView) findViewById(R.id.xCount);
                TextView oCount = (TextView) findViewById(R.id.oCount);
                TextView roundsPlayed = (TextView) findViewById(R.id.roundsPlayed);
                xCount.setText("0");
                oCount.setText("0");
                roundsPlayed.setText("0");
            case R.id.cMode:
                if (item.isChecked()) {
                    //item.setChecked(false);
                    AlertDialog nYet = new AlertDialog.Builder(MainActivity.this).create();
                    nYet.setMessage("Not ready yet!");
                    nYet.setTitle("Message");
                    nYet.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    nYet.show();
                } else {
                    //item.setChecked(true);
                    AlertDialog nYet = new AlertDialog.Builder(MainActivity.this).create();
                    nYet.setMessage("Not ready yet!");
                    nYet.setTitle("Message");
                    nYet.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    nYet.show();
                }
        }
        return false;
    }

    public void btnClick(View v) {
        Button b = (Button) v;
        if (b.isEnabled()) {
            if (turn == true)
                b.setText("X");
            else
                b.setText("0");
            b.setEnabled(false);
            turn = !turn;
            checkForWinner();
        }
    }

    private void checkForWinner() {
        String winner = "";
        boolean ok = false;
        if (A1.getText().equals(B1.getText()) && A1.getText().equals(C1.getText()) && !A1.getText().equals("")) {
            ok = true;
            winner = A1.getText().toString();
            A1.setTextColor(Color.RED);
            B1.setTextColor(Color.RED);
            C1.setTextColor(Color.RED);
        } else if (A2.getText().equals(B2.getText()) && A2.getText().equals(C2.getText()) && !A2.getText().equals("")) {
            ok = true;
            winner = A2.getText().toString();
            A2.setTextColor(Color.RED);
            B2.setTextColor(Color.RED);
            C2.setTextColor(Color.RED);
        } else if (A3.getText().equals(B3.getText()) && A3.getText().equals(C3.getText()) && !A3.getText().equals("")) {
            ok = true;
            winner = A3.getText().toString();
            A3.setTextColor(Color.RED);
            B3.setTextColor(Color.RED);
            C3.setTextColor(Color.RED);
        } else if (A1.getText().equals(A2.getText()) && A1.getText().equals(A3.getText()) && !A1.getText().equals("")) {
            ok = true;
            winner = A1.getText().toString();
            A1.setTextColor(Color.RED);
            A2.setTextColor(Color.RED);
            A3.setTextColor(Color.RED);
        } else if (B1.getText().equals(B2.getText()) && B1.getText().equals(B3.getText()) && !B1.getText().equals("")) {
            ok = true;
            winner = B1.getText().toString();
            B1.setTextColor(Color.RED);
            B2.setTextColor(Color.RED);
            B3.setTextColor(Color.RED);
        } else if (C1.getText().equals(C2.getText()) && C1.getText().equals(C3.getText()) && !C1.getText().equals("")) {
            ok = true;
            winner = C1.getText().toString();
            C1.setTextColor(Color.RED);
            C2.setTextColor(Color.RED);
            C3.setTextColor(Color.RED);
        } else if (A1.getText().equals(B2.getText()) && A1.getText().equals(C3.getText()) && !A1.getText().equals("")) {
            ok = true;
            winner = A1.getText().toString();
            A1.setTextColor(Color.RED);
            B2.setTextColor(Color.RED);
            C3.setTextColor(Color.RED);

        } else if (A3.getText().equals(B2.getText()) && A3.getText().equals(C1.getText()) && !A3.getText().equals("")) {
            ok = true;
            winner = A3.getText().toString();
            A3.setTextColor(Color.RED);
            B2.setTextColor(Color.RED);
            C1.setTextColor(Color.RED);
        }
        if (ok) {
            Toast.makeText(this, winner + " won!", Toast.LENGTH_LONG).show();
            if (winner.equals("X")) {
                TextView tcount = (TextView) findViewById(R.id.xCount);
                tcount.setText(Integer.toString(Integer.parseInt(tcount.getText().toString()) + 1));
            } else {
                TextView tcount = (TextView) findViewById(R.id.oCount);
                tcount.setText(Integer.toString(Integer.parseInt(tcount.getText().toString()) + 1));
            }
            A1.setEnabled(false);
            B1.setEnabled(false);
            C1.setEnabled(false);
            A2.setEnabled(false);
            B2.setEnabled(false);
            C2.setEnabled(false);
            A3.setEnabled(false);
            B3.setEnabled(false);
            C3.setEnabled(false);
        }
    }

    public void restartBtn() {
        remakeGame();
        TextView rplayed = (TextView) findViewById(R.id.roundsPlayed);
        rplayed.setText(Integer.toString(Integer.parseInt(rplayed.getText().toString()) + 1));
    }

    private void remakeGame() {
        A1.setText("");
        B1.setText("");
        C1.setText("");
        A2.setText("");
        B2.setText("");
        C2.setText("");
        A3.setText("");
        B3.setText("");
        C3.setText("");
        //
        A1.setTextColor(Color.BLACK);
        B1.setTextColor(Color.BLACK);
        C1.setTextColor(Color.BLACK);
        A2.setTextColor(Color.BLACK);
        B2.setTextColor(Color.BLACK);
        C2.setTextColor(Color.BLACK);
        A3.setTextColor(Color.BLACK);
        B3.setTextColor(Color.BLACK);
        C3.setTextColor(Color.BLACK);
        //
        A1.setEnabled(true);
        B1.setEnabled(true);
        C1.setEnabled(true);
        A2.setEnabled(true);
        B2.setEnabled(true);
        C2.setEnabled(true);
        A3.setEnabled(true);
        B3.setEnabled(true);
        C3.setEnabled(true);
        //
        turn = true;
    }

    public void changeAspect(int color) {
        if (color != 0) {
            A1.setBackgroundColor(color);
            B1.setBackgroundColor(color);
            C1.setBackgroundColor(color);
            A2.setBackgroundColor(color);
            B2.setBackgroundColor(color);
            C2.setBackgroundColor(color);
            A3.setBackgroundColor(color);
            B3.setBackgroundColor(color);
            C3.setBackgroundColor(color);
            getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
            getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                float[] hsv = new float[3];
                Color.colorToHSV(color, hsv);
                hsv[2] *= 0.8f;
                color = Color.HSVToColor(hsv);
                window.setStatusBarColor(color);
            }
        }
    }
}

colorPicker.java:

package com.example.alexsoft.tictactoe;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class colorPicker extends Activity {

    public int color = 0;
    String xCount, oCount, roundsPlayed;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_picker);
    }

    public void onColorConfirm(View v) {
        ImageView img = (ImageView) v;
        ColorDrawable s = (ColorDrawable) img.getDrawable();
        SharedPreferences prefs = this.getSharedPreferences(
                "com.example.alexsoft.tictactoe", Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = prefs.edit();
        ed.putInt("com.example.alexsoft.tictactoe", color).apply();
        finish();
    }

    @Override
    public void onStop() {
        super.onStop();
        SharedPreferences prefs = this.getSharedPreferences(
                "com.example.alexsoft.tictactoe", Context.MODE_PRIVATE);
        prefs.edit().putInt("com.example.alexsoft.tictactoe", color).apply();
    }
}

1 个答案:

答案 0 :(得分:0)

问题是您没有在color活动中的任何位置更新colorPicker的值。 color的值始终为0

<强> SOLUTION:

在将color存储到SharedPreferences之前更新public class colorPicker extends Activity { public int color = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_color_picker); } public void onColorConfirm(View v) { ImageView img = (ImageView) v; ColorDrawable s = (ColorDrawable) img.getDrawable(); SharedPreferences prefs = this.getSharedPreferences( "com.example.alexsoft.tictactoe", Context.MODE_PRIVATE); SharedPreferences.Editor ed = prefs.edit(); // Update color value color = SOME_VALUE; // Store ed.putInt("com.example.alexsoft.tictactoe", color).apply(); finish(); } }

更新您的代码,如下所示:

Giochi::select('giocos.*')
    ->with('Review','InfoGiochi')
    ->leftJoin('reviews', 'reviews.id', '=', 'giocos.id_gioco')
    ->orderBy('reviews.id','DESC')
    ->get();

希望这会有所帮助〜