我正在尝试在我的android活动中的searchview中实现建议,但未调用Content provider query()方法。
下面是我的代码,请花一些时间检查它并查找问题
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = findViewById(R.id.searchV);
searchView.setSearchableInfo(searchManager != null ? searchManager.getSearchableInfo(getComponentName()) : null);
searchView.setIconifiedByDefault(false);
activity_main.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.SearchView
android:id="@+id/searchV"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name=".medicaProvider"
android:authorities="meds.medicaProvider" />
</application>
src / xml / searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="Label"
android:hint="hintsss"
android:searchSuggestAuthority="meds.medicaProvider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:includeInGlobalSearch="true"
/>
medicaProvider.java
public class medicaProvider extends ContentProvider {
baseHelper baseAccess ;
String query;
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
sUriMatcher.addURI("meds.medicaProvider",SUGGEST_URI_PATH_QUERY+"/*", 1);
}
@Override
public boolean onCreate() {
baseAccess = baseHelper.getInstance(getContext());
System.out.println("*****************query Created*******************");
return true;
}
@Override
public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
Toast.makeText(getContext(), "it workkks", Toast.LENGTH_SHORT).show();
System.out.println("*****************query called*******************" +
"*****************query called*******************" +
"*****************query called*******************" +
"*****************query called*******************");
if(sUriMatcher.match(uri)==1){
query=uri.getLastPathSegment().toUpperCase();
}
baseAccess.open();
String[] columns= new String[]{BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1,SearchManager.SUGGEST_COLUMN_TEXT_2,SearchManager.SUGGEST_COLUMN_INTENT_DATA} ;
Object[] temp = new Object[] { "0", "default","default","default" };
MatrixCursor maxCursor = new MatrixCursor(columns);
Cursor c =baseAccess.getQuotes(query,null,1);
// Toast.makeText(getContext(),"cursor is "+c.moveToNext(), Toast.LENGTH_SHORT).show();
while(c.moveToNext()){
temp[0]=c.getString(0);
temp[1]=c.getString(1)+" "+c.getString(2)+" "+c.getString(4);
temp[2]=c.getString(7)+"/"+c.getString(15);
temp[3]=c.getString(0);
maxCursor.addRow(temp);
}
return maxCursor;
}
我正在尝试通过
验证query()是否成功System.out.println("*****************query called*******************" +
"*****************query called*******************" +
"*****************query called*******************" +
"*****************query called*******************");
但控制台中没有任何显示
谢谢