如何拆分ArrayList中每个句子并实现打乱重组的学生排序功能?
实现ArrayList中所有句子的拆分与打乱操作
核心思路
- 维护当前题目索引,逐个处理
ArrayList<QuestionModel1>中的每个句子 - 封装拆分+打乱的工具方法,针对每个
QuestionModel1对象的sentence生成打乱后的单词数组 - 替换原固定句子的逻辑,改为动态从当前题目中获取句子并处理
修改后的完整代码
QuestionActivity1 类
public class QuestionActivity1 extends AppCompatActivity { private int presCounter = 0; private int maxpresCounter; private int currentQuestionIndex = 0; // 当前处理的题目索引 private List<QuestionModel1> list; TextView textScreen, textQuestion, textTitle; Button submitBtn, nextBtn; // 新增下一题按钮(用于切换题目) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_question1); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); submitBtn = findViewById(R.id.submitBtn); nextBtn = findViewById(R.id.nextBtn); // 绑定下一题按钮 // 初始化题目列表 list = new ArrayList<>(); list.add(new QuestionModel1("The kids are playing")); list.add(new QuestionModel1("The kids are sleeping")); list.add(new QuestionModel1("The kids are dancing")); // 加载第一道题目 loadCurrentQuestion(); // 下一题按钮点击事件 nextBtn.setOnClickListener(v -> { if (currentQuestionIndex < list.size() - 1) { currentQuestionIndex++; loadCurrentQuestion(); } else { Toast.makeText(this, "所有题目已完成", Toast.LENGTH_SHORT).show(); } }); } // 加载当前题目:拆分句子、打乱单词、生成视图 private void loadCurrentQuestion() { // 清空之前的视图(避免叠加) LinearLayout layoutParent = findViewById(R.id.layoutParent); layoutParent.removeAllViews(); // 获取当前题目 QuestionModel1 currentQuestion = list.get(currentQuestionIndex); // 拆分句子并打乱单词 String[] keys = getShuffledWords(currentQuestion); // 生成打乱后的单词视图 for (String key : keys) { addView(layoutParent, key, findViewById(R.id.editText)); } maxpresCounter = keys.length; presCounter = 0; // 更新题目提示文本(可根据需求调整) if (textQuestion != null) { textQuestion.setText("请排列出正确的句子"); } } // 拆分句子并打乱单词的工具方法 private String[] getShuffledWords(QuestionModel1 question) { String sentence = question.getSentence(); // 按空格拆分句子为单词数组 String[] words = sentence.split(" "); // 将数组转为List进行打乱 List<String> wordList = new ArrayList<>(Arrays.asList(words)); Collections.shuffle(wordList); // 转回数组返回 return wordList.toArray(new String[0]); } // 原有的addView方法(保持你的原有实现逻辑) private void addView(LinearLayout layoutParent, String key, EditText editText) { Button wordBtn = new Button(this); wordBtn.setText(key); wordBtn.setOnClickListener(v -> { if (presCounter < maxpresCounter) { editText.setText(presCounter == 0 ? key : editText.getText().toString() + " " + key); presCounter++; } }); layoutParent.addView(wordBtn); } }
QuestionModel1 类(保持不变)
public class QuestionModel1 { private String sentence; public QuestionModel1(String sentence) { this.sentence = sentence; } public String getSentence() { return sentence; } public void setSentence(String sentence) { this.sentence = sentence; } }
关键改动说明
- 新增
currentQuestionIndex:用于追踪当前处理的题目位置,支持切换不同句子 - 封装
getShuffledWords方法:统一处理句子拆分与打乱逻辑,可复用 - 新增
loadCurrentQuestion方法:负责清空旧视图、加载当前题目并生成打乱后的单词按钮 - 添加下一题逻辑:通过按钮点击切换到列表中的下一个句子,完成所有句子的处理
- 视图清空逻辑:每次加载新题目时清空父布局,避免视图叠加
内容的提问来源于stack exchange,提问作者Kazz Domi
相关产品推荐
相关产品推荐

