我的 Android 應用程式在做筆記時遇到問題。當我將模式(在應用程式中)從默認模式更改為深色或淺色時,串列會自行復制。我有一個用于 ListView 的自定義配接器:
public View getView(int position, View listView, ViewGroup parent) {
View v = listView;
TextView tvTitle;
TextView tvContent;
Note n = noteList.get(position);
Set<String> folders = n.getFolders();
Set<String> foldersForChips = new HashSet<>(folders);
foldersForChips.remove("Notes");
foldersForChips.remove("All Notes");
NoteHolder holder = new NoteHolder();
if (listView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.row_layout, null);
// view of the labels of the note ------------------------------------------------------
holder.foldersChipGroup = (ChipGroup) v.findViewById(R.id.labels_of_note);
holder.foldersChipGroup.setChipSpacingHorizontal(2);
holder.foldersChipGroup.setClickable(false);
holder.foldersChipGroup.setFocusable(false);
for (String folder : foldersForChips) {
Chip folderChip = new Chip(context);
folderChip.setText(folder);
folderChip.setTextSize(8);
folderChip.setEnsureMinTouchTargetSize(false);
folderChip.setHeight(40);
folderChip.setChipMinHeight(10);
folderChip.setBackgroundColor(Color.TRANSPARENT);
folderChip.setChipBackgroundColor(null);
ChipDrawable chipFolderDrawable = ChipDrawable.createFromAttributes(context, null,0, R.style.Widget_App_Chip);
folderChip.setChipDrawable(chipFolderDrawable);
holder.foldersChipGroup.addView(folderChip);
}
tvTitle = (TextView) v.findViewById(R.id.title);
tvContent = (TextView) v.findViewById(R.id.content);
holder.titleView = tvTitle;
holder.contentView = tvContent;
v.setTag(holder);
} else {
holder = (NoteHolder) v.getTag();
}
// view of the title and content of the note -----------------------------------------------
if (n.getTitle() == "" || n.getTitle().isEmpty() || n.getTitle() == null ) {
holder.contentView.setVisibility(View.VISIBLE);
holder.contentView.setText(n.getContent());
holder.contentView.setPadding(20,0,20, 0);
} else if (n.getContent() == "" || n.getContent().isEmpty() || n.getContent() == null){
holder.titleView.setVisibility(View.VISIBLE);
holder.titleView.setText(n.getTitle());
holder.titleView.setPadding(20,0,20, 0);
} else {
holder.titleView.setVisibility(View.VISIBLE);
holder.titleView.setText(n.getTitle());
holder.titleView.setPadding(20,0,20, 0);
holder.contentView.setVisibility(View.VISIBLE);
holder.contentView.setText(n.getContent());
holder.contentView.setPadding(20,0,20, 0);
}
// background color ------------------------------------------------------------------------
holder.cardView = v.findViewById(R.id.cardView);
if (getItem(position).getBackgroundColor() != null) {
holder.cardView.setCardBackgroundColor(Color.parseColor(getItem(position).getBackgroundColor()));
}
return v;
}
在 MainActivity.java 中改變模式的按鈕:
FloatingActionButton fabLight = findViewById(R.id.light);
fabLight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
themeChange.setVisibility(View.VISIBLE);
SharedPreferences settingsPreferences = getApplicationContext().getSharedPreferences("com.example.settings", Context.MODE_PRIVATE);
settingsPreferences.edit().putInt("Mode", AppCompatDelegate.MODE_NIGHT_NO).apply();
listView.refreshDrawableState();
noteAdapter.notifyDataSetChanged();
//listView.setAdapter(noteAdapter);
}
});
我發現了類似的問題:ListView Added Duplicate item in list when screen orientation changes,但不幸的是答案對我沒有幫助 - 我不知道如何實作它們。我是 android 的新手,我將不勝感激一些指導。
我在我的 listView 上嘗試了 refreshDrawableState() 和 setAdapter(noteAdapter) 并在配接器上嘗試了 notifyDataSetChanged() 但它沒有幫助。很可能我錯誤地實作了 getView 或我的串列,但我不知道如何改進它。預先感謝您的所有幫助。
以防萬一 - 所有檔案都在我的 GitHub 存盤庫中: https ://github.com/wmaterkowska/MyNotes_app.git
uj5u.com熱心網友回復:
(我的波蘭語答案已被洗掉,因此將其更改為英語)
第一次啟動活動時會呼叫方法onCreate(),但也會在螢屏配置更改后(以及在某些其他情況下)呼叫 - 例如方向或您的案例主題。不能保證這個方法只會被呼叫一次,所以如果你在這里創建了一個串列,你應該清除它以避免每次呼叫方法時重復內容。
notesToShow.clear(); //Add a line here
for (Note note : allNotes) {
if (note.getFolders().contains(folder) && !note.getFolders().contains("Recycle Bin")) {
notesToShow.add(note);
}
}
希望能幫助到你!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/537082.html
標籤:安卓列表显示安卓适配器
