I want to implement an ActionBar Search Widget with suggestions ability.
I already have a string array stored in my ORMLite database that I want to use for the suggestions.
How can I do this without creating loads of classes (Provider, Searchable...!)?
3 Answers
It's a sample, I hope it can help you.
To implements more features in this cursor, eg. open activity when press in suggestion item, take a look on this http://developer.android.com/guide/topics/search/adding-custom-suggestions.html
package com.roberto.android.thetracker.app;
import android.database.MatrixCursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuInflater;
public class CustomSearchFragment extends Fragment {
private static final String[] SUGGESTIONS = {
"Bauru", "Sao Paulo", "Rio de Janeiro",
"Bahia", "Mato Grosso", "Minas Gerais",
"Tocantins", "Rio Grande do Sul"
};
private SimpleCursorAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String[] from = new String[] {"cityName"};
final int[] to = new int[] {android.R.id.text1};
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1,
null,
from,
to,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
final SearchView searchView = (SearchView) MenuItemCompat
.getActionView(menu.findItem(R.id.action_search));
searchView.setSuggestionsAdapter(mAdapter);
searchView.setIconifiedByDefault(false);
// Getting selected (clicked) item suggestion
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionClick(int position) {
Cursor cursor = (Cursor) mAdapter.getItem(position);
String txt = cursor.getString(cursor.getColumnIndex("cityName"));
searchView.setQuery(txt, true);
return true;
}
@Override
public boolean onSuggestionSelect(int position) {
// Your code here
return true;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
populateAdapter(s);
return false;
}
});
}
// You must implements your logic to get data using OrmLite
private void populateAdapter(String query) {
final MatrixCursor c = new MatrixCursor(new String[]{ BaseColumns._ID, "cityName" });
for (int i=0; i<SUGGESTIONS.length; i++) {
if (SUGGESTIONS[i].toLowerCase().startsWith(query.toLowerCase()))
c.addRow(new Object[] {i, SUGGESTIONS[i]});
}
mAdapter.changeCursor(c);
}
}
-
I implemented your code correctly, I got the history array from my ORMLite database, it all compiles with no errors. Except when I enter a character that is a start to a string in the database, nothing happens, and when I enter the second character I get
ArrayIndexOutOfBoundsException
... I have no idea why...– MehdiwayMay 15, 2014 at 10:10 -
Here : snipt.org/VCy7#expand I converted the String array to ArrayList so I can add to it later.– MehdiwayMay 15, 2014 at 15:03
-
1
-
2
-
1@betorcs at last I found the issue, the default threshold of autocomplete textview is 2 characters that why it was not working for single character. I had to do this ((AutoCompleteTextView) searchView.findViewById(R.id.search_src_text)).setThreshold(1); to change the default threshold. Dec 3, 2018 at 10:47
Base on betorcs's answer :
you don't need to have
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {...});
you can use this, instead of that
mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
return populateAdapter((String) constraint);
}
});
and on populateAdapter, Replace "return c;" with "mAdapter.changeCursor(c);"
private Cursor populateAdapter(String query) {
.
.
.
return c;
}
private String getSuggestion(int position) {
MatrixCursor cursor = (MatrixCursor) searchView.getSuggestionsAdapter().getCursor();
String suggest1 = cursor.getString(position);
return SUGGESTIONS[Integer.valueOf(suggest1)];
}
-
9While this code might answer the question you still might consider adding a few explanatory sentences as this increases the value of your answer for other users.– MBTMay 17, 2018 at 7:45
CursorAdapter(Context context, Cursor cursor)
. I try to do thisnew CursorAdapter(this, matrix)
with matrix being a MatrixCursor I created. But it's impossible to instantiate a CursorAdapter... ?