我添加了第二类问题,但我不知道如何使用代码,以便在列表中显示此类别。 请帮帮我 怎么做其他类别也可见? 我在数据库中的类别:
private void addQuestion3() {
QuestionHist q0 = new QuestionHist ("1.Historia Polski - Quiz z wiedzy ogólnej ","1", "0","0", "0", "0", "0", "0","none");
this.addQuestion3(q0);
QuestionHist q1 = new QuestionHist ("2.Historia Świata - Wiedza Ogólna ","1", "0","0", "0", "0", "0", "0","none");
this.addQuestion3(q1);
}
public void addQuestion3(QuestionHist his) {
// SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_SUBJECT, his.getSUBJECT());
values.put(KEY_LEVEL, his.getLEVEL());
values.put(KEY_QUES, his.getQUESTION());
values.put(KEY_ANSWER, his.getANSWER());
values.put(KEY_OPTA, his.getOPTA());
values.put(KEY_OPTB, his.getOPTB());
values.put(KEY_OPTC, his.getOPTC());
values.put(KEY_OPTD, his.getOPTD());
values.put(KEY_TRIAL1, "0");
values.put(KEY_TRIAL2, "0");
values.put(KEY_TRIAL3, "0");
values.put(KEY_TRIAL4, "0");
values.put(KEY_IMAGE, his.getIMAGE());
// Inserting Row
dbase.insert(TABLE_HIS, null, values);
}
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
QuestionHist his = new QuestionHist();
his.setID(cursor.getInt(0));
his.setSUBJECT(cursor.getString(1));
his.setLEVEL(cursor.getString(2));
his.setQUESTION(cursor.getString(3));
his.setANSWER(cursor.getString(4));
his.setOPTA(cursor.getString(5));
his.setOPTB(cursor.getString(6));
his.setOPTC(cursor.getString(7));
his.setOPTD(cursor.getString(8));
his.setTRIAL1(cursor.getString(9));
his.setTRIAL2(cursor.getString(10));
his.setTRIAL3(cursor.getString(11));
his.setTRIAL4(cursor.getString(12));
his.setIMAGE(cursor.getString(13)); //13, because the trials are between OPTD and IMAGE
quesList.add(his);
} while (cursor.moveToNext());
}
return quesList;
}
public List<QuestionHist> getQuestionsFromSubject(String subjectArg) {
List<QuestionHist> quesList = new ArrayList<QuestionHist>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_HIS;
dbase=this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToPosition(1)) {
do {
if (subjectArg.equals(cursor.getString(1))) {
QuestionHist hist = new QuestionHist();
hist.setID(cursor.getInt(0));
hist.setSUBJECT(cursor.getString(1));
hist.setLEVEL(cursor.getString(2));
hist.setQUESTION(cursor.getString(3));
hist.setANSWER(cursor.getString(4));
hist.setOPTA(cursor.getString(5));
hist.setOPTB(cursor.getString(6));
hist.setOPTC(cursor.getString(7));
hist.setOPTD(cursor.getString(8));
hist.setTRIAL1(cursor.getString(9));
hist.setTRIAL2(cursor.getString(10));
hist.setTRIAL3(cursor.getString(11));
hist.setTRIAL4(cursor.getString(12));
hist.setIMAGE(cursor.getString(13)); //13, because the trials are between OPTD and IMAGE
quesList.add(hist);
}
} while (cursor.moveToNext());
}
// return quest list
return quesList;
}
public List<QuestionHist> getQuestionsFromSubjectAndLevel(String subjectArg, int levelArg) {
List<QuestionHist> quesList = new ArrayList<QuestionHist>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_HIS;
dbase=this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToPosition(1)) {
do {
if (subjectArg.equals(cursor.getString(1)) && levelArg == Integer.parseInt(cursor.getString(2))) {
QuestionHist hist = new QuestionHist();
hist.setID(cursor.getInt(0));
hist.setSUBJECT(cursor.getString(1));
hist.setLEVEL(cursor.getString(2));
hist.setQUESTION(cursor.getString(3));
hist.setANSWER(cursor.getString(4));
hist.setOPTA(cursor.getString(5));
hist.setOPTB(cursor.getString(6));
hist.setOPTC(cursor.getString(7));
hist.setOPTD(cursor.getString(8));
hist.setTRIAL1(cursor.getString(9));
hist.setTRIAL2(cursor.getString(10));
hist.setTRIAL3(cursor.getString(11));
hist.setTRIAL4(cursor.getString(12));
hist.setIMAGE(cursor.getString(13)); //13, because the trials are between OPTD and IMAGE
quesList.add(hist);
}
} while (cursor.moveToNext());
}
// return quest list
return quesList;
}
public String getSubjects() {
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_HIS;
dbase=this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
String dbSubjects = "";
if (cursor.moveToPosition(0)) {
dbSubjects = cursor.getString(1);
}
return dbSubjects;
和我在活动中的代码:
btngra = (Button) findViewById(R.id.btngra);
btnscores = (Button) findViewById(R.id.btnscores);
btnsend = (Button) findViewById(R.id.btnsend);
btnchangesettings = (Button) findViewById(R.id.btnchangesettings);
listSubjects = (ListView) findViewById(R.id.listView1);
final QuizHelper db = new QuizHelper(this);
String subjectsRaw = db.getSubjects();
final String[] subjects = subjectsRaw.split("/");
// Definiowanie adaptera
// Pierwszy parametr - Kontekst
// Drugi parametr - Układ rzędzie
// Trzeci parametr - identyfikator TextView z którym są zapisywane dane
// czwarty - tablica danych
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, subjects) {
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
/*YOUR CHOICE OF COLOR*/
textView.setTextColor(Color.WHITE);
;
return view;
}
};
// Przypisywanie adapterow do listy
listSubjects.setAdapter(adapter);
listSubjects.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// ListView Item Click Listener
listSubjects.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long qid) {
final String subject = (String) parent.getAdapter().getItem(position);
// Set the item as checked to be highlighted
for (int i = 0; i < listSubjects.getCount(); i++) {
listSubjects.getChildAt(i).setBackgroundColor(Color.BLACK);
}
//listSubjects.setItemChecked(0, true);
view.setBackgroundColor(Color.BLUE);
//przejscie do kategorii
btngra.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Options.this,
HistQuestionActivityCategory.class);
Bundle bun = new Bundle();
bun.putString("subject", subject);
intent.putExtras(bun);
startActivity(intent);
resultsSent = false;
}
});
}
});
和我的屏幕:
如何添加第二类?请帮帮我
我的活动经过修改:
btngra = (Button) findViewById(R.id.btngra);
btnscores = (Button) findViewById(R.id.btnscores);
btnsend = (Button) findViewById(R.id.btnsend);
btnchangesettings = (Button) findViewById(R.id.btnchangesettings);
listSubjects = (ListView) findViewById(R.id.listView1);
final QuizHelper db = new QuizHelper(this);
List<String> subjects = db.getSubjects();
//final String[] subjects = subjectsRaw.split("/");
// Definiowanie adaptera
// Pierwszy parametr - Kontekst
// Drugi parametr - Układ rzędzie
// Trzeci parametr - identyfikator TextView z którym są zapisywane dane
// czwarty - tablica danych
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,subjects) {
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
/*YOUR CHOICE OF COLOR*/
textView.setTextColor(Color.WHITE);
;
return view;
}
};
// Przypisywanie adapterow do listy
listSubjects.setAdapter(adapter);
listSubjects.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// ListView Item Click Listener
listSubjects.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long qid) {
final String subject = (String) parent.getAdapter().getItem(position);
// Set the item as checked to be highlighted
for (int i = 0; i < listSubjects.getCount(); i++) {
listSubjects.getChildAt(i).setBackgroundColor(Color.BLACK);
}
//listSubjects.setItemChecked(0, true);
view.setBackgroundColor(Color.BLUE);
//przejscie do kategorii
btngra.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Options.this,
HistQuestionActivityCategory.class);
Bundle bun = new Bundle();
bun.putString("subject", subject);
intent.putExtras(bun);
startActivity(intent);
resultsSent = false;
}
});
}
});
和QuizHelper:
private void addQuestion3() {
QuestionHist q0 = new QuestionHist ("1.Historia Polski - Quiz z wiedzy ogólnej ","1", "0","0", "0", "0", "0", "0","none");
this.addQuestion3(q0);
QuestionHist q1 = new QuestionHist ("2.Historia Świata - Wiedza Ogólna ","1", "0","0", "0", "0", "0", "0","none");
this.addQuestion3(q1);
QuestionHist q2 = new QuestionHist("1.Historia Polski - Quiz z wiedzy ogólnej ","1","Chrzest Polski miał miejsce w roku", "956", "966", "965", "978", "966","drawable/chrzestpolski");
this.addQuestion3(q2);
QuestionHist q3 = new QuestionHist("1.Historia Polski - Quiz z wiedzy ogólnej ","1","Którą bitwą zakończyło się tzw. 100 dni Napoleona?", "Bitwą Narodów ", "Bitwa pod Borodino", "Bitwą pod Waterloo", "Bitwa nad Berezyną", "Bitwą pod Waterloo","drawable/napoleonbonaparte");
this.addQuestion3(q3);
QuestionHist q4 = new QuestionHist("1.Historia Polski - Quiz z wiedzy ogólnej ","1","W którym roku powstało Księstwo Warszawskie?", "W 1807", "W 1896", "W 1900", "W 1859", "W 1807","drawable/ksiestwowarszawskie");
this.addQuestion3(q4);
QuestionHist q5 = new QuestionHist("1.Historia Polski - Quiz z wiedzy ogólnej ","1","Król Polski Władysław III Warneńczyk zginął w 1444 roku w bitwie pod Warną. Ile miał wówczas lat?", "20 lat ", "44 lata ", "56 lat ", "30 lat ", "20 lat ","drawable/warna");
this.addQuestion3(q5);
QuestionHist q6 = new QuestionHist("1.Historia Polski - Quiz z wiedzy ogólnej ","1","W którym państwie władzę sprawował Francisco Franco?", "W Hiszpanii", "W Portugalii", "We Francji", "We Włoszech", "W Hiszpanii","drawable/franco");
this.addQuestion3(q6);
public List<String> getSubjects(){
SQLiteDatabase db = getReadableDatabase();
// Equivalent to the following SQLite query:
// SELECT DISTINCT col_subject FROM table_his ORDER BY col_subject
Cursor cursor = db.query(
true,
new String[]{KEY_SUBJECT},
TABLE_HIS,
null,null,null,
KEY_SUBJECT + "ASC",
null
);
List<String> subjects = new ArrayList<String>();
try {
// iterate over the cursor
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
subjects.add(cursor.getString(0));
}
}finally {
cursor.close();// release resources
}
return subjects;
}
/*public String getSubjects() {
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_HIS;
dbase=this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
String dbSubjects = "";
if (cursor.moveToPosition(0)) {
dbSubjects = cursor.getString(1);
}
return dbSubjects;
}
*/
修改后有一个问题:真的我做错了什么?
答案 0 :(得分:0)
您的getSubjects()
方法无法执行您的操作。目前它从表中选择所有内容(这是不必要的,您只需要主题列),移动到第一行,然后返回主题。换句话说,它只会返回一个值。它完成时也不会关闭Cursor
。在Activity
中,final String[] subjects = subjectsRaw.split("/");
行可能无法执行任何操作,因为只返回了一个主题。
这是一个应该更好的实现:
public List<String> getSubjects() {
SQLiteDatabase db = getReadableDatabase();
// Equivalent to the following SQLite query:
// SELECT DISTINCT col_subject FROM table_his ORDER BY col_subject
Cursor cursor = db.query(
true, /* distinct */
TABLE_HIS, /* table */
new String[]{KEY_SUBJECT}, /* columns */
null, null, null, null /* where, where args, group by, having */
KEY_SUBJECT + " ASC", /* order by */
null /* limit */
);
List<String> subjects = new ArrayList<>();
try {
// iterate over the cursor
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
subjects.add(cursor.getString(0));
}
} finally {
cursor.close(); // release resources
}
return subjects;
}
使用此方法您不会需要String.split()
来电,您可以立即使用List
。