
我正在开发驾驶学校测试应用程序,我需要一些帮助.
我有这样的xml文件:
<!-- language: lang-xml --><?xml version="1.0" enCoding="utf-8"?><ScrollVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/Widget28" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:background="#AAC1E2" > <linearLayout androID:ID="@+ID/Widget29" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:orIEntation="vertical" > <ImageVIEw androID:ID="@+ID/i1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" /> <TextVIEw androID:ID="@+ID/e1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Enun" androID:textAppearance="?androID:attr/textAppearanceMedium" /> <RadioGroup androID:ID="@+ID/radioGroup1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" > <Radiobutton androID:ID="@+ID/radio0" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:checked="true" androID:text="Radiobutton" /> <Radiobutton androID:ID="@+ID/radio1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Radiobutton" /> <Radiobutton androID:ID="@+ID/radio2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Radiobutton" /> </RadioGroup> <ImageVIEw androID:ID="@+ID/i2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" /> <TextVIEw androID:ID="@+ID/e2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Enun" androID:textAppearance="?androID:attr/textAppearanceMedium" /> <RadioGroup androID:ID="@+ID/radioGroup2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" > <Radiobutton androID:ID="@+ID/radio0" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:checked="true" androID:text="Radiobutton" /> <Radiobutton androID:ID="@+ID/radio1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Radiobutton" /> <Radiobutton androID:ID="@+ID/radio2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Radiobutton" /> </RadioGroup>… x 30次,并有一个按钮将更正测试并显示是否通过.如果您未通过3个或更少的问题,您就可以通过测试.
问题以这种方式存储:
ID_question ID_topic ID_test问题答案A答案B答案C答案OK图像
> ID_question ID_topic和ID_test是整数字段
> Question和AnswerA-C是文本字段
> AnswerOK是一个整数字段.如果AnswerA OK = 1,AnswerB = 2,AnswerC = 3
>图像是从资源文件夹中提取的相关图像的名称.
我使用数据库填充xml中的每个字段.通过以下方式从数据库中提取图像和问题:
<!-- language: lang-java --> Cursor c = adbh.query(bundle.getString("test")); //"test"-->"SELECT question, image FROM table WHERE ID_test = 1" Integer i = 1; if (c.movetoFirst()) { do { String path = c.getString(index); String nimage = "i"+i; int IDImage = getResources().getIDentifIEr(nimage, "ID", this.getPackagename()); ImageVIEw image = (ImageVIEw) findVIEwByID(IDImage); int IDDrawable = getResources().getIDentifIEr(ruta, "drawable", this.getPackagename()); image.setimageResource(IDDrawable); String nenun = "e"+i; String enun = c.getString(anotherIndex); int IDEnun = getResources().getIDentifIEr(nenun, "ID", this.getPackagename()); TextVIEw txtenun = (TextVIEw) findVIEwByID(IDEnun); txtenun.setText(enun); i++; } while (c.movetoNext());如何填充单选按钮?该查询将是“从表WHERE ID_test = 1中选择答案A,答案B,答案C”
我的最后一个问题是如何纠正测试.我认为将按下的单选按钮(我不知道怎么做)存储在数组中,然后与正确答案进行比较.该查询将是“从表WHERE ID_test = 1中选择SELECT AnswerOK”.
例:
一系列已回答的问题:
1 1 1 2 2 3 2 3 1 3 2 …
正确答案数组:
3 2 1 2 2 3 2 3 2 2 2 …
<!-- language: lang-java --> for (int i=0; i<30; i++) if (a[i] == b[i]) numberOfRightAnswers++;谢谢 :)
解决方法:
How can I populate radiobuttons? The query would be “SELECT AnswerA,
AnswerB, AnswerC from table WHERE ID_test = 1”
可能想直接在第一个查询中选择这些值,所以代替:
Cursor c = adbh.query(bundle.getString("test")); //"test"-->"SELECT question, image FROM table WHERE ID_test = 1"将具有:
Cursor c = adbh.query(bundle.getString("test")); //"test"-->"SELECT question, AnswerA, AnswerB, AnswerC, image FROM table WHERE ID_test = 1"然后在while循环中,像对TextVIEw txtenun一样,将文本分配给Radiobuttons.
And my last question is how can I correct the test. I think on storing
radiobutton pressed (I don’t kNow exactly how) in an array and then
compare with correct answers.
将OnCheckedchangelistener()添加到所有RadioGroup中.在该侦听器中,您将获得RadioGroup,用户在其中检查了某些内容以及所检查的Radiobutton的ID.用它来构造正确答案的数组.
一些建议:
也许您应该修改布局和当前方法.让用户滚动查看30个问题的布局可能不是一个好主意,尽管您只有在遇到特定问题时才会真正看到它们,但您正在加载许多资源(我不知道您的图片大小,如果它们很小,则可能不是问题).您还可以选择使用ListVIEw布局或根据您所在的问题按需填充的一个问题的简单布局.我的建议将是第二个选择.该选项还将帮助您避免调用getIDentifIEr方法,此方法比findVIEwByID慢,应避免使用.波纹管是一种布局:
<?xml version="1.0" enCoding="utf-8"?><ScrollVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:background="#AAC1E2" > <linearLayout androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:orIEntation="vertical" > <ImageVIEw androID:ID="@+ID/questionImage" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" /> <TextVIEw androID:ID="@+ID/questionText" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Enun" androID:textAppearance="?androID:attr/textAppearanceMedium" /> <RadioGroup androID:ID="@+ID/radioGroup1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" > <Radiobutton androID:ID="@+ID/radio0" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:checked="true" androID:text="Radiobutton" /> <Radiobutton androID:ID="@+ID/radio1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Radiobutton" /> <Radiobutton androID:ID="@+ID/radio2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Radiobutton" /> </RadioGroup> <linearLayout androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:weightSum="3" > <button androID:ID="@+ID/prevIoUsQuestion" androID:layout_wIDth="wrap_content" androID:layout_height="fill_parent" androID:layout_weight="1" androID:text="prevIoUs" /> <button androID:ID="@+ID/valIDateTest" androID:layout_wIDth="wrap_content" androID:layout_height="fill_parent" androID:layout_weight="1" androID:text="valIDate" /> <button androID:ID="@+ID/nextQuestion" androID:layout_wIDth="wrap_content" androID:layout_height="fill_parent" androID:layout_weight="1" androID:text="next" /> </linearLayout> </linearLayout></ScrollVIEw>您将对该测试中的所有问题(包括所有数据)进行查询,并以一个静态字段int counter = 0开始.启动应用程序后,将光标移至计数器值(在计数器值为0时)开始):
c.movetoposition(counter);并使用该行中的值填充上述问题布局:
ImageVIEw questionImage = (ImageVIEw) findVIEwByID(R.ID.questionImage);int IDDrawable = getResources().getIDentifIEr(ruta, "drawable", this.getPackagename()); questionImage.setimageResource(IDDrawable);String enun = c.getString(anotherIndex);TextVIEw txtenun = (TextVIEw) findVIEwByID(R.ID.questionText);txtenun.setText(enun);// will do the same to populate the other parts of the question from the cursor当用户按下nextQuestion按钮时,您将增加计数器的值(您应进行一些检查,以免使30个问题不至于过大),再次将光标移至计数器的位置,然后像上面那样填充布局.当用户按下上一个问题递减计数器时,移动光标并再次使用数据填充布局.
另外,您将只向OnRadIEdGroup添加一个OnCheckedchangelistener.当该监听器触发时,您应该将问题ID(从您应该能够分辨出哪个问题的计数器值中)和选定的Radiobutton存储在数据结构(可能是HashMap)中,然后从数据库,并查看用户是否是一个很好的驱动程序.
您也可以(可能)优化数据库.
编辑:
private ArrayList<Integer> responses = new ArrayList<Integer>(); // this will hold the number of the correct answer. Note that this will work if the user answers question one after the other// if the user skips question you'll get in trouble, this is one of the reasons why your design is not so good.String rg = "radioGroup" + i;int IDRg = getResources().getIDentifIEr(rg, "ID", this.getPackagename());RadioGroup radioGroup = (TextVIEw) findVIEwByID(IDRg);radioGroup.setonChekedchangelistener(new OnCheckedchangelistener(){ public voID onCheckedChanged (RadioGroup group, int checkedID) { if (group.getChildAt(0).getID() == checkedID) { responses.add(0); // the first answer was checked } else if (group.getChildAt(1).getID() == checkedID) { responses.add(1); // the first answer was checked } else if (group.getChildAt(2).getID() == checkedID) { responses.add(2); // the first answer was checked } } }); 总结 以上是内存溢出为你收集整理的从数据库Android填充单选按钮全部内容,希望文章能够帮你解决从数据库Android填充单选按钮所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)