我是一个本土的新手编码器,试图为我儿子的第一个生日开发一个测验应用程序(对于家庭成员,当然,不是为了他)。
我面临的问题是将测验的结果显示出来 - 并按分数排序。
当用户通过此测验取得进展时,会收集两个变量:他的名字(String whatName)和他的分数(int currentScore)。
如何在最终问题结束时将这两个放在图表中? 最好根据currentScore从最佳到最差对齐。
谢谢!
编辑:我打算使用自定义类:
public class results {
public String endName;
public int endScore;
}
然后在最后一个方法toEndResults中实现它。
public class MainActivity extends Activity {
int currentScore = 0;
ArrayList<results> result = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
public void toQuiz (View view) {
EditText name = (EditText) findViewById(R.id.name_set);
String whatName = name.getText().toString();
setContentView(R.layout.question);
TextView contestant = findViewById(R.id.name_quiz);
contestant.setText(whatName);
}
public void right1 (View view) {
currentScore++;
toQuestion2();
}
public void wrong1 (View view) {
toQuestion2();
}
public void toQuestion2 () {
TextView name = findViewById(R.id.name_quiz);
String whatName = name.getText().toString();
setContentView(R.layout.question2);
TextView contestant = findViewById(R.id.name_quiz);
contestant.setText(whatName);
display(currentScore);
}
public void right2 (View view) {
currentScore++;
toQuestion3();
}
public void wrong2 (View view) {
toQuestion3();
}
public void toQuestion3 () {
TextView name = findViewById(R.id.name_quiz);
String whatName = name.getText().toString();
setContentView(R.layout.question3);
TextView contestant = findViewById(R.id.name_quiz);
contestant.setText(whatName);
display(currentScore);
}
public void right3 (View view) {
currentScore++;
toQuestion4();}
public void wrong3 (View view) {
toQuestion4();
}
public void toQuestion4 () {
TextView name = findViewById(R.id.name_quiz);
String whatName = name.getText().toString();
setContentView(R.layout.question4);
TextView contestant = findViewById(R.id.name_quiz);
contestant.setText(whatName);
display(currentScore);
}
public void right4 (View view) {
currentScore++;
toQuestion5();
}
public void wrong4 (View view) {
toQuestion5();
}
public void toQuestion5 () {
TextView name = findViewById(R.id.name_quiz);
String whatName = name.getText().toString();
setContentView(R.layout.question5);
TextView contestant = findViewById(R.id.name_quiz);
contestant.setText(whatName);
display(currentScore);
}
public void right5 (View view) {
currentScore++;
toQuestion6();
}
public void wrong5 (View view) {
toQuestion6();
}
public void toQuestion6 () {
TextView name = findViewById(R.id.name_quiz);
String whatName = name.getText().toString();
setContentView(R.layout.question6);
TextView contestant = findViewById(R.id.name_quiz);
contestant.setText(whatName);
display(currentScore);
}
public void right6 (View view) {
currentScore++;
toQuestion7();
}
public void wrong6 (View view) {
toQuestion7();
}
public void toQuestion7 () {
TextView name = findViewById(R.id.name_quiz);
String whatName = name.getText().toString();
setContentView(R.layout.question7);
TextView contestant = findViewById(R.id.name_quiz);
contestant.setText(whatName);
display(currentScore);
}
public void right7 (View view) {
currentScore++;
toQuestion8();
}
public void wrong7 (View view) {
toQuestion8();
}
public void toQuestion8 () {
TextView name = findViewById(R.id.name_quiz);
String whatName = name.getText().toString();
setContentView(R.layout.question8);
TextView contestant = findViewById(R.id.name_quiz);
contestant.setText(whatName);
display(currentScore);
}
public void right8 (View view) {
currentScore++;
toQuestion9();
}
public void wrong8 (View view) {
toQuestion9();
}
public void toQuestion9 () {
TextView name = findViewById(R.id.name_quiz);
String whatName = name.getText().toString();
setContentView(R.layout.question9);
TextView contestant = findViewById(R.id.name_quiz);
contestant.setText(whatName);
display(currentScore);
}
public void right9 (View view) {
currentScore++;
toQuestion10();
}
public void wrong9 (View view) {
toQuestion10();
}
public void toQuestion10 () {
TextView name = findViewById(R.id.name_quiz);
String whatName = name.getText().toString();
setContentView(R.layout.question10);
TextView contestant = findViewById(R.id.name_quiz);
contestant.setText(whatName);
display(currentScore);
}
public void right10 (View view) {
currentScore++;
toEndResults();
}
public void wrong10 (View view) {
toEndResults();
}
public void toEndResults () {
TextView name = findViewById(R.id.name_quiz);
String whatName = name.getText().toString();
setContentView(R.layout.endResults);
TextView contestant = findViewById(R.id.name_quiz);
contestant.setText(whatName);
display(currentScore);
result.add(0, whatName, currentScore));
}
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.score_quiz);
quantityTextView.setText("" + number);
}
}
答案 0 :(得分:0)
据我了解解决方案,您需要简单而不必专业。
在name_quiz.xml中使用ListView (https://github.com/aFarkas/lazysizes/blob/master/src/lazysizes-core.js#L676或https://javastart.pl/static/programowanie-android/listview-przyklad-widoku-listowego/使用更复杂的适配器)它在开始时比RecyclerView更容易理解。
在你的toEndResults()方法中,将分数写入csv文件,读取之前的分数并填充你的记录列表(逗号分隔值)(http://www.vogella.com/tutorials/AndroidListView/article.html)
如果您不再害怕某些事情 - 请考虑使用JSON将整个列表写入文件。 (https://howtodoinjava.com/apache-commons/parse-read-write-csv-files-opencsv-tutorial/)
要对列表进行排序,您可以在此处使用示例:Read Json file in Android and Populate in ListView
Pozdro。
答案 1 :(得分:0)
我建议添加一个像这样的列表视图的xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
然后在您的活动中的onCreate()中定义它:
setContentView(R.layout.activity_list_view_android_example);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);
//Create an arraylist populated with your values here
//then pass them into an adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
然后在更新列表时写一些代码以在填充之前对它们进行排序。
希望有所帮助