我正在尝试在我的应用程序中的列表视图上实现过滤器。目前我有一个listview,它由一个接收查询结果的游标填充(这很好)。我有一个浮动按钮,当点击时会弹出一个对话框,其中包含可供用户输入的过滤器选项。
选项以三个微调器的形式出现。当用户从每个微调器中选择一个项目时,结果被发送到三个不同查询中的一个。我正在使用这些查询来执行过滤,并在每个查询中包含不同的where子句。但是,如果没有选择,则变量值将设置为0
或""
我遇到两个问题,第一个是for循环来检查是否已经选择了某些过滤器(如果已选择它们确定要使用哪个过滤器查询)它似乎只检查第一个{ {1}}即使我在一些微调器中更改了值,第二个在下面的SQL节目中。
我也知道我应该使用比原始查询更好的东西但是我有一种复杂的查询,我知道它可以处理原始查询,所以不要急于改变它。
任何帮助都会非常感谢
纱厂
for
For循环检查微调器信息并运行相应的查询
final Spinner heat = (Spinner) dialog.findViewById(R.id.heatSpinner);
heat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String heat2 = heat.getSelectedItem().toString();
possibleRecipes.finalHeat = Integer.parseInt(heat2);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
possibleRecipes.finalHeat = 0;
}
});
final Spinner dietary = (Spinner) dialog.findViewById(R.id.dietarySpinner);
dietary.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
possibleRecipes.finalDietary = dietary.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
possibleRecipes.finalDietary = "";
}
});
final Spinner time = (Spinner) dialog.findViewById(R.id.timeSpinner);
time.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String time2 = time.getSelectedItem().toString();
possibleRecipes.finalTime = Integer.parseInt(time2);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
possibleRecipes.finalTime = 0;
}
});
查询过滤器
Button dialogButtonOk = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom ingredient_dialog
dialogButtonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(possibleRecipes.finalHeat != 0 && possibleRecipes.finalTime != 0 && possibleRecipes.finalDietary !="")
{
//full filter search
possibleRecipes.filterCursor = adapter.fullFilter(finalHeat, finalDietary, finalTime);
String[] columns = new String[] {adapter.KEY_RECIPE_NAME, adapter.KEY_ID};
int[] to = new int[] {R.id.recipeName};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getApplicationContext(),R.layout.row1, possibleRecipes.filterCursor, columns, to, 0);
ListView recipeList = (ListView) findViewById(R.id.possibleRecipeList);
recipeList.setAdapter(myCursorAdapter);
}
if(possibleRecipes.finalHeat == 0 && possibleRecipes.finalTime != 0 && possibleRecipes.finalDietary !="")
{
//filter for final time and dietary
possibleRecipes.timeAnddietaryFilterCursor = adapter.timeAnddietaryFilter(finalTime, finalDietary);
String[] columns = new String[] {adapter.KEY_RECIPE_NAME, adapter.KEY_ID};
int[] to = new int[] {R.id.recipeName};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getApplicationContext(),R.layout.row1, possibleRecipes.timeAnddietaryFilterCursor, columns, to, 0);
ListView recipeList = (ListView) findViewById(R.id.possibleRecipeList);
recipeList.setAdapter(myCursorAdapter);
}
if(possibleRecipes.finalHeat != 0 && possibleRecipes.finalTime == 0 && possibleRecipes.finalDietary !="")
{
//filter for heat and dietary
possibleRecipes.heatAnddietaryFilterCursor = adapter.heatAnddietaryFilter(finalHeat, finalDietary);
String[] columns = new String[] {adapter.KEY_RECIPE_NAME, adapter.KEY_ID};
int[] to = new int[] {R.id.recipeName};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getApplicationContext(),R.layout.row1, possibleRecipes.heatAnddietaryFilterCursor, columns, to, 0);
ListView recipeList = (ListView) findViewById(R.id.possibleRecipeList);
recipeList.setAdapter(myCursorAdapter);
}
if(possibleRecipes.finalHeat != 0 && possibleRecipes.finalTime != 0 && possibleRecipes.finalDietary.length()>1)
{
//filter for heat and time
possibleRecipes.heatAndtimeFilterCursor = adapter.heatAndtimeFilter(finalHeat, finalTime);
String[] columns = new String[] {adapter.KEY_RECIPE_NAME, adapter.KEY_ID};
int[] to = new int[] {R.id.recipeName};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getApplicationContext(),R.layout.row1, possibleRecipes.heatAndtimeFilterCursor, columns, to, 0);
ListView recipeList = (ListView) findViewById(R.id.possibleRecipeList);
recipeList.setAdapter(myCursorAdapter);
}
else
{
String[] columns = new String[] {adapter.KEY_RECIPE_NAME, adapter.KEY_ID};
int[] to = new int[] {R.id.recipeName};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getApplicationContext(),R.layout.row1, possibleRecipes.cursor, columns, to, 0);
ListView recipeList = (ListView) findViewById(R.id.possibleRecipeList);
recipeList.setAdapter(myCursorAdapter);
}
dialog.dismiss();
}
});
dialog.show();
错误
public Cursor fullFilter(int finalHeat, String finalDietary, int finalTime)
{
return db.rawQuery("select recipe_name, _id from recipes where recipes.recipe_code in " +
"(select ingredients.recipe_code from ingredients inner join kitchen on " +
"kitchen.ingredient_name = ingredients.ingredient_name) where recipes.heat = "+finalHeat+" and recipes.dietary='"+finalDietary+"' and recipes.time = "+ finalTime, null);
}
public Cursor timeAnddietaryFilter(int finalTime, String finalDietary)
{
return db.rawQuery("select recipe_name, _id from recipes where recipes.recipe_code in " +
"(select ingredients.recipe_code from ingredients inner join kitchen on " +
"kitchen.ingredient_name = ingredients.ingredient_name) where recipes.dietary='"+finalDietary+"' and recipes.time = "+ finalTime, null);
}
public Cursor heatAnddietaryFilter(int finalHeat, String finalDietary)
{
return db.rawQuery("select recipe_name, _id from recipes where recipes.recipe_code in " +
"(select ingredients.recipe_code from ingredients inner join kitchen on " +
"kitchen.ingredient_name = ingredients.ingredient_name) where recipes.heat = "+finalHeat+" and recipes.dietary='"+finalDietary, null);
}
public Cursor heatAndtimeFilter(int finalHeat, int finalTime)
{
return db.rawQuery("select recipe_name, _id from recipes where recipes.recipe_code in " +
"(select ingredients.recipe_code from ingredients inner join kitchen on " +
"kitchen.ingredient_name = ingredients.ingredient_name) where recipes.heat = "+finalHeat+" and recipes.time = "+ finalTime, null);
}
答案 0 :(得分:0)
我对旋转器非常有把握但你的SQL错了,你在每个语句中都有两个where
子句,删除最后一个where
并用and
替换它。我已经更改了下面的查询,但您可以将其应用于其他查询。希望这有帮助
替换FullFilter
public Cursor fullFilter(int finalHeat, String finalDietary, int finalTime)
{
return db.rawQuery("select recipe_name, _id from recipes where recipes.recipe_code in " +
"(select ingredients.recipe_code from ingredients inner join kitchen on " +
"kitchen.ingredient_name = ingredients.ingredient_name) and recipes.heat = "+finalHeat+" and recipes.dietary='"+finalDietary+"' and recipes.time = "+ finalTime, null);
}