Android 使用谷歌地图、模型和可观察工具实现搜索视图的最佳方式
看起来很简单,但目前还没有找到任何答案。我的应用程序中有一个搜索视图,我想在谷歌地图标记上添加搜索功能。考虑到下面的代码实现,我的问题是实现这一点的最佳方法是什么 我的活动被映射到一个可观察对象,以从数据库中检索数据,然后当我获取数据时,我在地图上显示标记。文本视图显示计数。 这是我与谷歌地图的主要片段。如您所见,我使用observable来更新uiAndroid 使用谷歌地图、模型和可观察工具实现搜索视图的最佳方式,android,google-maps-markers,viewmodel,searchview,Android,Google Maps Markers,Viewmodel,Searchview,看起来很简单,但目前还没有找到任何答案。我的应用程序中有一个搜索视图,我想在谷歌地图标记上添加搜索功能。考虑到下面的代码实现,我的问题是实现这一点的最佳方法是什么 我的活动被映射到一个可观察对象,以从数据库中检索数据,然后当我获取数据时,我在地图上显示标记。文本视图显示计数。 这是我与谷歌地图的主要片段。如您所见,我使用observable来更新ui public class MyFragment extends Fragment{ .... private void observerSetup
public class MyFragment extends Fragment{
....
private void observerSetup() {
homeViewModel.getCount().observe(this, new Observer<Integer>() {
@Override
public void onChanged(@Nullable Integer s) {
textView.setText(String.valueOf(s));
}
});
homeViewModel.getAll().observe(this, new Observer<List<Item>>() {
@Override
public void onChanged(@Nullable final List<Items> itemsLoaded) {
if(itemsLoaded != null){
// build markers
buildMarkersFromItems(itemsLoaded);
}
}
});
}
...
}
最后一步是在视图模型和UI中实现过滤器的功能,但我不知道如何实现这一点。我必须隐藏/显示有关搜索过滤器的标记,并更新计数器文本字段。或者我应该在片段中做些什么?
但是如何,最好的方法是什么
这是我的模型。它使用存储库从我的数据库中检索数据
public class HomeViewModel extends AndroidViewModel {
private MutableLiveData<String> mText;
private Repository mRepository;
public HomeViewModel(Application application) {
super(application);
mText = new MutableLiveData<>();
mRepository = new Repository(application);
}
public void filter(String text){
// how to perform something here to update ?
}
public LiveData<Integer> getCount() {
return mRepository.count();
}
public LiveData<List<Item>> getAll() { return mRepository.getAll();
公共类HomeViewModel扩展了AndroidViewModel{
私有可变livedata多行文字;
私有存储库;
公共HomeViewModel(应用程序){
超级(应用);
mText=新的可变LiveData();
mRepository=新存储库(应用程序);
}
公共无效筛选器(字符串文本){
//如何在这里执行更新?
}
公共LiveData getCount(){
返回mRepository.count();
}
public LiveData getAll(){return mRepository.getAll();
谢谢你的帮助
编辑:我发布了我的解决方案,不确定它是否是最好的,但确实有效。如果你有任何建议,请告诉我
public class HomeViewModel extends AndroidViewModel {
private MutableLiveData<String> mText;
private Repository mRepository;
public HomeViewModel(Application application) {
super(application);
mText = new MutableLiveData<>();
mRepository = new Repository(application);
}
public void filter(String text){
// how to perform something here to update ?
}
public LiveData<Integer> getCount() {
return mRepository.count();
}
public LiveData<List<Item>> getAll() { return mRepository.getAll();