我正在尝试在我的一个片段中实现搜索功能,因为用户类型列表将变得更加定义并匹配用户输入。
我让代码在一个活动中工作但是在尝试在片段中实现它时我遇到了错误。我在下面的代码段中收到错误消息,指出.setFilterQueryProvider
无法解析。
有谁可以指出我可能出错的地方?
代码段
adapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return db.fetchRecipes(constraint.toString());
}
});
包含代码段的片段
public class SearchRecipes extends ListFragment{
private ListView lv;
private static int finalHeat;
private static String finalDietary;
private static int finalTime;
private static Cursor filterCursor;
private static Cursor timeAnddietaryFilterCursor;
private static Cursor heatAnddietaryFilterCursor;
private static Cursor heatAndtimeFilterCursor;
private static Cursor cursor;
private static boolean filter;
EditText inputSearch;
SimpleCursorAdapter myCustomAdaper;
public SearchRecipes() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootView = inflater.inflate(R.layout.fragment_search_recipes, container, false);
final DBMain db = new DBMain(getActivity());
db.open();
cursor = db.getAllRecipes();
String[] values = new String[cursor.getCount()];
cursor.moveToFirst();
for(int i = 0; i < cursor.getCount(); i++)
{
String row = cursor.getString(cursor.getColumnIndex("recipe_name"));
values[i] = row;
cursor.moveToNext();
}
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
adapter.getFilter().filter(cs);
//myCursorAdapter.getFilter().filter(cs);
//dynamicSearch.this.myCustomAdaper.getFilter().filter(cs);
//dynamicSearch.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
adapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return db.fetchRecipes(constraint.toString());
}
});
FloatingActionButton filterButton = (FloatingActionButton) rootView.findViewById(R.id.filterButton);
filterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// custom ingredient_dialog
filter = false;
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.filter_dialog);
dialog.setTitle("Add Ingredient");
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();
finalHeat = Integer.parseInt(heat2);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
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) {
finalDietary = dietary.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
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();
finalTime = Integer.parseInt(time2);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
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 (finalHeat != 0 && finalTime != 0 && finalDietary != "") {
//full filter search
filterCursor = db.fullFilter(finalHeat, finalDietary, finalTime);
String[] columns = new String[]{db.KEY_RECIPE_NAME, db.KEY_ID};
int[] to = new int[]{R.id.recipeName};
final SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.possible_recipe_row, filterCursor, columns, to, 0);
//myCursorAdapter.getFilter().filter();
//lv.setAdapter(myCursorAdapter);
//ListView lv = (ListView) rootView.findViewById(R.id.list_view);
//lv.setAdapter(myCursorAdapter);
setListAdapter(adapter);
}
if (finalHeat == 0 && finalTime != 0 && finalDietary != "") {
//filter for final time and dietary
timeAnddietaryFilterCursor = db.timeAnddietaryFilter(finalTime, finalDietary);
String[] columns = new String[]{db.KEY_RECIPE_NAME, db.KEY_ID};
int[] to = new int[]{R.id.recipeName};
final SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.possible_recipe_row, timeAnddietaryFilterCursor, columns, to, 0);
//myCursorAdapter.getFilter().filter();
//lv.setAdapter(myCursorAdapter);
//ListView lv = (ListView) rootView.findViewById(R.id.list_view);
//lv.setAdapter(myCursorAdapter);
setListAdapter(adapter);
}
if (finalHeat != 0 && finalTime == 0 && finalDietary != "") {
//filter for heat and dietary
heatAnddietaryFilterCursor = db.heatAnddietaryFilter(finalHeat, finalDietary);
String[] columns = new String[]{db.KEY_RECIPE_NAME, db.KEY_ID};
int[] to = new int[]{R.id.recipeName};
final SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.possible_recipe_row, heatAnddietaryFilterCursor, columns, to, 0);
//myCursorAdapter.getFilter().filter();
//lv.setAdapter(myCursorAdapter);
//ListView lv = (ListView) rootView.findViewById(R.id.list_view);
//lv.setAdapter(myCursorAdapter);
setListAdapter(adapter);
}
if (finalHeat != 0 && finalTime != 0 && finalDietary.length() > 1) {
//filter for heat and time
heatAndtimeFilterCursor = db.heatAndtimeFilter(finalHeat, finalTime);
String[] columns = new String[]{db.KEY_RECIPE_NAME, db.KEY_ID};
int[] to = new int[]{R.id.recipeName};
final SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.possible_recipe_row, heatAndtimeFilterCursor, columns, to, 0);
//myCursorAdapter.getFilter().filter();
//lv.setAdapter(myCursorAdapter);
//ListView lv = (ListView) rootView.findViewById(R.id.list_view);
//lv.setAdapter(myCursorAdapter);
setListAdapter(adapter);
} else {
String[] columns = new String[]{db.KEY_RECIPE_NAME, db.KEY_ID};
int[] to = new int[]{R.id.recipeName};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.possible_recipe_row, cursor, columns, to, 0);
//ListView recipeList = (ListView) rootView.findViewById(R.id.possibleRecipeList);
//recipeList.setAdapter(myCursorAdapter);
setListAdapter(adapter);
}
dialog.dismiss();
}
});
dialog.show();
}
});
return rootView;
}
}
错误记录
Error:(101, 16) error: cannot find symbol method setFilterQueryProvider(<anonymous FilterQueryProvider>)