Я использую ListView внутри фрагмента, который загружает заметки из хранилища Firebase и затем отображает их.Однако проблема, с которой я сталкиваюсь, заключается в том, что когда он отображает заметки, это просто пустой макет заметки, но когда я переключаю действия и возвращаюсь, они больше не пусты, они загружаются.Я не уверен, где именно я иду не так, я попытался вызвать onDataSetChange для Adapter Everywhere, также попытался invalidate () и invalidateViews () в ListView, надеясь, что это просто обновит представления, но, к сожалению, это не сработалолибо: / код ниже, спасибо заранее за любую помощь!:)
именно здесь я устанавливаю адаптер в своем фрагменте заметок
mNoteStorage = FirebaseStorage.getInstance().getReference().child("Notes").child(currentUser.getUid());
mNotesDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
my_notes.setAdapter(null);
Iterable<DataSnapshot> all_keys = dataSnapshot.getChildren();
filename_arrayList = new ArrayList<>();
na = null;
for (DataSnapshot each_key : all_keys) {
String each = each_key.getKey();
filename_arrayList.add(each);
}
for (String each_filename : filename_arrayList) {
downloadFile(each_filename);
Note each_note = Utilities.getNoteByName(getContext(), each_filename + Utilities.FileExtention);
each_note_array.add(each_note);
}
if (each_note_array == null || each_note_array.size() == 0) {
Toast.makeText(getActivity().getApplicationContext(), "No Notes!", Toast.LENGTH_LONG).show();
} else {
na = new MyNotesAdapter(getContext(), R.layout.item_note, each_note_array);
my_notes.setAdapter(na);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
, а вот мой адаптер
class MyNotesAdapter extends ArrayAdapter<Note> {
Context ctx;
public MyNotesAdapter(@NonNull Context context, int resource, ArrayList<Note> note) {
super(context, resource, note);
this.ctx = context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//return super.getView(position, convertView, parent);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_note, null);
}
Note note = getItem(position);
if(note != null) {
TextView title = (TextView) convertView.findViewById(R.id.list_note_title);
TextView content = (TextView) convertView.findViewById(R.id.list_note_content);
TextView date = (TextView) convertView.findViewById(R.id.list_note_date);
Typeface music_font = Typeface.createFromAsset(getContext().getAssets(), "fonts/melodymakernotesonly.ttf");
Typeface scribble_card = Typeface.createFromAsset(getContext().getAssets(), "fonts/the unseen.ttf");
if (getThemeCountInt() == 0) {
title.setTypeface(music_font);
} else if (getThemeCountInt() == 1) {
title.setTypeface(scribble_card);
}
content.setTypeface(scribble_card);
title.setText(note.getTitle());
date.setText(note.getDateTimeFormatted(getContext()));
if(note.getContent().length() > 25) {
content.setText(note.getContent().substring(0,25) + "...");
} else {
content.setText(note.getContent());
}
if(note.getContent().length() <= 0) {
content.setText("(Empty Note..)");
} else {
content.setText(note.getContent());
}
if (note.getTitle().length() <= 0) {
title.setText("(Untitled)");
} else {
title.setText(note.getTitle());
}
}
return convertView;
}
private int getThemeCountInt() {
SharedPreferences mSharedPreferences = this.getContext().getSharedPreferences("theme", MODE_PRIVATE);
int selectedTheme = mSharedPreferences.getInt("theme", 0);
return selectedTheme;
}
}
скачать файл
private void downloadFile(final String filenames) {
final String each_filename = filenames + Utilities.FileExtention;
final long file_size = 1024 * 1024;
mNoteStorage.child(each_filename).getBytes(file_size).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
FileOutputStream stream;
FileInputStream streamIn;
try {
File dir = getActivity().getApplication().getFilesDir();
File file = new File(dir, each_filename);
stream = new FileOutputStream(file);
stream.write(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
Utilities.getNoteByName
public static Note getNoteByName(Context context, String filename) {
File file = new File(context.getFilesDir(), filename);
Note note;
if (file.exists()) {
FileInputStream fis;
ObjectInputStream ois;
try {
fis = context.openFileInput(filename);
ois = new ObjectInputStream(fis);
note = (Note) ois.readObject();
fis.close();
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
return note;
}
return null;
}