Room LiveData,ViewModel。按名称搜索。奇怪的过滤行为

时间:2019-03-09 09:50:04

标签: android android-edittext android-room

当您单击搜索按钮时,它会显示完整的项目列表。最顶部是editText,当用户开始输入名称时,应动态过滤列表。我只精通Architecture Components,看来我设法实现了搜索,但是它工作得很歪。

假设以下名称:“魔术”,“夏天”,“盐”,“火花”等。我开始输入“ s”-列表更改,仅保留带有该字母的单词。然后输入“ a”,应该保留单词“ salt”,但没有任何变化。当我删除键入内容时,列表不会更新。帮助解决问题。

DataDao

 @Query("SELECT * FROM Data WHERE title LIKE :search")
LiveData<List<Data>> getAllSearch(String search);

DataRepository

    public class DataRepository {
private DatabaseCopier db;
private DataDao dataDao;

DataRepository(Application application) {
    db = DatabaseCopier.getInstance(application);
    dataDao = db.getDatabase().dataDao();
}

LiveData<List<Data>> getAllSearch(String search) {
    return db.getDatabase().dataDao().getAllSearch(search);
}

带有工厂的ViewModel,我将名称传递给构造函数。

 public class DataViewModelSearch extends AndroidViewModel {
private LiveData<List<Data>> currentData;
private DataRepository repository;

public DataViewModelSearch(@NonNull Application application, final String dataTitle) {
    super(application);
    String verseTitle = dataTitle;
    repository = new DataRepositorySearch(application);
    currentData = repository.getAllSearch(verseTitle);
}

public LiveData<List<Data>> getAllSearch() {
    return currentData;
}

    public static class ModelFactorySearch extends ViewModelProvider.NewInstanceFactory {
        @NonNull
        private final Application application;
        private final String dataTitle;
        private final DataRepository repository;

        public ModelFactorySearch(@NonNull Application application, String title) {
            super();
            this.application = application;
            this.dataTitle = title;
            repository = new DataRepository(application);
        }

        @NonNull
        @SuppressWarnings("unchecked")
        @Override
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            if (modelClass == DataViewModel.class) {
                return (T) new DataViewModelSearch(application, dataTitle);
            }
            return null;
        }
    }

}

SearchActivity

 public class SearchActivity extends AppCompatActivity {
private SearchAdapter adapter;
RvObserver observer;
EditText etFilter;
RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    observer = new RvObserver();
    etFilter = findViewById(R.id.et_filter);
    recyclerView = findViewById(R.id.rv_search);

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
    dividerItemDecoration.setDrawable(this.getResources().getDrawable(R.drawable.divider));
    recyclerView.addItemDecoration(dividerItemDecoration);
    adapter = new SearchAdapter(onSearchClickListener, this);
    recyclerView.setAdapter(adapter);
    DataListViewModel dataListViewModel = ViewModelProviders.of(this).get(DataListViewModel.class);
    dataListViewModel.getAllVersesABS().observe(this, new Observer<List<Data>>() {
        @Override
        public void onChanged(List<Data> data) {
            adapter.setData(data);
        }
    });

    Bundle bundle = new Bundle(1);
    bundle.putString("filter", "");


    etFilter.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            refresh(charSequence.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }

    });
}


private void refresh(String searchText) {
    Bundle bundle = new Bundle(1);
    bundle.putString("filter", searchText);

    searchText = "%" + searchText + "%";
    final DataViewModelSearch.ModelFactorySearch factory = new DataViewModelSearch.ModelFactorySearch(
            getApplication(), searchText);

    final DataViewModelSearch modelSearch = ViewModelProviders.of(this, factory)
            .get(DataViewModelSearch.class);

    modelSearch.getAllSearch().observe(this, new Observer<List<Data>>() {
        @Override
        public void onChanged(List<Data> data) {
            adapter.setData(data);
            adapter.notifyDataSetChanged();
        }
    });

}

@Override
protected void onResume() {
    super.onResume();
    adapter.registerAdapterDataObserver(observer);
}

@Override
protected void onPause() {
    super.onPause();
    adapter.unregisterAdapterDataObserver(observer);
}

1 个答案:

答案 0 :(得分:0)

首先,您的刷新逻辑有问题。每次键入内容时,都会使用新的存储库创建新的ViewModel。

第二,您将LiveData用作异步工作工具,但是当您更改数据库中的内容时,LiveData会触发。但是,您无需更改数据,而只想执行SQL查询。这就是为什么我在示例中使用AsyncTask(为简单起见)。您可以使用RxJava。

这是一个简单的例子。

SearchActivity

public class SearchActivity extends AppCompatActivity {
private SearchAdapter adapter;
private DataViewModelSearch dataListViewModel;
EditText etFilter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);
    etFilter = findViewById(R.id.et_filter);
    RecyclerView recyclerView = findViewById(R.id.rv_search);

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    adapter = new SearchAdapter(onSearchClickListener, this);
    recyclerView.setAdapter(adapter);

    dataListViewModel = ViewModelProviders.of(this).get(DataListViewModel.class);
    dataListViewModel.searchData.observe(this, new Observer<List<Data>>() {
        @Override
        public void onChanged(List<Data> data) {
            adapter.setData(data); // it will be triggered every time when we change searchData in ViewModel via searchData.setValue()
        }
    });

    etFilter.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            dataListViewModel.refresh(charSequence.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }

    });
}
}    

ViewModel

public class DataViewModelSearch extends AndroidViewModel {

MutableLiveData<List<Data>> searchData = new MutableLiveData<>();

private DataRepository repository;

public DataViewModelSearch(@NonNull Application application) {
    super(application);
    repository = new DataRepositorySearch(application); // better create repos outside viewModel or activity, but this is for example
}

private void refresh(String searchText) {
    String formattedSearchText = "%" + searchText + "%";
    new GetSearchDataTask.execute(formattedSearchText)
}

//for Async Work
private class GetSearchDataTask extends AsyncTask<Void, Void, List<Data>> {

        @Override
        protected List<Data> doInBackground(String... params) {
            return repository.getAllSearch(params[0])
        }

        @Override
        protected void onPostExecute(List<Data> data) {
            super.onPostExecute(data); 
            searchData.setValue(data); //change LiveData value
        }
    }


}

您的存储库应返回List<Data> getAllSearch(String search);