通常,来自数据库的数据被加载到recycleview 中。检查一部手机 - 所有规则。检查第二个,那里的列表不是按那个顺序显示的。在 SQL 查询中,我专门指定了按 id 排序,但没有任何变化。有什么问题?
private void loadingLevel() {
Cursor c = mDb.rawQuery("SELECT * FROM levels ORDER BY level_id", null);
c.moveToFirst();
if (c.getCount() != 0) {
for (int i = 0; i < c.getCount(); i++) {
map.put(c.getInt(0), c.getString(1));
c.moveToNext();
}
}
c.close();
}
// заполнение листа для адаптера
private void fillList() {
for(Map.Entry<Integer, String> entry : map.entrySet()) {
int id = entry.getKey();
String word = entry.getValue();
if (id <= currentLevel)
aList.add(new Word(id, word));
else
aList.add(new Word(id, "?"));
}
}
// отображение введенных слов
private void showEnteredWords() {
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL));
LevelAdapter adapter = new LevelAdapter(this, aList);
recyclerView.setAdapter(adapter); // устанавливаем адаптер
adapter.setOnWordClickListener(position -> {
Word word = adapter.getWords().get(position);
if (word.getId() < currentLevel) {
AllRhymesFragment dialog = new AllRhymesFragment();
Bundle args = new Bundle();
args.putInt("level", word.getId());
args.putString("requestFrom", getClass().getSimpleName());
dialog.setArguments(args);
dialog.show(getSupportFragmentManager(), "dialogAllRhymes");
} else
Toasty.error(this, getString(R.string.access_is_closed), Toast.LENGTH_SHORT, false).show();
});
}


很可能您正在使用 HashMap,并且它不保证在对其进行迭代时将保留添加元素的顺序。
如果你用 LinkedHashMap 替换它,那么顺序将被保留。
此外,在按 id 排序的情况下,TreeMap 也适用(按升序对键进行排序),但仅适用于这种特殊情况。