如何在android本地数据库上存储意图或屏幕进度?

时间:2017-03-11 13:06:38

标签: java android json android-intent gson

我是android开发的初学者。我目前正为我的学校项目创建像 4-Pics-1-Word 这样的游戏。它有25个问题所以我创建了25个活动。我知道这不是正确的做法。其他人建议我只需更改ImageView中的图片。我试着学习如何做到,但我真的不明白,所以我做了这个方法。现在我被卡住了。这就是我的游戏的工作方式。它就像4个图片1个字,但我的教授告诉我,活动必须是随机的。我已经随机化了。唯一的问题是我无法在Android本地数据库上存储游戏的进度。我使用intent.putExtra将当前活动的信息发送到另一个活动。因此,基本上在回答上一个活动之后,必须从列表中删除先前的活动。我已经完成了这件事。我无法找到将其存储在本地数据库中的方法,因此当用户关闭游戏并再次打开游戏时,会保存进度。我在下面尝试了这个代码但应用程序不会打开。它说"不幸的是停止" 。不确定问题出在哪里。请帮我。谢谢。

这是 MainActivity 的代码。

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

Button btnStart;
Context context;
SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
ArrayList<Class> activityList


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btnStart = (Button) findViewById(R.id.btnStart);
    String arrayStr = mPrefs.getString("MyObject","defValue");
    Gson gson = new Gson();
    activityList= gson.fromJson(arrayStr,new TypeToken<List<Class>>(){}.getType());
    //if the cache is null then new arraylist.
    if(activityList==null){
         // We are creating a list, which will store the activities that haven't been opened yet
        activityList = new ArrayList<>();
        activityList.add(first.class);
        activityList.add(second.class);
        activityList.add(third.class);
        activityList.add(fourth.class);
        activityList.add(fifth.class);
    }
    btnStart.setOnClickListener(this);
}

@Override
        public void onClick(View v) {

            Random generator = new Random();
            int number = generator.nextInt(5) + 1;

            Class activity = null;

            switch(number) {
                case 1:
                    activity = first.class;
                    activityList.remove(first.class);
                    break;
                case 2:
                    activity = second.class;
                    activityList.remove(second.class);
                    break;
                case 3:
                    activity = third.class;
                    activityList.remove(third.class);
                    break;
                case 4:
                    activity = fourth.class;
                    activityList.remove(fourth.class);
                    break;
                default:
                    activity = fifth.class;
                    activityList.remove(fifth.class);
                    break;
            }

            Intent intent = new Intent(getBaseContext(), activity);
            intent.putExtra("ACTIVITY_LIST", activityList);
    String json = gson.toJson(activityList); // myObject - instance of MyObject
    prefsEditor.putString("MyObject", json);
    prefsEditor.commit();
    startActivity(intent);
        }}

以及第一次活动的代码:

public class first extends AppCompatActivity implements View.OnClickListener{
EditText etAnswer;
Button btnGo;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
    etAnswer = (EditText) findViewById(R.id.etAnswer);
    btnGo = (Button) findViewById(R.id.btnGo);
    btnGo.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.btnGo:
            String answer = etAnswer.getText().toString();
            if(answer.equals("Jose Rizal") ||  answer.equals("jose rizal") || answer.equals("Rizal") || answer.equals("rizal") ){
                AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
                dlgAlert.setMessage("The famous Rizal monument in Luneta was not the work of a Filipino but a Swiss sculptor named Richard Kissling?" +
                        "\n" +
                        "\n" +
                        "Source: http://www.joserizal.ph/ta01.html");
                dlgAlert.setTitle("Did you know that ...");
                dlgAlert.setPositiveButton("Next",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                ArrayList<Class> activityList = new ArrayList<>();
                                Bundle extras = getIntent().getExtras();
                                activityList = (ArrayList<Class>) extras.get("ACTIVITY_LIST");

                                if(activityList.size() == 0) {
                                    Context context = getApplicationContext();
                                    CharSequence last = "Congratulations! You just finished the game! Please wait for the next update!";
                                    int durationFinal = Toast.LENGTH_LONG;

                                    Toast toast = Toast.makeText(context, last, durationFinal);
                                    toast.show();
                                } else {
                                    // Now, the random number is generated between 1 and however many
                                    // activities we have remaining
                                    Random generator = new Random();
                                    int number = generator.nextInt(activityList.size()) + 1;

                                    Class activity = null;

                                    // Here, we are checking to see what the output of the random was
                                    switch(number) {
                                        case 1:
                                            // We will open the first remaining activity of the list
                                            activity = activityList.get(0);
                                            // We will now remove that activity from the list
                                            activityList.remove(0);
                                            break;
                                        case 2:
                                            // We will open the second remaining activity of the list
                                            activity = activityList.get(1);
                                            activityList.remove(1);
                                            break;
                                        case 3:
                                            // We will open the third remaining activity of the list
                                            activity = activityList.get(2);
                                            activityList.remove(2);
                                            break;
                                        case 4:
                                            // We will open the fourth remaining activity of the list
                                            activity = activityList.get(3);
                                            activityList.remove(3);
                                            break;
                                        default:
                                            // We will open the fifth remaining activity of the list
                                            activity = activityList.get(4);
                                            activityList.remove(4);
                                            break;
                                    }

                                    // Note: in the above, we might not have 3 remaining activities, for example,
                                    // but it doesn't matter because that case wouldn't be called anyway,
                                    // as we have already decided that the number would be between 1 and the number of
                                    // activities left.


                                    // Starting the activity, and passing on the remaining number of activities
                                    // to the next one that is opened
                                    Intent intent = new Intent(getBaseContext(), activity);
                                    intent.putExtra("ACTIVITY_LIST", activityList);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                    startActivity(intent);
                                }
                            }
                        });
                dlgAlert.setCancelable(true);
                dlgAlert.create().show();

            }else{
                Context context = getApplicationContext();
                CharSequence text = "Wrong! Try Again.";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
            break;
    }
}}

