我创建了一个 TabLayout,我想通过所选选项卡显示我的数据库条目。这是带有查询的代码片段:
public Cursor filteredItems(int tabPosition) throws SQLException {
Log.d(TAG, "filteredItems called, Parameter: "+tabPosition);
String filter = "";
if(tabPosition == 0){
filter = "[a-cA-C]%";
}else if(tabPosition == 1){
filter = "[d-fD-F]%";
}else{
Log.d(TAG, "default tab");
filter = "[a-zA-Z]%";
}
Cursor cursor = null;
try{
open();
String query ="SELECT _id, surname, prename, street, zipcode, telephone, email FROM "+Database.getMyDbTable()+" " +
"WHERE surname LIKE '"+filter+"';";
Log.d(TAG,"Query "+query);
cursor = database.rawQuery(query, null);
}catch(Exception ex){
Log.e(TAG, "Could not get list");
CharSequence text = dataNotFound;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
return cursor;
}
不幸的是,过滤器不起作用——查询的结果总是空的(表不为空——我已经检查过了)。