For some unknown reason, I can't add an item to RecyclerView.

I have an onActivityResult method and in it I do the following things:

 file.add(new FileModel()); 

In turn, somewhere higher in the OnStart method:

 file = new ArrayList<>(); fAdapter = new ADDFileAdapter(file); rvAddFile.setAdapter(fAdapter); 

If I fill in the file in the OnStart method, the elements are added and displayed.

  rvAddFile.setAdapter(fAdapter); file.add(new FileModel()); file.add(new FileModel()); file.add(new FileModel()); 

The question is why in the onActivityResul t method the elements are not added to RecyclerView?

UPD

 public class ADDFileAdapter extends RecyclerView.Adapter<ADDFileAdapter.ADDFileViewHolder> { ArrayList<FileModel> fileConteiner; public ADDFileAdapter(ArrayList<FileModel> fileConteiner) { this.fileConteiner = fileConteiner; } @Override public ADDFileViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.add_file_card_layout, parent, false); ADDFileAdapter.ADDFileViewHolder nh = new ADDFileAdapter.ADDFileViewHolder(v); return nh; } @Override public void onBindViewHolder(ADDFileViewHolder holder, int position) { Context ctx = holder.itemView.getContext(); LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); LinearLayout.LayoutParams lparamsFile = new LinearLayout.LayoutParams(100,100); LinearLayout linearLayoutFileContent = new LinearLayout(ctx); //Контейнер для вложения linearLayoutFileContent.setLayoutParams(lparams); for (int i = 0; i < fileConteiner.size(); i++){ ImageView file = new ImageView(ctx); //Изображение вложения file.setLayoutParams(lparamsFile); file.setImageDrawable(ctx.getResources().getDrawable(R.drawable.audio)); linearLayoutFileContent.addView(file); } holder.cv.addView(linearLayoutFileContent); } @Override public int getItemCount() { return fileConteiner.size(); } public static class ADDFileViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ CardView cv; public ADDFileViewHolder(View itemView) { super(itemView); cv = (CardView) itemView.findViewById(R.id.add_file_card); } @Override public void onClick(View view) { } } 

onActivityResult method

 if(u.toString().equals("image/jpeg") || u.toString().equals("image/png") || u.toString().equals("image/gif") || u.toString().equals("image/tiff")){ fileAdd.add(new FileModel()); fAdapter.notifyDataSetChanged(); } 

Debugger

enter image description here

  • Are you making a call to your adapter.notifyDataSetChanged? - plesser
  • Yes, alas, it does not help in this situation - Heaven
  • Then try to override in the adapter the array of values ​​that should be displayed. If it doesn’t help then .... are you sure that onActivityResult is called for you? - plesser
  • Yes, it is. Added adapter itself higher - Heaven
  • call notifyDataSetChanged mADDFileAdapter.fileConteiner = List <New List> - plesser

2 answers 2

in onActivityResult() after adding data:

 file.add(new FileModel()); 

Notify the adapter about changes by calling notifyDataSetChanged() :

 fAdapter.notifyDataSetChanged() 

If this does not work, then either:

  • link to the adapter is no longer the current adapter;
  • The adapter refers to another collection, not the one to which you added data.

UPD:

file ! = fileAdd

those. see item number 2 above

  • In the debugger mode, I checked the collection, it is filled correctly, the link to the adapter is also not lost - Heaven
  • @Heaven add a question with your onActivityResult() method - ermak0ff
  • Added part of the method that is responsible for adding items. The condition passes normally. He even begged the Adapter, alas, it does not help - Heaven
  • @Heaven updated the answer - ermak0ff
  • Added a screen from the debugger, the Adapter comes filleAdd which I filled in OnActivityResult - Heaven

onCreate data initialization to onCreate method. Apparently when onActivityResult was called, the link to the fragment was lost, although it was not completely destroyed but went to the onStop stage onStop

onStart

 public void onStart() { setHasOptionsMenu(true); rvAddFile = (RecyclerView) getActivity().findViewById(R.id.file_content); fLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false); rvAddFile.setLayoutManager(fLayoutManager); fAdapter = new ADDFileAdapter(fileAdd); } 

onCreate

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); fileAdd = new ArrayList<>(); } 

and actually OnActivityResult itself

 fileAdd.add(new FileModel()); rvAddFile.setAdapter(fAdapter); fAdapter.notifyDataSetChanged();