这是我的第二次活动:

public class second extends AppCompatActivity implements View.OnClickListener{
EditText etAnswer;
Button btnGo;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    etAnswer = (EditText) findViewById(R.id.etAnswer);
    btnGo = (Button) findViewById(R.id.btnGo);
    btnGo.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.btnGo:
            String answer = etAnswer.getText().toString();
            if(answer.equals("Antonio Luna") || answer.equals("antonio luna") || answer.equals("Heneral Luna") || answer.equals("heneral luna") ){
                AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
                dlgAlert.setMessage("He is the younger brother of the famous Filipino painter Juan Luna who is known for his masterpiece Spoliarium." +
                        "\n" +
                        "\n" +
                        "Source: http://nobert-bermosa.blogspot.com/2011/05/70-interesting-facts-about-general.html");
                dlgAlert.setTitle("Did you know that ...");
                dlgAlert.setPositiveButton("Next",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                ArrayList<Class> activityList = new ArrayList<>();
                                Bundle extras = getIntent().getExtras();
                                activityList = (ArrayList<Class>) extras.get("ACTIVITY_LIST");

                                if(activityList.size() == 0) {
                                    Context context = getApplicationContext();
                                    CharSequence last = "Congratulations! You just finished the game! Please wait for the next update!";
                                    int durationFinal = Toast.LENGTH_LONG;

                                    Toast toast = Toast.makeText(context, last, durationFinal);
                                    toast.show();
                                } else {
                                    // Now, the random number is generated between 1 and however many
                                    // activities we have remaining
                                    Random generator = new Random();
                                    int number = generator.nextInt(activityList.size()) + 1;

                                    Class activity = null;

                                    // Here, we are checking to see what the output of the random was
                                    switch(number) {
                                        case 1:
                                            // We will open the first remaining activity of the list
                                            activity = activityList.get(0);
                                            // We will now remove that activity from the list
                                            activityList.remove(0);
                                            break;
                                        case 2:
                                            // We will open the second remaining activity of the list
                                            activity = activityList.get(1);
                                            activityList.remove(1);
                                            break;
                                        case 3:
                                            // We will open the third remaining activity of the list
                                            activity = activityList.get(2);
                                            activityList.remove(2);
                                            break;
                                        case 4:
                                            // We will open the fourth remaining activity of the list
                                            activity = activityList.get(3);
                                            activityList.remove(3);
                                            break;
                                        default:
                                            // We will open the fifth remaining activity of the list
                                            activity = activityList.get(4);
                                            activityList.remove(4);
                                            break;
                                    }

                                    // Note: in the above, we might not have 3 remaining activities, for example,
                                    // but it doesn't matter because that case wouldn't be called anyway,
                                    // as we have already decided that the number would be between 1 and the number of
                                    // activities left.


                                    // Starting the activity, and passing on the remaining number of activities
                                    // to the next one that is opened
                                    Intent intent = new Intent(getBaseContext(), activity);
                                    intent.putExtra("ACTIVITY_LIST", activityList);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                    startActivity(intent);
                                }
                            }
                        });
                dlgAlert.setCancelable(true);
                dlgAlert.create().show();

            }else{
                Context context = getApplicationContext();
                CharSequence text = "Wrong! Try Again.";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
            break;
    }
}}

1 个答案:

答案 0 :(得分:0)

您可以使用ViewPager一个Activity来显示多个问题,如果您无法使用ViewPager并通过多个Activitys,那么您可以存储您的问题,使用SharedPrefrencesApplicationContext来回答状态,或者你应该使用SQLite数据库制作你的游戏。