Parsing a JSON string with many elements. The text of each item is successfully added to the ListView .

It also contains links to pictures, for each ListView item has its own picture. The link is contained in the variable small .

How to implement this image loading and insertion in a ListView ? I use SimpleAdapter .

    3 answers 3

    First, I advise you to use RecycleView - this is a more flexible tool than ListView .
    Regarding images, whether ListView or RecycleView , you need in the adapter when creating a component, I upload Picasso image to the ImageView using the library, it performs this task very quickly, and at the same time has automatic caching.

      static class ViewHolder { public ImageView imageView; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; //...... holder = new ViewHolder(); holder.imageView = (ImageView) convertView.findViewById(R.id.icon); //...... Picasso.with("Your context").load("Your URL").into(holder.imageView); } 
    • java.lang.IllegalArgumentException: Target must not be null Swears on the line with Picasso - SerGon146
    • Check if you initialize the ImageView correctly. And at the stage of calling the Picasso method, is the ImageView initialized, or is it null . Also, the context should be taken via the getContext() method, not this.context . - Vyacheslav Martynenko
    • ImageView img = (ImageView) findViewById(R.id.ImgSmall); Picasso.with(getApplicationContext()).load(small).into(img); - SerGon146
    • One of the parameters that you pass to the method is null , look through debug, which. It causes an error in the method. - Vyacheslav Martynenko
    • img gives null how to fix it? - SerGon146 pm

    Connect to the project any library for downloading images, for example Fresco . For your SimpleAdapter, you set your binder view using the setViewBinder method in which you start downloading and displaying a picture for a view with a picture using the above mentioned one.

    The link as well as the text is thrown into the adapter, so to say "opposite" of the Yiddish view with a picture, respectively, in the binder you check that the view came with the ImageView id (or the widget from the library if there is your ImageView) and, if so, start downloading the picture if not, return false so that the adapter fills the other views with the same data as before.

    • Can you give more details? JSON is parsed in my loop, at each iteration I get text and a link. I drop the text in the adapter. What to do with the link? - SerGon146
    • Added in response to the details. - xkor

    It is enough to write your own CusromListAdapter , which expands from ArrayAdapter .

    An article on this topic