public class ques1 extends AppCompatActivity {
SQLiteDatabase sql2;
TextView txtv2;
RadioButton rb1,rb2;
Button btn2;
Cursor c;
String Ans="";
这些是我的3阵列列表
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ques1);
ArrayList<String> q = new ArrayList<String>(3);
q.add("1.who is the fastest man on Earth?");
q.add("2.how many days are there in a leap year?");
q.add("3.what is the national animal of India");
ArrayList<String> options1 = new ArrayList<String>(3);
options1.add("Nikhil sudan");
options1.add("365");
options1.add("Lion");
ArrayList<String> options2 = new ArrayList<String>(3);
options2.add("Usain Bolt");
options2.add("366");
options2.add("Tiger");
ArrayList<String> answer = new ArrayList<String>(3);
answer.add("Usain BOlt");
answer.add("365");
answer.add("Tiger");
以及我如何将其插入到我的表中
sql2 = openOrCreateDatabase("ddb", MODE_PRIVATE, null);
sql2.execSQL("create table if not exists bank(question varchar,option1 varchar,option2 varchar,answ varchar)");
for (int i = 0; i < q.size(); i++){
sql2.execSQL("insert into bank values('" + q.get(i) + "','" + options1.get(i) + "','" + options2.get(i) + "','"+answer.get(i)+"')");
}
和原始查询将问题和选项添加到我的android活动并提交按钮以使用 Ans
检查单选按钮的文本值 c=sql2.rawQuery("Select * from bank where q="+q.get(1).charAt(1),null);
c.moveToFirst();
txtv2.setText(c.getString(c.getColumnIndex("question")));
rb1.setText(c.getString(c.getColumnIndex("option1")));
rb2.setText(c.getString(c.getColumnIndex("option2")));
Ans=c.getString(c.getColumnIndex("answ"));
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (rb1.isChecked()){
if (rb1.getText().equals(Ans)){
Toast.makeText(ques1.this,"Right answer",Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(ques1.this,"wrong choice",Toast.LENGTH_SHORT).show();
}
if (rb2.isChecked()){
if (rb1.getText().equals(Ans)){
Toast.makeText(ques1.this,"right Ans",Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(ques1.this,"Wrong choice",Toast.LENGTH_SHORT).show();
}
}
});
我的日志(已更新):
致命的例外:主要 过程:com.nikhil.dingdong,PID:26739 java.lang.RuntimeException:无法启动活动ComponentInfo {com.nikhil.dingdong / com.nikhil.dingdong.ques1}:android.database.sqlite.SQLiteException:near&#34;。&#34;:语法错误(代码1):,编译时:从bank中选择*,其中question =。 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 引起:android.database.sqlite.SQLiteException:near&#34;。&#34 ;:语法错误(代码1):,同时编译:select * from bank where question =。 在android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法) 在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887) 在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498) 在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) 在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37) 在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316) 在android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255) 在com.nikhil.dingdong.ques1.onCreate(ques1.java:67) 在android.app.Activity.performCreate(Activity.java:6237) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
答案 0 :(得分:0)
首先纠正你的答案的arraylist所以它不会给出arrayindexoutofboundexception
ArrayList<String> answer = new ArrayList<String>(3);
answer .add("Usain BOlt");
answer .add("365");
answer .add("Tiger");
编辑如下:
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (rb1.isChecked()){
if (rb1.getText().equals(Ans)){
Toast.makeText(ques1.this,"Right answer",Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(ques1.this,"wrong choice",Toast.LENGTH_SHORT).show();
}
if (rb2.isChecked()){
if (rb2.getText().equals(Ans)){
Toast.makeText(ques1.this,"right Ans",Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(ques1.this,"Wrong choice",Toast.LENGTH_SHORT).show();
}
}
});
在你的rawQuery中删除.charAt(1)
在rawquery
中将q = q.get(1)替换为question = q.get(1)