Good day!

If you decide to read this - sorry for the unpleasant code.

The problem is this: Everything seems to be working, only Picasso does not display pictures. The site is parsed, links are reached, but not displayed, and a very strange point: if you put stops on a piece of code with Picasso in the debug, then they are displayed, but if you just start it, it does not.

public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; LinearLayoutManager verticalLinearLayoutManager; RecyclerAdapter adapter; public Elements title; public ArrayList<String> titleList = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); NewThread nt = new NewThread(); nt.execute(); recyclerView = (RecyclerView) findViewById(R.id.recycler); verticalLinearLayoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(verticalLinearLayoutManager); adapter = new RecyclerAdapter(); recyclerView.setAdapter(adapter); adapter.addAll(titleList); } private class RecyclerAdapter extends RecyclerView.Adapter<RecyclerViewHolder>{ ArrayList<String> linkList = new ArrayList<String>(); public void addAll(ArrayList<String> titleList) { int pos = 0; this.linkList.addAll(titleList); notifyItemRangeInserted(pos, this.linkList.size()); } @Override public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card, parent, false); return new RecyclerViewHolder(view); } @Override public void onBindViewHolder(RecyclerViewHolder holder, int position) { Picasso.with(MainActivity.this).load(linklist.get(position)).into(holder.image3); } @Override public int getItemCount() { return linkList.size(); } } private class RecyclerViewHolder extends RecyclerView.ViewHolder{ public ImageView image3; public RecyclerViewHolder(View itemView) { super(itemView); image3 = (ImageView) itemView.findViewById(R.id.image2); } public void bind(String s) { } } public class NewThread extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... arg) { Document doc; try { doc = Jsoup.connect("https://habrahabr.ru/").get(); titleList.clear(); Elements link3 = doc.select("img[src$=.jpg]"); for(Element el:link3){ titleList.add(el.absUrl("src")); } } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { } } } 
  • 2
    I want to recommend to use the library Glide github.com/bumptech/glide - analogue of Picasso. With Picasso, there were some incomprehensible problems that Glid managed to get around. The interface is very similar to Picasso, it does not cause problems to go. One more thing I noticed, you wrote a cycle in onBindViewHolder, which is not true, since onBindViewHolder is called for each view separately, and you get to assign the same view to the same view always the last picture. Get the link: String link = linkList.get (position) and pass it to Picasso or Glide. - Oleksandr Karpovich

1 answer 1

You fill your list in another thread. The call to recyclerView.setAdapter () occurs at the moment when NewThread is not yet completed.

 @Override protected void onPostExecute(String result) { adapter.addAll(titleList); adapter.notifyDataSetChanged(); } 

onPostExecute will be called as soon as the data is loaded, so it will be ready for display. In debug you did it, because the data had time to load before assigning the adapter.

 NewThread nt = new NewThread(); nt.execute(); // выполняется в другом потоке, соответсвенно нельзя ждать что после этого вызова данные для отображения будут готовы 
  • But it is impossible to work with the network in the main stream - there will be an error. What to do then? - Yegor Prokofiev
  • That's right, get the data as you are getting now, just add the string adapter.notifyDataSetChanged () to onPostExecute; It will indicate to the adapter that changes have been made to the data source. onPostExecute is always executed in the main thread (UI) .... Added a minor edit to the response. - Oleksandr Karpovich
  • Yes thank you! This is exactly what you need! - Yegor Prokofiev