Add Filter in SearchView with Room and RecyleView
up vote
0
down vote
favorite
(google translate)
Intro:
I have searched for this solution, but did not find it. If there is a duplicate of this problem, please let me know. thanks.
Problem:
I created a storage application with the Room database and RecycleView. Then I looked for ways to add Search to my data. Everything went smoothly, but there was one problem.
If the search word is reduced, then the search is limited to visible data. (Look at the screenshot)
- Normal
- Search is done (Normal)
- Empty word
- Search again and The result
- GIF Image
I hope you understand.
Please help me. I have seen a lot of tutorials but nothing works.
thank you
My Code:
Filter in my Adapter
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
List<Barang> filteredList = new ArrayList<>();
if (charString.isEmpty()) {
filteredList.addAll(namaBarang);
} else {
for (Barang row : namaBarang) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(row);
}
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
namaBarang = (ArrayList<Barang>) filterResults.values;
notifyDataSetChanged();
}
};
}
MainActivity
@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);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
adapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// t = newText.;
adapter.getFilter().filter(newText);
return false;
}
And I have this too in my Adapter:
void setBarangs(List<Barang> barangs){
namaBarang = barangs;
notifyDataSetChanged();
}
android android-recyclerview searchview android-room android-filterable
add a comment |
up vote
0
down vote
favorite
(google translate)
Intro:
I have searched for this solution, but did not find it. If there is a duplicate of this problem, please let me know. thanks.
Problem:
I created a storage application with the Room database and RecycleView. Then I looked for ways to add Search to my data. Everything went smoothly, but there was one problem.
If the search word is reduced, then the search is limited to visible data. (Look at the screenshot)
- Normal
- Search is done (Normal)
- Empty word
- Search again and The result
- GIF Image
I hope you understand.
Please help me. I have seen a lot of tutorials but nothing works.
thank you
My Code:
Filter in my Adapter
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
List<Barang> filteredList = new ArrayList<>();
if (charString.isEmpty()) {
filteredList.addAll(namaBarang);
} else {
for (Barang row : namaBarang) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(row);
}
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
namaBarang = (ArrayList<Barang>) filterResults.values;
notifyDataSetChanged();
}
};
}
MainActivity
@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);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
adapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// t = newText.;
adapter.getFilter().filter(newText);
return false;
}
And I have this too in my Adapter:
void setBarangs(List<Barang> barangs){
namaBarang = barangs;
notifyDataSetChanged();
}
android android-recyclerview searchview android-room android-filterable
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
(google translate)
Intro:
I have searched for this solution, but did not find it. If there is a duplicate of this problem, please let me know. thanks.
Problem:
I created a storage application with the Room database and RecycleView. Then I looked for ways to add Search to my data. Everything went smoothly, but there was one problem.
If the search word is reduced, then the search is limited to visible data. (Look at the screenshot)
- Normal
- Search is done (Normal)
- Empty word
- Search again and The result
- GIF Image
I hope you understand.
Please help me. I have seen a lot of tutorials but nothing works.
thank you
My Code:
Filter in my Adapter
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
List<Barang> filteredList = new ArrayList<>();
if (charString.isEmpty()) {
filteredList.addAll(namaBarang);
} else {
for (Barang row : namaBarang) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(row);
}
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
namaBarang = (ArrayList<Barang>) filterResults.values;
notifyDataSetChanged();
}
};
}
MainActivity
@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);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
adapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// t = newText.;
adapter.getFilter().filter(newText);
return false;
}
And I have this too in my Adapter:
void setBarangs(List<Barang> barangs){
namaBarang = barangs;
notifyDataSetChanged();
}
android android-recyclerview searchview android-room android-filterable
(google translate)
Intro:
I have searched for this solution, but did not find it. If there is a duplicate of this problem, please let me know. thanks.
Problem:
I created a storage application with the Room database and RecycleView. Then I looked for ways to add Search to my data. Everything went smoothly, but there was one problem.
If the search word is reduced, then the search is limited to visible data. (Look at the screenshot)
- Normal
- Search is done (Normal)
- Empty word
- Search again and The result
- GIF Image
I hope you understand.
Please help me. I have seen a lot of tutorials but nothing works.
thank you
My Code:
Filter in my Adapter
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
List<Barang> filteredList = new ArrayList<>();
if (charString.isEmpty()) {
filteredList.addAll(namaBarang);
} else {
for (Barang row : namaBarang) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(row);
}
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
namaBarang = (ArrayList<Barang>) filterResults.values;
notifyDataSetChanged();
}
};
}
MainActivity
@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);
final MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
adapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// t = newText.;
adapter.getFilter().filter(newText);
return false;
}
And I have this too in my Adapter:
void setBarangs(List<Barang> barangs){
namaBarang = barangs;
notifyDataSetChanged();
}
android android-recyclerview searchview android-room android-filterable
android android-recyclerview searchview android-room android-filterable
edited Nov 7 at 23:26
asked Nov 7 at 14:36
Jufry Ananta
11
11
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
My problem has been solved with this:
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
List<Barang> listHasilFilter = new ArrayList<>();
List<Barang> listYgAkandiFilter = new ArrayList<>();
listYgAkandiFilter.addAll(namaBarangC);
if (charString.isEmpty()) {
listHasilFilter = listYgAkandiFilter;
// namaBarang = listYgAkandiFilter;
Log.d(TAG, "" + namaBarang.size());
} else {
for (Barang row : namaBarangC) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
listHasilFilter.add(row);
}
}
}
FilterResults hasilFilter = new FilterResults();
hasilFilter.values = listHasilFilter;
return hasilFilter;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults hasilFilter) {
namaBarang = (ArrayList<Barang>) hasilFilter.values;
notifyDataSetChanged();
}
};
}
Thanks
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
My problem has been solved with this:
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
List<Barang> listHasilFilter = new ArrayList<>();
List<Barang> listYgAkandiFilter = new ArrayList<>();
listYgAkandiFilter.addAll(namaBarangC);
if (charString.isEmpty()) {
listHasilFilter = listYgAkandiFilter;
// namaBarang = listYgAkandiFilter;
Log.d(TAG, "" + namaBarang.size());
} else {
for (Barang row : namaBarangC) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
listHasilFilter.add(row);
}
}
}
FilterResults hasilFilter = new FilterResults();
hasilFilter.values = listHasilFilter;
return hasilFilter;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults hasilFilter) {
namaBarang = (ArrayList<Barang>) hasilFilter.values;
notifyDataSetChanged();
}
};
}
Thanks
add a comment |
up vote
0
down vote
My problem has been solved with this:
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
List<Barang> listHasilFilter = new ArrayList<>();
List<Barang> listYgAkandiFilter = new ArrayList<>();
listYgAkandiFilter.addAll(namaBarangC);
if (charString.isEmpty()) {
listHasilFilter = listYgAkandiFilter;
// namaBarang = listYgAkandiFilter;
Log.d(TAG, "" + namaBarang.size());
} else {
for (Barang row : namaBarangC) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
listHasilFilter.add(row);
}
}
}
FilterResults hasilFilter = new FilterResults();
hasilFilter.values = listHasilFilter;
return hasilFilter;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults hasilFilter) {
namaBarang = (ArrayList<Barang>) hasilFilter.values;
notifyDataSetChanged();
}
};
}
Thanks
add a comment |
up vote
0
down vote
up vote
0
down vote
My problem has been solved with this:
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
List<Barang> listHasilFilter = new ArrayList<>();
List<Barang> listYgAkandiFilter = new ArrayList<>();
listYgAkandiFilter.addAll(namaBarangC);
if (charString.isEmpty()) {
listHasilFilter = listYgAkandiFilter;
// namaBarang = listYgAkandiFilter;
Log.d(TAG, "" + namaBarang.size());
} else {
for (Barang row : namaBarangC) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
listHasilFilter.add(row);
}
}
}
FilterResults hasilFilter = new FilterResults();
hasilFilter.values = listHasilFilter;
return hasilFilter;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults hasilFilter) {
namaBarang = (ArrayList<Barang>) hasilFilter.values;
notifyDataSetChanged();
}
};
}
Thanks
My problem has been solved with this:
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
List<Barang> listHasilFilter = new ArrayList<>();
List<Barang> listYgAkandiFilter = new ArrayList<>();
listYgAkandiFilter.addAll(namaBarangC);
if (charString.isEmpty()) {
listHasilFilter = listYgAkandiFilter;
// namaBarang = listYgAkandiFilter;
Log.d(TAG, "" + namaBarang.size());
} else {
for (Barang row : namaBarangC) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getNamaBarang().toLowerCase().contains(charString.toLowerCase())) {
listHasilFilter.add(row);
}
}
}
FilterResults hasilFilter = new FilterResults();
hasilFilter.values = listHasilFilter;
return hasilFilter;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults hasilFilter) {
namaBarang = (ArrayList<Barang>) hasilFilter.values;
notifyDataSetChanged();
}
};
}
Thanks
answered Nov 8 at 8:21
Jufry Ananta
11
11
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53191600%2fadd-filter-in-searchview-with-room-and-recyleview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown