我正在构建一个Android应用程序。我正在使用Sqlite数据库,四个单选按钮和一个文本视图。当我单击每个单选按钮时,它应显示数据库中的下一组数据。现在,它只显示数据库中的第一个数据行。如何更改代码以使其显示相应的行?
编码
db.open();
try{
final Cursor c = db.getAllQue();
if(c != null)
{
if (c.moveToFirst())
{
int size = c.getCount();
for(int i=0;i<=size;i++)
quen = (TextView) findViewById(R.id.textView1);
anss1 = (RadioButton) findViewById(R.id.radio0);
anss2 = (RadioButton) findViewById(R.id.radio1);
anss3 = (RadioButton) findViewById(R.id.radio2);
anss4 = (RadioButton) findViewById(R.id.radio3);
rgp1 = (RadioGroup) findViewById(R.id.radioGroup1);
String ques = c.getString(0);
String anes1 = c.getString(1);
String anes2 = c.getString(2);
String anes3 = c.getString(3);
String anes4 = c.getString(4);
String anes = c.getString(5);
rgp1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = (RadioButton) findViewById(checkedId);
Toast.makeText(SqlliteActivity.this, "" + radioButton.getText(), 2000).show();
c.moveToNext();
}
});
quen.setText(ques);
anss1.setText(anes1);
anss2.setText(anes2);
anss3.setText(anes3);
anss4.setText(anes4);
}
}
}catch(Exception e){
System.out.println(e);
}
db.close();
}
答案 0 :(得分:0)
不是你只迭代这一行吗? quen = (TextView) findViewById(R.id.textView1);
此外,您正在寻找游标迭代中的每个UI元素以及for ...
但即使你把括号添加到for:
db.open();
try{
final Cursor c = db.getAllQue();
if(c != null)
{
if (c.moveToFirst())
{
int size = c.getCount();
for(int i=0;i<=size;i++) {
quen = (TextView) findViewById(R.id.textView1);
anss1 = (RadioButton) findViewById(R.id.radio0);
anss2 = (RadioButton) findViewById(R.id.radio1);
anss3 = (RadioButton) findViewById(R.id.radio2);
anss4 = (RadioButton) findViewById(R.id.radio3);
rgp1 = (RadioGroup) findViewById(R.id.radioGroup1);
String ques = c.getString(0);
String anes1 = c.getString(1);
String anes2 = c.getString(2);
String anes3 = c.getString(3);
String anes4 = c.getString(4);
String anes = c.getString(5);
rgp1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = (RadioButton) findViewById(checkedId);
Toast.makeText(SqlliteActivity.this, "" + radioButton.getText(), 2000).show();
c.moveToNext();
}
}
您将多次设置OnchangeListener,但是当您真正点击该按钮时,如果光标未关闭,您将移至下一条记录,但不会更新您的信息。
您应该分离UI设置,按钮clic事件和光标或数据管理。
我认为最好的方法可能是:
希望有所帮助:)
答案 1 :(得分:0)
在循环之外启动对象元素。
quen = (TextView) findViewById(R.id.textView1);
anss1 = (RadioButton) findViewById(R.id.radio0);
anss2 = (RadioButton) findViewById(R.id.radio1);
anss3 = (RadioButton) findViewById(R.id.radio2);
anss4 = (RadioButton) findViewById(R.id.radio3);
rgp1 = (RadioGroup) findViewById(R.id.radioGroup1);
使用while循环获取下一条记录。
db.open();
try{
final Cursor c = db.getAllQue();
if(c != null)
{
c.moveToFirst();
do{
//All your operations
//everytime you will get next record.
}while(moveToNext());
}
}catch(Exception e){
e.printstack();
}