In my activity there is a RecyclerView. Adapter code for it:

public class StoryListAdapter extends RecyclerView.Adapter<StoryListAdapter.ViewHolder> { private LayoutInflater inflater; private ArrayList<Story> storyList; private Context context; StoryListAdapter(Context context,ArrayList<Story> storyList){ this.storyList = storyList; this.context = context; this.inflater = LayoutInflater.from(context); } @Override public StoryListAdapter.ViewHolder onCreateViewHolder( ViewGroup viewGroup, int i) { View view = inflater.inflate(R.layout.recyclerview_item, viewGroup, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull StoryListAdapter.ViewHolder viewHolder, int i) { final Story story = storyList.get(i); viewHolder.tv.setText(story.getTitle()); viewHolder.cv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(context,StoryViewActivity.class); intent.putExtra("number", story.getNumber()); context.startActivity(intent); } }); } @Override public int getItemCount() { return storyList.size(); } public class ViewHolder extends RecyclerView.ViewHolder { final TextView tv; final CardView cv; ViewHolder(View view){ super(view); cv = (CardView) view.findViewById(R.id.cv_item); tv = (TextView) view.findViewById(R.id.tv_item); } } } 

Initialization of the adapter and ArrayList for it occur in a separate stream:

 private class ProgressTask extends AsyncTask<Void, Void, Void>{ @Override protected void onPreExecute() { // Начало загрузки pb.setVisibility(View.VISIBLE); // pb - ProgressBar } @Override protected Void doInBackground(Void... voids) { stories = Parser.getStoriesByTag(tag); // stories - ArrayList который хранит в себе объекты класса Story return null; } @Override protected void onPostExecute(Void aVoid) { pb.setVisibility(View.INVISIBLE); adapter = new StoryListAdapter(StoryListActivity.this,stories); // Установка адаптера rv.setAdapter(adapter); } } 

But after the end of the stream, the RecyclerView content does not appear on the user's screen, despite the fact that the stories (ArrayList used in the adapter) are filled with objects. And as far as I understand the problem in the adapter code (but this is not accurate) Tell me what am I doing wrong and how to fix it?

  • rv.setLayoutManager(new LinearLayoutManager(this)); - Try over the implementation of the adapter to set the recycling. - McDaggen
  • @McDaggen, indeed. It helped me, thanks - Felay
  • I brought in the answer, please tick off, so that those who will search for a question on the topic take advantage of the working answer. - McDaggen

1 answer 1

Try to set a recycling before implementing an adapter.

 rv.setLayoutManager(new LinearLayoutManager(this));