皆さんの助けが必要です。私はクイズを作っていますが、行き詰まっています。まず、カテゴリから選択できるアクティビティを取得しました。
package com.laurenswuytsjordipapen.cultural.pursuit;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import com.laurenswuytsjordipapen.cultural.pursuit.library.UserFunctions;
public class DashboardActivity extends Activity {
UserFunctions userFunctions;
Button btnLogout;
Button btnEuropa;
Button btnAmerika;
Button btnAfrika;
Button btnAzie;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* Dashboard Screen for the application
* */
// Check login status in database
userFunctions = new UserFunctions();
if(userFunctions.isUserLoggedIn(getApplicationContext())){
setContentView(R.layout.dashboard);
btnLogout = (Button) findViewById(R.id.btnLogout);
btnEuropa = (Button) findViewById(R.id.btnEuropa);
btnAmerika = (Button) findViewById(R.id.btnAmerika);
btnAfrika = (Button) findViewById(R.id.btnAfrika);
btnAzie = (Button) findViewById(R.id.btnAzie);
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
userFunctions.logoutUser(getApplicationContext());
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
finish();
}
});
btnEuropa.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent start = new Intent(getApplicationContext(), MainActivity.class);
start.putExtra("continent","europa");
startActivity(start);
// Closing dashboard screen
finish();
}
});
btnAmerika.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent start = new Intent(getApplicationContext(), MainActivity.class);
start.putExtra("continent","amerika");
startActivity(start);
// Closing dashboard screen
finish();
}
});
btnAfrika.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent start = new Intent(getApplicationContext(), MainActivity.class);
start.putExtra("continent","afrika");
startActivity(start);
// Closing dashboard screen
finish();
}
});
btnAzie.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent start = new Intent(getApplicationContext(), MainActivity.class);
start.putExtra("continent","azie");
startActivity(start);
// Closing dashboard screen
finish();
}
});
}else{
// user is not logged in show login screen
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
finish();
}
}
}
そして、クイズ = MainActivity に進みます。インテントを送信して、MainActivity がそれがどのカテゴリであるかを認識できるようにします。
package com.laurenswuytsjordipapen.cultural.pursuit;
import java.util.List;
import com.laurenswuytsjordipapen.cultural.pursuit.library.UserFunctions;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends Activity {
UserFunctions userFunctions;
Button btnLogout;
List<Vragen> quesList;
int score=0;
int qid=0;
Vragen currentQ;
TextView txtVraag;
RadioButton rda, rdb, rdc;
Button butVolgende;
String value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
if (extras != null) {
value = extras.getString("continent");
}
DBHelper db=new DBHelper(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtVraag=(TextView)findViewById(R.id.txtVraag);
rda=(RadioButton)findViewById(R.id.antwoord1);
rdb=(RadioButton)findViewById(R.id.antwoord2);
rdc=(RadioButton)findViewById(R.id.antwoord3);
butVolgende=(Button)findViewById(R.id.btnVolgende);
setQuestionView();
butVolgende.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
RadioGroup grp=(RadioGroup)findViewById(R.id.grpAntwoord);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
if(currentQ.getANSWER().equals(answer.getText()))
{
score++;
Log.d("score", "Your score"+score);
}
if(qid<10){
currentQ=quesList.get(qid);
setQuestionView();
}else{
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void setQuestionView()
{
txtVraag.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
qid++;
}
}
しかし、質問自体に関しては、それらはクラス DBHelper にあります。
package com.laurenswuytsjordipapen.cultural.pursuit;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "DbQuiz";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_OPTA= "opta"; //option a
private static final String KEY_OPTB= "optb"; //option b
private static final String KEY_OPTC= "optc"; //option c
private SQLiteDatabase dbase;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
dbase=db;
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
+ " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
+KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT)";
db.execSQL(sql);
addQuestions();
//db.close();
}
private void addQuestions()
{
Vragen q1=new Vragen("Op welke hooglanden draven er wilde rendieren rond in Groot Brittanie?","Andes gebergte", "Schotse hooglanden", "Britse hooglanden", "Schotse hooglanden");
this.addVraag(q1);
Vragen q2=new Vragen("In welke Italiaanse stad zijn de Ponte Vechio en het Palazzo Medici belangrijke bezienswaardigheden??", "Milan", "Rome", "Firenze", "Firenze");
this.addVraag(q2);
Vragen q3=new Vragen("Wat is het favoriete land van een Gallomaan?","Frankrijk", "Egypte", "Duitsland", "Frankrijk");
this.addVraag(q3);
Vragen q4=new Vragen("Welke Europese hoofdstad is net zoals Rome op zeven heuvels gebouwd?", "Brussel", "Parijs", "Rome", "Brussel");
this.addVraag(q4);
Vragen q5=new Vragen("Wat is naast Vaticaanstad, Monaco, San Marino en Andorra het vijfde Europese ministaatje?","Corsica", "Luxemburg", "Lichtenstein", "Lichtenstein");
this.addVraag(q5);
Vragen q6=new Vragen("Aan welke golf ligt de kuststreek tussen Marseille en Perpignan?", "Golf van Frankrijk", "Golf van Lyon", "Middellandse zee", "Golf van Lyon");
this.addVraag(q6);
Vragen q7=new Vragen("Welke nationaliteit heeft hordeloper Jonathan N Senga?","Belgische", "Canadees", "Ganees", "Belgische");
this.addVraag(q7);
Vragen q8=new Vragen("Welke munt werd de Alpendollar genoemd?", "Oostenrijkse Shilling", "Bulgaarse Lef", "Franse Frank", "Oostenrijkse Shilling");
this.addVraag(q8);
Vragen q9=new Vragen("Aan welke natie gaf Zanzibar zich in 1896 over na exact 38 minuten oorlog voeren?","Engeland", "China", "Amerika", "Engeland");
this.addVraag(q9);
Vragen q10=new Vragen("Welke stad telde als eerste in de geschiedenis meer dan 1 miljoen inwoners?", "Rome", "Tokyo", "Kopenhagen", "Rome");
this.addVraag(q10);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
// Create tables again
onCreate(db);
}
// Adding new question
public void addVraag(Vragen quest) {
//SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUES, quest.getQUESTION());
values.put(KEY_ANSWER, quest.getANSWER());
values.put(KEY_OPTA, quest.getOPTA());
values.put(KEY_OPTB, quest.getOPTB());
values.put(KEY_OPTC, quest.getOPTC());
// Inserting Row
dbase.insert(TABLE_QUEST, null, values);
}
public List<Vragen> getAllQuestions() {
List<Vragen> quesList = new ArrayList<Vragen>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
dbase=this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Vragen quest = new Vragen();
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quest.setANSWER(cursor.getString(2));
quest.setOPTA(cursor.getString(3));
quest.setOPTB(cursor.getString(4));
quest.setOPTC(cursor.getString(5));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
return quesList;
}
public int rowcount()
{
int row=0;
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row=cursor.getCount();
return row;
}
}
Mainativity で取得したインテントをクラス DBHelper に何らかの形で配置する必要があります。したがって、質問に対して if 句と else 句を作成できます。MainActivity から DbHelper にインテント「大陸」を送信する方法を知っている人はいますか :)
前もって感謝します!