Search code examples
androidlistviewandroid-arrayadapteruniversal-image-loader

universal image loader not working for Relative layout in ArrayAdapter


i have list of image path's in json response. i prompt to set the images as background of relative layout from the path. so on this i have been advised and used to ArrayAdapter and universal image loader to achieve this.i hope myself 95% i have done this. but the problem is its loaded only one image in my ui which is from two images where i have in json response.

here i tried in ArrayAdapter class inside getView() method

RelativeLayout layContentOfListView;

// Initialize the Relative layout container
        layContentOfListView = (RelativeLayout)rowView.findViewById(R.id.layContentOfListView);

    // Load image, decode it to Bitmap and return Bitmap to callback
        ImageSize targetSize = new ImageSize(150, 100); // result Bitmap will be fit to this size
        imageLoader.loadImage(fullImagePath, targetSize, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                if (loadedImage != null) {

                    Drawable d = new BitmapDrawable(context.getResources(), loadedImage);
                    layContentOfListView.setBackgroundDrawable(d);
                }
            }
        });

this is my relative layout content in the list view of each row.

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/layContentOfListView"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    android:orientation="vertical"
    android:background="@drawable/gray_boardered_transparent_shape"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_red"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:id="@+id/layHeader"
        android:layout_marginBottom="10dp"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lblMerchantName"
            android:layout_weight="1"
            android:text="Merchant Name"
            android:textAlignment="textStart"
            android:textSize="15sp"
            android:textColor="@color/color_white"
            />

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:id="@+id/lblRewardPoints"
        android:text="Reward points"
        android:textSize="15sp"
        android:textColor="@color/color_white"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:id="@+id/lblRewardName"
        android:text="Reward Name"
        android:textSize="15sp"
        android:textColor="@color/color_white"
        android:layout_below="@+id/lblRewardPoints"
        android:layout_centerHorizontal="true"
        />

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/imgMemStar"
        android:src="@drawable/icon_mem_star"
        android:layout_below="@+id/lblRewardName"/>

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginRight="40dp"
        android:id="@+id/imgMemHand"
        android:src="@drawable/icon_mem_like"
        android:layout_below="@+id/lblRewardName"
        android:layout_toLeftOf="@+id/imgMemPhone"
        />

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/imgMemPhone"
        android:src="@drawable/icon_mem_phone"
        android:layout_below="@+id/lblRewardName"
        android:layout_centerHorizontal="true"/>

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginLeft="40dp"
        android:id="@+id/imgMemQRCode"
        android:src="@drawable/icon_mem_qr"
        android:layout_below="@+id/lblRewardName"
        android:layout_toRightOf="@+id/imgMemPhone"
        />

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/imgMemLocation"
        android:src="@drawable/icon_mem_location"
        android:layout_below="@+id/lblRewardName"
        android:layout_alignParentRight="true"/>


</RelativeLayout>

and this is my output screen

enter image description here

i can't found the solution as too long. can anyone please charity on this.Thanks.!


Solution

  • I have tested this and it is working fine.

    import android.graphics.Bitmap;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.RelativeLayout;
    import com.squareup.picasso.Picasso;
    import com.squareup.picasso.Target;
    
    public class MainActivity extends AppCompatActivity {
    RelativeLayout relativeLayout;
    String url = "Image_URL";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        relativeLayout = (RelativeLayout) findViewById(R.id.lay);
    
    
        Picasso.with(this)
                .load(url)
                .into(new Target() {
                    @Override
                    public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){
                /* Save the bitmap or do something with it here */
    
                        //Set it in the ImageView
                        Drawable dr = new BitmapDrawable(bitmap);
                        relativeLayout.setBackground(dr);
                    }
    
                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {
    
                    }
    
                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {
    
                    }
                });
    }
    }
    

    Use this in manifest before Application.

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    

    Add the following line in dependencies or Gradle.

    compile 'com.squareup.picasso:picasso:2.5.2'