Android ArrayList Verileri Kaydetme
06.04.2020 - 03:24
selamlar
Şu kodda nasıl son sorulan soruya dönebilirim uygulma kapatılıp açıldığında
arraylisti gson ile kaydetmeye çalıştım ama olmuyor buton eklemek de hata veriyor
yardımlarınız için Hepinize şimididen Teşekkürler
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private TextView countLabel;
private ImageView questionImage;
private EditText editText;
private String rightAnswer;
private int rightAnswerCount = 0;
private int quizCount = 1;
ArrayList<ArrayList<String>> quizArray = new ArrayList<>();
String quizData[][] = {
// {"Image Name", "Right Answer"}
{"image_circle", "circle"},
{"image_triangle", "triangle"},
{"image_square", "square"},
{"image_pentagon", "pentagon"},
{"image_hexagon", "hexagon"},
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countLabel = findViewById(R.id.countLabel);
questionImage = findViewById(R.id.questionImage);
editText = findViewById(R.id.editText);
editText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
checkAnswer();
}
}
return false;
}
});
// Create quizArray from quizData.
for (int i = 0; i < quizData.length; i++) {
// Prepare array.
ArrayList<String> tmpArray = new ArrayList<>();
tmpArray.add(quizData[i][0]); // Image Name
tmpArray.add(quizData[i][1]); // Right Answer
// Add tmpArray to quizArray.
quizArray.add(tmpArray);
}
showNextQuiz();
}
public void showNextQuiz() {
// Update quizCountLabel.
countLabel.setText("Q" + quizCount);
// Generate random number between 0 and 4 (quizArray's size -1)
Random random = new Random();
int randomNum = random.nextInt(quizArray.size());
// Pick one quiz set.
ArrayList<String> quiz = quizArray.get(randomNum);
// Set image and right answer.
// Array format: {"Image Name", "Right Answer"}
questionImage.setImageResource(
getResources().getIdentifier(quiz.get(0), "drawable", getPackageName()));
rightAnswer = quiz.get(1);
// Remove this quiz from quizArray.
quizArray.remove(randomNum);
}
public void checkAnswer() {
// Get editText.
String answer = editText.getText().toString().toLowerCase();
String alertTitle;
if (answer.equals(rightAnswer)) {
// Correct!
alertTitle = "Correct!";
rightAnswerCount++;
} else {
// Wrong...
alertTitle = "Wrong...";
}
// Create Dialog.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(alertTitle);
builder.setMessage("Answer : " + rightAnswer);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
editText.setText("");
if (quizArray.size() < 1) {
// quizArray is empty.
showResult();
} else {
quizCount++;
showNextQuiz();
}
}
});
builder.setCancelable(false);
builder.show();
}
public void showResult() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Result");
builder.setMessage(rightAnswerCount + " / 5");
builder.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
recreate();
}
});
builder.setNegativeButton("Quit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
builder.show();
}
}
555
Görüntülenme
0 Beğeni