我试图在我的应用中使用搜索建议。它应该显示来自网络的最近搜索和自定义搜索建议。但是以下代码不起作用。我无法弄清问题是什么。有人可以帮忙吗?
MainActivity:
public class MainActivity extends AppCompatActivity {
public SearchView searchView;
ListView list;
ArrayList<String> li;
String url;
String key, value;
SearchRecentSuggestions sugg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView1);
Intent intent = getIntent();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// handleIntent(intent);
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
sugg = new SearchRecentSuggestions(
this, SampleRecentSuggestionsProvider.AUTHORITY,
SampleRecentSuggestionsProvider.MODE);
sugg.saveRecentQuery(query, null);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
searchView.setFocusable(true);
searchView.setIconified(false);
searchView.requestFocusFromTouch();
return true;
}
}
SearchRecentSuggestionsProvider:
public class SampleRecentSuggestionsProvider extends
SearchRecentSuggestionsProvider {
public static final String AUTHORITY = "com.example.SampleRecentSuggestionsProvider";
ArrayList<String> li;
public static final int MODE = DATABASE_MODE_QUERIES;
final List<String> suggestions = new ArrayList<>();
String[] columns = { BaseColumns._ID,
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_INTENT_DATA };
MatrixCursor cursor = new MatrixCursor(columns);
public SampleRecentSuggestionsProvider() {
setupSuggestions(AUTHORITY, MODE);
}
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
return 0;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
@Override
public boolean onCreate() {
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
li = new ArrayList<String>();
JsonObjectRequest jobjRequest = new JsonObjectRequest(url
+ selectionArgs[0].trim(),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray sugg = response.getJSONArray(array);
Log.e("USER", "" + sugg);
for (int i = 0; i < sugg.length(); i++) {
li.add(sugg.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
suggestions.clear();
suggestions.addAll(li);
Log.e("Suggestions ", "" + suggestions);
for (int i = 0; i < li.size(); i++) {
String[] tmp = { Integer.toString(i), li.get(i),li.get(i) };
cursor.addRow(tmp);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), error.getMessage(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(jobjRequest);
Cursor recentCursor = super.query(uri, projection, selection, selectionArgs, sortOrder);
MatrixCursor returnMatrix = cursor;
cursor = new MatrixCursor(columns);
Cursor[] cursors = new Cursor[] { recentCursor, returnMatrix };
return new MergeCursor(cursors);
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return 0;
}
}
Searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint"
android:searchSuggestAuthority="com.example.SampleRecentSuggestionsProvider"
android:searchSuggestSelection=" ?" >
</searchable>
清单:
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<provider
android:name=".SampleRecentSuggestionsProvider"
android:authorities="com.example.SampleRecentSuggestionsProvider"
android:enabled="true" />