Android arrayList检索数据

丈夫欲遂平生志,一载寒窗一举汤。这篇文章主要讲述Android arrayList检索数据相关的知识,希望能为你提供帮助。
我有两节课:
question model.java

package com.invertemotech.quizapp; public class QuestionModel {public QuestionModel(String questionString, String answer ,String optA, String optB, String optC , String optD) { QuestionString = questionString; Answer = answer; OptA = optA; OptB = optB; OptC = optC; OptD= optD; }public String getQuestionString() {return QuestionString; }public void setQuestionString (String questionString) { QuestionString = questionString; }public void setOptA(String optA) { OptA = optA; }public String getOptA() {return OptA; }public void setOptB(String optB) { OptB = optB; }public String getOptB() { return OptB; }public String getOptC() { return OptC; }public void setOptC(String optC) { OptC = optC; }public String getOptD() { return OptD; }public void setOptD(String optD) { OptD = optD; }public String getAnswer() { return Answer; }public void setAnswer(String answer) { Answer = answer; }private String QuestionString; private String OptA; private String OptB; private String OptC; private String OptD; private String Answer; }

main activity.Java
package com.invertemotech.quizapp; import android.support.v7.app.AppCompat Activity; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.Collections; import java.util.Random; import cn.pedant.SweetAlert.SweetAlertDialog; public class MainActivity extends AppCompatActivity {ArrayList< QuestionModel> questionModelArraylist; TextView questionLabel, questionCountLabel, scoreLabel ; //EditText answerEdt; //TextView txtQuestion; RadioButton option1, option2 , option3 ; Button submitButton; ProgressBar progressBar; QuestionModel currentQuestion; int currentPosition = 0; int numberOfCorrectAnswer = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); questionCountLabel = findViewById(R.id.noQuestion); questionLabel = findViewById(R.id.question); scoreLabel = findViewById(R.id.score); option1 = findViewById(R.id.option1); option2 = findViewById(R.id.option2); option3 = findViewById(R.id.option3); //option4 = findViewById(R.id.option4); submitButton = findViewById(R.id.submit); progressBar = findViewById(R.id.progress); questionModelArraylist = new ArrayList< > (); questionModelArraylist.add(new QuestionModel("Which company is the largest manufacturer of network equipment ?", "IBM", "A: IBM","B: CICSO", "C: DELL" , "D: HP")); questionModelArraylist.add(new QuestionModel("Which of the following is NOT an operating system ?", "Bios", "A: BIOS", "B: LINUX", "C: WINDOWS" , "D: MAC")); questionModelArraylist.add(new QuestionModel("Who is the first cricketer to score an international double century in 50-over match ?", "Sachin Tendulkar", "A: Sachin Tendulkar", "B: Brian Lara", "C: Rohit Sharma" , "D: Rahul Dravid")); questionModelArraylist.add(new QuestionModel("Who is the founder of Apple Inc. ?", "Steve", "A: Bill Gates", "B: Steve Jobs", "C: Suresh" , "D: Mark")); questionModelArraylist.add(new QuestionModel("Which is the biggest largest city in the world ?", "Reno", "A: Shanghai", "B: Vienna", "C: Reno" , "D: Hong Kong")); Collections.shuffle(questionModelArraylist , new Random()); currentQuestion = questionModelArraylist.get(currentPosition); setData(); //setUpQuestion(); submitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {checkAnswer(); } }); } public void checkAnswer(){ RadioGroup grp = findViewById(R.id.options); RadioButton answer = findViewById(grp.getCheckedRadioButtonId()); String newAnswer = String.valueOf(answer.getText()); if(newAnswer.equalsIgnoreCase(questionModelArraylist .get(currentPosition).getAnswer())){ numberOfCorrectAnswer ++; Log.d("Score", "Your score: "+newAnswer); new SweetAlertDialog(MainActivity.this, SweetAlertDialog.SUCCESS_TYPE) .setTitleText("Good job! Keep Going") .setContentText("Right Answer") .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() { @Override public void onClick(SweetAlertDialog sweetAlertDialog) { currentPosition ++; setData(); //answerEdt.setText(""); sweetAlertDialog.dismiss(); } }) .show(); }else {new SweetAlertDialog(MainActivity.this, SweetAlertDialog.WARNING_TYPE) .setTitleText("Wrong Answer") .setContentText("The right answer is : "+questionModelArraylist.get(currentPosition).getAnswer()) .setConfirmText("OK") .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() { @Override public void onClick(SweetAlertDialog sDialog) { sDialog.dismiss(); currentPosition ++; setData(); //answerEdt.setText(""); } }) .show(); }int x = ((currentPosition+1) * 100) / questionModelArraylist.size(); progressBar.setProgress(x); }public void setData(){if(questionModelArraylist.size()> currentPosition) { questionLabel.setText(questionModelArraylist .get(currentPosition).getQuestionString()); option1.setText(questionModelArraylist.get(currentPosition).getOptA()); option2.setText(questionModelArraylist.get(currentPosition).getOptB()); option3.setText(questionModelArraylist.get(currentPosition).getOptC()); // option4.setText(questionModelArraylist.get(currentPosition).getOptD()); scoreLabel.setText("Score :" + numberOfCorrectAnswer + "/" + questionModelArraylist.size()); questionCountLabel.setText("Question No : " + (currentPosition + 1)); }else{new SweetAlertDialog(MainActivity.this, SweetAlertDialog.SUCCESS_TYPE) .setTitleText("You have successfully completed the quiz") .setContentText("Your score is : "+ numberOfCorrectAnswer + "/" + questionModelArraylist.size()) .setConfirmText("Restart") .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() { @Override public void onClick(SweetAlertDialog sDialog) {sDialog.dismissWithAnimation(); currentPosition = 0; numberOfCorrectAnswer = 0; progressBar.setProgress(0); setData(); } }) .setCancelText("Close") .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() { @Override public void onClick(SweetAlertDialog sDialog) {sDialog.dismissWithAnimation(); finish(); } }) .show(); } }

}
我如何在ArrayList中限制。就像我在其中放入300个问题一样,我必须随机使用所有10个问题。每当应用程序打开时,所有问题都应该是随机的,但只有300个问题。它是android中的初级程序。
答案【Android arrayList检索数据】如果源列表具有唯一项,则可以使用以下逻辑:
public static List< String> getRandomList(List< String> list, int n) { List< String> copy = new LinkedList< String> (list); Collections.shuffle(copy); return copy.subList(0, n); }


    推荐阅读