-1

I'm a homegrown greenhorn coder, trying to develop a quiz app for my son's 1st birthday (to family members, of course, not for him).

Problem I'm facing is making results of a quiz visible - and in order by score.

As user is making progress through this quiz, there are two variables gathered: his name (String whatName) and his score (int currentScore).

How can I put these two in a chart at the end of a final question? Preferably aligned by currentScore from best to worst.

Thank You!

edit: I intended to use custom class:

public class results {
public String endName;
public int endScore;
}

and then implement it in last method, 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);
}

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Paweł
  • 41
  • 3

2 Answers2

0

As I understand solution you need has to be simple and not necessary professional.

  1. Use ListView in your name_quiz.xml (https://javastart.pl/static/programowanie-android/listview-przyklad-widoku-listowego/ or http://www.vogella.com/tutorials/AndroidListView/article.html with more complex adapter) it's just easier to understand at the beginning than RecyclerView.

  2. In your toEndResults() method write the score to csv file, read previous ones and populate your record list (comma separated values) (https://howtodoinjava.com/apache-commons/parse-read-write-csv-files-opencsv-tutorial/)

  3. If you're not scared of something more - think of using JSON to write whole your list to a file. Just a little bit more complicated but more efficient also (Read Json file in Android and Populate in ListView)

  4. To sort the list you can use example from here: Android-java- How to sort a list of objects by a certain value within the object

Hope your son will like your project.

Pozdro.

muminers
  • 1,190
  • 10
  • 19
0

I would suggest adding an xml file with a listview like this:

<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>

Then define it in onCreate() in your activity:

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); 

Then when updating the list write some code to sort them before populating.

https://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65

Hope that helps

Skromak
  • 490
  • 6
  • 12