Суть такова, я пытаюсь сохранять и обновлять историю поиска, библиотеку использую
implementation 'com.github.mancj:MaterialSearchBar:0.8.5'
Проблема в следующем, на телефоне работает практически всё корректно за исключением удаления истории, удаляет максимум 5 пунктов из 10 показываемых. А вот на эмуляторе я постоянно в тег
load
пустой массив
2023-04-10 12:04:33.348 12195-12195/com.buratinoapps.materialsearchbarhistory E/load: []
Для меня важно что бы и на эмуляторе всё корректно работало!
public class MainActivity extends AppCompatActivity implements MaterialSearchBar.OnSearchActionListener {
private MaterialSearchBar searchBar;
String SAVE = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchBar = findViewById(R.id.search);
searchBar.setHint("Your product");
searchBar.setSpeechMode(true);
searchBar.setOnSearchActionListener(this);
searchBar.setLastSuggestions(loadSearchSuggestionFromDisk());
searchBar.clearFocus();
}
// Load search history
private List<String> loadSearchSuggestionFromDisk() {
List<String> result = new ArrayList<>();
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("DATA", MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("search_data", null);
if (json != null) {
Type type = new TypeToken<List<String>>() {}.getType();
List<String> lastSearches = gson.fromJson(json, type);
result.addAll(lastSearches);
}
Log.e("load", String.valueOf(result));
return result;
}
@Override
protected void onResume() {
updateSearchSuggestionToDisk(searchBar.getLastSuggestions());
super.onResume();
}
// Update search history
@SuppressLint("ApplySharedPref")
private void updateSearchSuggestionToDisk(List<String> lastSearches) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("DATA", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(lastSearches);
editor.putString("search_data", json);
editor.commit();
Log.e("save", String.valueOf(lastSearches));
}
@Override
protected void onDestroy() {
//save last queries to disk
saveSearchSuggestionToDisk(searchBar.getLastSuggestions());
super.onDestroy();
}
// Saved search history
private void saveSearchSuggestionToDisk(List<String> lastSearches) {
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("DATA", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(lastSearches);
editor.putString("search_data", json);
editor.apply();
Log.e("save", String.valueOf(lastSearches));
}
@Override
public void onSearchStateChanged(boolean b) {
String state = b ? "enable" : "disable";
Toast.makeText(this, "Search " + state, Toast.LENGTH_SHORT).show();
}
@Override
public void onSearchConfirmed(CharSequence charSequence) {
SAVE = charSequence.toString();
Log.e("saves", searchBar.getLastSuggestions().toString());
searchBar.clearFocus();
Toast.makeText(this, "Searching " + charSequence.toString() + " ...", Toast.LENGTH_SHORT).show();
}
@Override
public void onButtonClicked(int i) {
switch (i) {
case MaterialSearchBar.BUTTON_NAVIGATION:
Toast.makeText(MainActivity.this, "Button Nav ", Toast.LENGTH_SHORT).show();
break;
}
}
}