我的问题是,只有单击“下一步”按钮时,最后一个选项文本才会更改,其余选项保持不变。我认为循环存在一些问题。有人可以解决这个问题。谢谢你
这是db类:
package com.myapps.quiz;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper extends SQLiteOpenHelper{
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.myapps.quiz/databases/";
private static String DB_NAME = "quiz";
private static String Table_name="Quiz";
private SQLiteDatabase myDataBase;
private SQLiteDatabase myData;
private final Context myContext;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
CopyFiles();
}
}
private void CopyFiles()
{
try
{
InputStream is = myContext.getAssets().open(DB_NAME);
File outfile = new File(DB_PATH,DB_NAME);
outfile.getParentFile().mkdirs();
outfile.createNewFile();
if (is == null)
throw new RuntimeException("stream is null");
else
{
FileOutputStream out = new FileOutputStream(outfile);
// BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(outfile));
byte buf[] = new byte[128];
do {
int numread = is.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);
is.close();
out.close();
}
//AssetFileDescriptor af = am.openFd("world_treasure_hunter_deluxe.apk");
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
/// Get Book content////////
public Cursor getQuiz_Content(int bookId)
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Cursor cur;
cur=myData.rawQuery("select quiz_text from Quiz where quiz_id='"+bookId+"'",null);
cur.moveToFirst();
myData.close();
return cur;
};
//////////////////////////
/// Get Book content////////
public Cursor getQuiz_List()
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
int i;
Cursor cur;
cur=myData.rawQuery("select quiz_id,quiz_text,correct_answer from quiz",null);
cur.moveToFirst();
i = cur.getCount();
myData.close();
return cur;
};
//////////////////////////
public Cursor getAns(int quizid)
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Cursor cur;
cur = myData.rawQuery("select answers from answer where quiz_id='"+quizid+"'", null);
cur.moveToFirst();
myData.close();
return cur;
}
public Cursor getAnsList()
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Cursor cur;
cur = myData.rawQuery("select answers from answer", null);
cur.moveToFirst();
myData.close();
return cur;
}
//---updates a title---
/* public boolean UpdateFavourite_Individual(long rowid,String fav)
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
ContentValues args = new ContentValues();
args.put("bookmark", fav);
return myData.update("lyrics", args,
"rowid=" + rowid, null) > 0;
}*/
//////////////////
}
这是Quiz.java:
package com.myapps.quiz;
import java.io.IOException;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class Quiz extends Activity{
/** Called when the activity is first created. */
final DataBaseHelper db = new DataBaseHelper(this);
Cursor c3;
Cursor c4;
int counter=1;
String label;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String options[] = new String[19];
// get reference to radio group in layout
RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1);
// layout params to use when adding each radio button
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
c4 = db.getAnsList();
c4.moveToFirst();
for (int k=0;k<19;k++)
{
options[k]=c4.getString(0);
c4.moveToNext();
Log.e("value:", options[k]);
}
for (int i = 0; i < 4; i++){
final RadioButton newRadioButton = new RadioButton(this);
c3 = db.getAns(3);
for (int j=0;j<i;j++)
c3.moveToNext();
label = c3.getString(0);
newRadioButton.setText(label);
newRadioButton.setId(6);
radiogroup.addView(newRadioButton, layoutParams);
//getOptions();
try {
db.createDataBase();
db.openDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final Cursor c1;
c1= db.getQuiz_Content(counter);
//rdoAns1.setText("Lisbon");
final TextView tvQuizText = (TextView)findViewById(R.id.TextView01);
tvQuizText.setText(c1.getString(0));
Button btnNext = (Button)findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor c2 = null ;
Cursor c3 =null;
if (counter < 5)
{
counter +=1;
c2 = db.getQuiz_Content(counter);
c3 = db.getAns(counter);
//Log.e("Row Position after increment:",Integer.toString(c2.getPosition()));
tvQuizText.setText(c2.getString(0));
}
for (int j=0;j<3;j++)
{
c3.moveToNext();
label = c3.getString(0);
newRadioButton.setText(label);
}
//Log.e("value:",Integer.toString(counter));
//Log.e("value",c3.getString(0));
}
});
//c3 = db.getAns(4);
// rdoAns1.setText(c3.getString(0));
//rdoAns2.setText(c3.getString(1));
//rdoAns3.setText(c3.getString(2));
//rdoAns4.setText(c3.getString(3));
}
/** add radio buttons to the group */
// private void getOptions()
{
// get reference to radio group in layout
//RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1);
// layout params to use when adding each radio button
//LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
// RadioGroup.LayoutParams.WRAP_CONTENT,
// RadioGroup.LayoutParams.WRAP_CONTENT);
// add 20 radio buttons to the group
//for (int i = 0; i < 4; i++){
// RadioButton newRadioButton = new RadioButton(this);
// c3 = db.getAns(4);
//for (int j=0;j<i;j++)
// c3.moveToNext();
// String label = c3.getString(0);
//newRadioButton.setText(label);
//newRadioButton.setId(i);
//radiogroup.addView(newRadioButton, layoutParams);
}
}
}
这是我的XML布局:
<AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<RadioGroup
android:id="@+id/rdbGp1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_y="30dip"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
</RadioGroup>
<Button android:text="Save" android:id="@+id/btnSave" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_x="50dip" android:layout_y="250dip"></Button>
<Button android:text="Next" android:id="@+id/btnNext" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_x="175dip" android:layout_y="250dip"></Button>
<TextView android:layout_x="17dip" android:layout_y="15dip" android:id="@+id/TextView01" android:layout_width="wrap_content" android:text="@+id/TextView01" android:layout_height="wrap_content"></TextView>
</AbsoluteLayout>
答案 0 :(得分:2)
如果我说得对,并且代码与所有大括号有点混淆,你可以在for循环中创建RadioButtons吗?您创建newRadioButton并将其添加到布局。在完成后,你有newRadioButton,这是你创建的最后一个。当您尝试在NewRadioButton上设置文本时,您唯一要做的就是在同一个单选按钮上设置文本3次。
所以你必须使用他们的RadioGroup单独获取每个RadioButton。
我希望有所帮助。