我正在尝试创建一个自定义联系人应用程序,它只显示那些具有Contact Number 的联系人。首先,有没有什么自动化的方法来做到这一点?假设不是,那么我正在尝试按名称搜索联系人,例如Rohan。
这是代码:-
Cursor photoCursor = getContentResolver().query(
android.provider.ContactsContract.Contacts.CONTENT_URI,
new String[] { ContactsContract.Contacts.PHOTO_ID,
ContactsContract.Contacts.DISPLAY_NAME },
ContactsContract.Contacts.DISPLAY_NAME + " = ?",
new String[]{"Rohan"}, null);
photoCursor.moveToFirst();
while (photoCursor.moveToNext()) {
Log.d("Photo Thumbnail", "" + photoCursor.getString(1));
}
Run Code Online (Sandbox Code Playgroud)
虽然联系人存在,但我没有收到任何日志,如果我删除Selection & Selection Args,我会在日志中看到Rohan。我究竟做错了什么?
我已经根据文档和其他问题检查并仔细检查了我的清单。我不明白为什么 setSearchableInfo 返回 NullPointerException
主活动.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
// current activity is not searchable activity
ComponentName cn = new ComponentName(this, SearchResultsActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
return true;
}
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml
<activity
android:name=".MainActivity"
android:label="@string/title_main_activity">
<intent-filter>
<action android:name="com.csgiusa.treatmentmanager.csgitreatmentmanager.RoomEditDetailed" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.csgiusa.treatmentmanager.csgitreatmentmanager.MainActivity" />
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
</activity>
<activity …Run Code Online (Sandbox Code Playgroud) 根据要创建的文档,我试图将我的应用程序包含在Android TV全局搜索中:
当然,将它们包括在清单中。这就是我所做的,我的contentprovider非常简单。它不返回任何数据,但是当它获得query()调用时,它将在日志中打印一些行,但尚未完成。
public class VideoContentProvider extends ContentProvider {
private static String TAG = "VideoContentProvider";
public static String AUTHORITY = "test.tvsearch";
// UriMatcher stuff
private static final int SEARCH_SUGGEST = 0;
private static final int REFRESH_SHORTCUT = 1;
private static final UriMatcher URI_MATCHER = buildUriMatcher();
//private VideoDatabase mVideoDatabase;
/**
* Builds up a UriMatcher for search suggestion and shortcut refresh queries.
*/
private static UriMatcher buildUriMatcher() {
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
// to get suggestions... …Run Code Online (Sandbox Code Playgroud) 我尝试找到一种为SearchView设置文本的方法,但找不到任何内容?有谁知道这样做的任何方法吗?例如这样的事情
searchView.setText("blah blah");
Run Code Online (Sandbox Code Playgroud) 我有显示项目列表的活动,还有过滤器和搜索选项。我正在使用 android 分页库显示项目。第一次加载项目列表时,当我滚动到底部加载下一组项目时,它的工作正常。但我也想过滤项目并搜索项目。在过滤或搜索项目上,我使现有源无效。如果不使数据源无效,则过滤器和搜索 api 不会触发。我想使用数据源根据我的过滤器和搜索键加载新项目列表。
executor = Executors.newFixedThreadPool(5);
celebrityDataFactory = new CelebrityDataFactory(apicallInterface, mFansismParam);
networkState = Transformations.switchMap(celebrityDataFactory.getCelebrityData(),
dataSource -> dataSource.getNetworkState());
PagedList.Config pagedListConfig =
(new PagedList.Config.Builder())
.setEnablePlaceholders(false)
.setPrefetchDistance(8)
.setInitialLoadSizeHint(10)
.setPageSize(20).build();
if (!mFansismParam.getCategoryId().isEmpty()) {
celebrityDetails = new LivePagedListBuilder(celebrityDataFactory, pagedListConfig)
.setFetchExecutor(executor)
.build();
} else(!mFansismParam.getProfessionId().isEmpty()) {
celebrityDetails = new LivePagedListBuilder(celebrityDataFactory, pagedListConfig)
.setFetchExecutor(executor)
.build();
}
Run Code Online (Sandbox Code Playgroud)
数据工厂创建数据源
@Override
public DataSource create() {
celebrityDataSource = new CelebrityDataSource(apicallInterface, params);
celebrityData.postValue(celebrityDataSource);
return celebrityDataSource;
}
Run Code Online (Sandbox Code Playgroud)
改造 API 调用:
Call<CelebrityList> getCelebrityList(@Query("categoryId") String categoryId,
@Query("professionId") String professionId,
@Query("page") String pageNumber,
@Query("name") String searchKey); …Run Code Online (Sandbox Code Playgroud) android android-search retrofit2 android-paging android-paging-library
我有一个包含两个活动的应用程序,有时,我需要切换活动,同时在刚刚恢复的活动的操作栏中打开搜索输入。一切正常,只是我无法让键盘出现。我的代码的相关部分如下(注意:true如果需要搜索输入,则布尔值 startsearch 设置为切换活动的结果):
public class MyActivity extends Activity {
private InputMethodManager imm;
public boolean startsearch;
private MenuItem DestinationTxt;
private SearchView mySearchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// various initialisation, and then:
startsearch = false;
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
DestinationTxt = menu.findItem(R.id.actionbar_search);
mySearchView = (SearchView)DestinationTxt.getActionView();
// more menu create stuff appears here
}
@Override
public void onResume() {
super.onResume();
if (startsearch) {
DestinationTxt.expandActionView();
imm.showSoftInput(mySearchView, …Run Code Online (Sandbox Code Playgroud) 我在我的android应用程序中实现了可搜索的建议,并在自动完成文本中显示结果(我已经与搜索视图绑定).
以下是我的配置
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="Search Name"
android:searchSuggestAuthority="com.mdb.db"
android:searchSuggestIntentAction="android.intent.action.VIEW" />
Run Code Online (Sandbox Code Playgroud)
这是我的匹配器
MATCHER.addURI(MyApp.AUTHORITY,SearchManager.SUGGEST_URI_PATH_QUERY, DB.NAME.SEARCH_SUGGEST);
MATCHER.addURI(MyApp.AUTHORITY,SearchManager.SUGGEST_URI_PATH_QUERY+"/*", DB.NAME.SEARCH_SUGGEST);
Run Code Online (Sandbox Code Playgroud)
现在的问题是,当我输入文本并查看自动完成列表并点击自动完成内的任何项目时,它会打开相同的活动,而不是在a中设置所选项目searchview.我不知道如何解决这个问题.