mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 13:48:29 +00:00
Fix search function not work
This commit is contained in:
@@ -131,6 +131,58 @@ public class FoodList extends AppCompatActivity {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setTitle(categoryName);
|
||||||
|
|
||||||
|
//Search
|
||||||
|
materialSearchBar = (MaterialSearchBar) findViewById(R.id.searchBar);
|
||||||
|
materialSearchBar.setHint("Enter your food");
|
||||||
|
loadSuggest();
|
||||||
|
materialSearchBar.setCardViewElevation(10);
|
||||||
|
materialSearchBar.addTextChangeListener(new TextWatcher() {
|
||||||
|
@Override
|
||||||
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||||
|
//When user type their text, we will change suggest list
|
||||||
|
|
||||||
|
List<String> suggest = new ArrayList<String>();
|
||||||
|
for(String search:suggestList) {
|
||||||
|
if(search.toLowerCase().contains(materialSearchBar.getText().toLowerCase()))
|
||||||
|
suggest.add(search);
|
||||||
|
}
|
||||||
|
materialSearchBar.setLastSuggestions(suggest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterTextChanged(Editable s) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
|
||||||
|
@Override
|
||||||
|
public void onSearchStateChanged(boolean enabled) {
|
||||||
|
//When search bar is close
|
||||||
|
//Restore original adapter
|
||||||
|
if(!enabled)
|
||||||
|
recyclerView.setAdapter(adapter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSearchConfirmed(CharSequence text) {
|
||||||
|
//When search finish
|
||||||
|
//Show result of search adapter
|
||||||
|
startSearch(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onButtonClicked(int buttonCode) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -139,59 +191,6 @@ public class FoodList extends AppCompatActivity {
|
|||||||
layoutManager = new LinearLayoutManager(this);
|
layoutManager = new LinearLayoutManager(this);
|
||||||
recyclerView.setLayoutManager(layoutManager);
|
recyclerView.setLayoutManager(layoutManager);
|
||||||
|
|
||||||
//Search
|
|
||||||
materialSearchBar = (MaterialSearchBar) findViewById(R.id.searchBar);
|
|
||||||
materialSearchBar.setHint("Enter your food");
|
|
||||||
loadSuggest();
|
|
||||||
materialSearchBar.setLastSuggestions(suggestList);
|
|
||||||
materialSearchBar.setCardViewElevation(10);
|
|
||||||
materialSearchBar.addTextChangeListener(new TextWatcher() {
|
|
||||||
@Override
|
|
||||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
||||||
//When user type their text, we will change suggest list
|
|
||||||
|
|
||||||
List<String> suggest = new ArrayList<String>();
|
|
||||||
for(String search:suggestList) {
|
|
||||||
if(search.toLowerCase().contains(materialSearchBar.getText().toLowerCase()))
|
|
||||||
suggest.add(search);
|
|
||||||
}
|
|
||||||
materialSearchBar.setLastSuggestions(suggest);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterTextChanged(Editable s) {
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
|
|
||||||
@Override
|
|
||||||
public void onSearchStateChanged(boolean enabled) {
|
|
||||||
//When search bar is close
|
|
||||||
//Restore original adapter
|
|
||||||
if(!enabled)
|
|
||||||
recyclerView.setAdapter(adapter);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSearchConfirmed(CharSequence text) {
|
|
||||||
//When search finish
|
|
||||||
//Show result of search adapter
|
|
||||||
startSearch(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onButtonClicked(int buttonCode) {
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
setTitle(categoryName);
|
|
||||||
|
|
||||||
// calling the action bar
|
// calling the action bar
|
||||||
ActionBar actionBar = getSupportActionBar();
|
ActionBar actionBar = getSupportActionBar();
|
||||||
|
|
||||||
@@ -207,7 +206,7 @@ public class FoodList extends AppCompatActivity {
|
|||||||
|
|
||||||
private void startSearch(CharSequence text) {
|
private void startSearch(CharSequence text) {
|
||||||
//Create query by name
|
//Create query by name
|
||||||
Query searchByName = foodList.orderByChild("Name").equalTo(text.toString());
|
Query searchByName = foodList.orderByChild("name").equalTo(text.toString());
|
||||||
//Create options with query
|
//Create options with query
|
||||||
FirebaseRecyclerOptions<Food> foodOptions = new FirebaseRecyclerOptions.Builder<Food>()
|
FirebaseRecyclerOptions<Food> foodOptions = new FirebaseRecyclerOptions.Builder<Food>()
|
||||||
.setQuery(searchByName, Food.class)
|
.setQuery(searchByName, Food.class)
|
||||||
@@ -243,7 +242,7 @@ public class FoodList extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void loadSuggest() {
|
private void loadSuggest() {
|
||||||
foodList.orderByChild("MenuId").equalTo(categoryId)
|
foodList.orderByChild("menuId").equalTo(categoryId)
|
||||||
.addValueEventListener(new ValueEventListener() {
|
.addValueEventListener(new ValueEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onDataChange(DataSnapshot dataSnapshot) {
|
public void onDataChange(DataSnapshot dataSnapshot) {
|
||||||
@@ -251,6 +250,8 @@ public class FoodList extends AppCompatActivity {
|
|||||||
Food item = postSnapshot.getValue(Food.class);
|
Food item = postSnapshot.getValue(Food.class);
|
||||||
suggestList.add(item.getName());
|
suggestList.add(item.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
materialSearchBar.setLastSuggestions(suggestList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user