package com.welchsoftwaredevelopers.myapplication; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.List; import java.util.Map; public class ChoiceActivity extends AppCompatActivity { private ArrayList> script; private ArrayList ScriptQuestion; private RadioGroup group; private SharedPreferences sharedPref; private Boolean clear = false; private Integer current; public ChoiceActivity() { script = new ArrayList<>(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_choice); Intent intent = getIntent(); group = (RadioGroup) findViewById(R.id.radioGroup1); ScriptQuestion = intent.getStringArrayListExtra("Questions"); current = intent.getIntExtra("Next", 0); clear = intent.getBooleanExtra("Clear", false); Integer i = 0; Integer size = intent.getIntExtra("Size", 0); ((TextView) findViewById(R.id.textView)).setText(ScriptQuestion.get(current)); while (i < size) { HashMap tmp1 = new HashMap<>(); tmp1.putAll((HashMap) intent.getSerializableExtra("Opts" + i.toString())); script.add(tmp1); i++; } Log.d("D", size.toString()); Log.d("D", script.toString()); Log.d("D", current.toString()); Log.d("D", ScriptQuestion.toString()); Iterator iter1 = script.get(current).get("Text").iterator(); RadioButton button; while (iter1.hasNext()) { button = new RadioButton(this); button.setText((String) iter1.next()); group.addView(button); } } @Override protected void onStart() { super.onStart(); Integer Total; sharedPref = this.getPreferences(Context.MODE_PRIVATE); if (clear) { Log.d("D", "clear"); Total = 0; SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt(getString(R.string.Total), 0); editor.commit(); } else { Total = sharedPref.getInt(getString(R.string.Total), 0); } ((TextView) findViewById(R.id.textView_choice_total)).setText(Total.toString()); } public void ClickDone(View v) { Integer totalDone = sharedPref.getInt(getString(R.string.Total), 0) + 1; SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt(getString(R.string.Total), totalDone); Intent intent = new Intent(this, MainActivity.class); intent.putExtra(getString(R.string.Total), totalDone); editor.apply(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); finish(); } public void ClickChoice(View v) { if (group.getCheckedRadioButtonId() == -1) { Toast.makeText(this, "Select an option", Toast.LENGTH_SHORT).show(); } else { Intent intent = new Intent(this, ChoiceActivity.class); intent.putStringArrayListExtra("Questions", ScriptQuestion); intent.putExtra("Size", script.size()); Log.d("D", "Sel button: " + ( (Integer) group.indexOfChild(findViewById(group.getCheckedRadioButtonId())) ).toString() ); intent.putExtra("Next", (Integer) script.get(current).get("Destination").get(group.indexOfChild(findViewById(group.getCheckedRadioButtonId())))); Iterator> iter1 = script.iterator(); Integer i = 0; while (iter1.hasNext()) { String nameOpts = "Opts" + i.toString(); intent.putExtra(nameOpts, iter1.next()); i++; } startActivity(intent); } } }