首页 > Java > java教程 > 正文

Android Quiz App开发:解决用户自定义问题数量循环问题

DDD
发布: 2025-11-16 11:43:01
原创
955人浏览过

android quiz app开发:解决用户自定义问题数量循环问题

本文旨在解决Android Quiz App开发中,用户自定义问题数量时循环无法达到预期次数的问题。通过分析问题代码,提供简化后的解决方案,确保循环能够按照用户设定的数量执行,并成功存储问题和答案,最后跳转到答题页面。

问题分析

原始代码中,循环逻辑存在一些问题,导致无法按照用户设定的问题数量进行循环。主要问题在于:

  1. 循环条件错误: for (int i = 1; i < amounts; i++) 导致循环次数比用户设定的数量少一次。
  2. i == amounts 判断永远为 false: 由于循环条件是 i < amounts,因此 i 永远不会等于 amounts,导致跳转页面的逻辑永远不会执行。
  3. Intent 的启动时机错误: Intent 应该在循环结束后启动,而不是在循环内部的某个特定条件满足时启动。

解决方案

以下是修改后的 logicg() 方法,它解决了上述问题,并简化了代码:

private void logicg() {
    String mount = jshared.getString("amount", "");
    int amounts = Integer.parseInt(mount);

    for (int i = 0; i < amounts; i++) {
        map = new HashMap<>();
        map.put("answer", Objects.requireNonNull(ans.getText()).toString());
        map.put("question", inop.getText().toString());
        jshared2.edit().putString("data", new Gson().toJson(map)).apply(); // 使用 apply 异步提交
    }

    Intent intent = new Intent(question.this, answerscrn.class);
    startActivity(intent);
}
登录后复制

代码解释:

AI建筑知识问答
AI建筑知识问答

用人工智能ChatGPT帮你解答所有建筑问题

AI建筑知识问答 22
查看详情 AI建筑知识问答
  1. 循环条件修改: for (int i = 0; i < amounts; i++) 将循环起始值改为 0,循环条件改为 i < amounts,确保循环执行 amounts 次。
  2. 移除不必要的判断: 移除了 if (i == amounts) 判断,因为跳转页面的逻辑应该在循环结束后执行。
  3. 异步提交: 使用 apply() 方法代替 commit() 方法,异步提交数据到 SharedPreferences,避免阻塞主线程。

注意事项:

  • 确保 ans.getText() 和 inop.getText() 不为空,否则可能会导致 NullPointerException。可以使用 TextUtils.isEmpty() 方法进行判断,或者在UI上进行强制输入。
  • SharedPreferences 适合存储少量数据,如果需要存储大量问题和答案,建议使用数据库。
  • 建议使用 apply() 方法代替 commit() 方法,异步提交数据到 SharedPreferences,避免阻塞主线程。commit() 是同步操作,可能会导致UI卡顿。
  • 在循环内部创建 HashMap 可能会导致内存占用过高,如果问题数量非常大,可以考虑在循环外部创建 HashMap,并在每次循环时清空。

完整示例代码:

package com.prgr.quizards.canary;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatButton;

import com.google.android.material.textfield.TextInputEditText;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Objects;

public class question extends Activity {

    private HashMap<String, Object> map = new HashMap<>();
    private TextView text;
    private EditText inop;
    private TextInputEditText ans;
    private AppCompatButton btn;
    private SharedPreferences jshared2;

    private SharedPreferences jshared;

    @Override
    protected void onCreate(Bundle _savedInstanceState) {
        super.onCreate(_savedInstanceState);
        setContentView(R.layout.activity_question);
        initializedata();
        AppCompatButton btn2 = findViewById(R.id.button3);
        btn = findViewById(R.id.button);
        btn2.setOnClickListener(v -> gotoback());
        btn.setOnClickListener(view -> logicg());

    }
    public void gotoback(){
        Intent intent = new Intent(question.this, activity_home_screen.class);
        startActivity(intent);
    }

    private void initializedata(){
        jshared = getSharedPreferences("j", Activity.MODE_PRIVATE);
        jshared2 = getSharedPreferences("j2", Activity.MODE_PRIVATE);
        inop = findViewById(R.id.inop);
        ans = findViewById(R.id.ans);
        text = findViewById(R.id.text1);
    }
    private void logicg() {
        String mount = jshared.getString("amount", "");
        int amounts = Integer.parseInt(mount);

        for (int i = 0; i < amounts; i++) {
            String question = inop.getText().toString();
            String answer = Objects.requireNonNull(ans.getText()).toString();

            if (TextUtils.isEmpty(question) || TextUtils.isEmpty(answer)) {
                Toast.makeText(getApplicationContext(), "Question or Answer cannot be empty", Toast.LENGTH_SHORT).show();
                return; // 停止执行,提示用户输入
            }

            map = new HashMap<>();
            map.put("answer", answer);
            map.put("question", question);
            jshared2.edit().putString("data", new Gson().toJson(map)).apply();
        }

        Intent intent = new Intent(question.this, answerscrn.class);
        startActivity(intent);
    }
}
登录后复制

总结

通过修改循环条件,移除不必要的判断,并使用 apply() 方法异步提交数据,可以解决Android Quiz App开发中用户自定义问题数量时循环无法达到预期次数的问题。同时,需要注意输入校验和数据存储方式的选择,以确保程序的稳定性和性能。

以上就是Android Quiz App开发:解决用户自定义问题数量循环问题的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号