Android Studio的多选答案的ArrayList的测验应用程序
创始人
2024-08-17 10:30:14
0

以下是一个使用Android Studio的多选答案ArrayList的测验应用程序的解决方法:

首先,在Android Studio中创建一个新的项目。

然后,打开app目录下的build.gradle文件,并在dependencies块中添加以下依赖项:

dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
}

接下来,创建一个新的Java类,名为Question。

public class Question {
    private String question;
    private ArrayList answers;
    private ArrayList correctAnswers;

    public Question(String question, ArrayList answers, ArrayList correctAnswers) {
        this.question = question;
        this.answers = answers;
        this.correctAnswers = correctAnswers;
    }

    public String getQuestion() {
        return question;
    }

    public ArrayList getAnswers() {
        return answers;
    }

    public ArrayList getCorrectAnswers() {
        return correctAnswers;
    }
}

然后,创建一个新的Java类,名为QuestionAdapter。

public class QuestionAdapter extends RecyclerView.Adapter {
    private ArrayList questions;
    private ArrayList> selectedAnswers;

    public QuestionAdapter(ArrayList questions) {
        this.questions = questions;
        this.selectedAnswers = new ArrayList<>();

        for (int i = 0; i < questions.size(); i++) {
            ArrayList selected = new ArrayList<>();
            for (int j = 0; j < questions.get(i).getAnswers().size(); j++) {
                selected.add(false);
            }
            selectedAnswers.add(selected);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_question, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Question question = questions.get(position);
        holder.questionTextView.setText(question.getQuestion());

        ArrayList answers = question.getAnswers();
        ArrayList selected = selectedAnswers.get(position);

        for (int i = 0; i < answers.size(); i++) {
            CheckBox checkBox = new CheckBox(holder.itemView.getContext());
            checkBox.setText(answers.get(i));
            checkBox.setChecked(selected.get(i));

            checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                selected.set(i, isChecked);
            });

            holder.answersLinearLayout.addView(checkBox);
        }
    }

    @Override
    public int getItemCount() {
        return questions.size();
    }

    public ArrayList> getSelectedAnswers() {
        return selectedAnswers;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView questionTextView;
        LinearLayout answersLinearLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            questionTextView = itemView.findViewById(R.id.questionTextView);
            answersLinearLayout = itemView.findViewById(R.id.answersLinearLayout);
        }
    }
}

接下来,创建一个名为MainActivity的Java类,并在布局文件activity_main.xml中添加一个RecyclerView。

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private QuestionAdapter questionAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        ArrayList answers1 = new ArrayList<>();
        answers1.add("A");
        answers1.add("B");
        answers1.add("C");
        ArrayList correctAnswers1 = new ArrayList<>();
        correctAnswers1.add("A");
        correctAnswers1.add("B");
        Question question1 = new Question("Question 1", answers1, correctAnswers1);

        ArrayList answers2 = new ArrayList<>();
        answers2.add("A");
        answers2.add("B");
        answers2.add("C");
        ArrayList correctAnswers2 = new ArrayList<>();
        correctAnswers2.add("B");
        Question question2 = new Question("Question 2", answers2, correctAnswers2);

        ArrayList answers3 = new ArrayList<>();
        answers3.add("A");
        answers3.add("B");
        answers3.add("C");
        ArrayList correctAnswers3 = new ArrayList<>();
        correctAnswers3.add("C");
        Question question3 = new Question("Question 3", answers3, correctAnswers3);

        ArrayList questions = new ArrayList<>();
        questions.add(question1);
        questions.add(question2);
        questions.add(question3);

        questionAdapter = new QuestionAdapter(questions);
        recyclerView.setAdapter(questionAdapter);

        Button submitButton = findViewById(R

相关内容

热门资讯

记者揭秘!智星菠萝辅助(透视辅... 记者揭秘!智星菠萝辅助(透视辅助)拱趴大菠萝辅助神器,扑克教程(有挂细节);模式供您选择,了解更新找...
一分钟揭秘!约局吧能能开挂(透... 一分钟揭秘!约局吧能能开挂(透视辅助)hhpoker辅助靠谱,2024新版教程(有挂教学);约局吧能...
透视辅助!wepoker模拟器... 透视辅助!wepoker模拟器哪个好用(脚本)hhpoker辅助挂是真的,科技教程(有挂技巧);囊括...
透视代打!hhpkoer辅助器... 透视代打!hhpkoer辅助器视频(辅助挂)pokemmo脚本辅助,2024新版教程(有挂教程);风...
透视了解!约局吧德州真的有透视... 透视了解!约局吧德州真的有透视挂(透视脚本)德州局HHpoker透视脚本,必胜教程(有挂分析);亲,...
六分钟了解!wepoker挂底... 六分钟了解!wepoker挂底牌(透视)德普之星开辅助,详细教程(有挂解密);德普之星开辅助是一种具...
9分钟了解!wpk私人辅助(透... 9分钟了解!wpk私人辅助(透视)hhpoker德州透视,插件教程(有挂教学);风靡全球的特色经典游...
推荐一款!wepoker究竟有... 推荐一款!wepoker究竟有透视(脚本)哈糖大菠萝开挂,介绍教程(有挂技术);囊括全国各种wepo...
每日必备!wepoker有人用... 每日必备!wepoker有人用过(脚本)wpk有那种辅助,线上教程(有挂规律);wepoker有人用...
玩家必备教程!wejoker私... 玩家必备教程!wejoker私人辅助软件(脚本)哈糖大菠萝可以开挂,可靠技巧(有挂神器)申哈糖大菠萝...