我正在尝试从student列等于从微调器中选择的项目的学生表中读取数据。 监听器是spinner.setOnItemSelectedListener。问题是,当我选择微调器项目时,它会停止活动。 当我删除WHERE条款时,它运行正常。,但是我必须使用WHERE CLAUSE。 请帮忙。
public ArrayList<HashMap<String, String>> GetAllStudents(){
ArrayList<HashMap<String, String>> userList = new ArrayList<>();
try{
SQLiteDatabase db = MyDB.getInstance(this).getReadableDatabase();
String query = "SELECT * FROM Students WHERE semester="+selected;
Cursor cursor = db.rawQuery(query,null);
if(cursor.moveToFirst()) {
while (cursor.moveToNext()) {
HashMap<String, String> user = new HashMap<>();
user.put("name", cursor.getString(0));
user.put("roll", cursor.getString(1));
user.put("semester", cursor.getString(2));
user.put("pass", cursor.getString(3));
userList.add(user);
}
}
}
catch (Exception e){
Toast.makeText(this, "Error "+selected, Toast.LENGTH_SHORT).show();
}
return userList;
}
(从注释中获取的代码,用于显示所选来自何处)
String selected;
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selected=parent.getItemAtPosition(position).toString();
loadStudents();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
loadStudents();
答案 0 :(得分:0)
我已经解决了这个问题,我只是在所选变量周围错过了''
这是解决方案 字符串查询=“ SELECT * FROM从学生所在学期='” + selected +“'”;