1

I am loading Images from web url using asynctask.

And at the same time I am trying to load images into a gallery using custom adapter. But the Gallery is shown with no Images.

activity,

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.property_image_adapter);
    Intent intent = getIntent();
    intent.getStringExtra("PropertyID");
    PropertyImagesList ImageList = new PropertyImagesList();
    img = ImageList
            .fetchPropertyImages(intent.getStringExtra("PropertyID"));
    //imgfromWebUrl=(ImageView)findViewById(R.id.imgfromWebUrl);
     propertyImageGallery = (Gallery) findViewById(R.id.gallery1);
    new LoadViewTask().execute();
}

private class LoadViewTask extends AsyncTask<Void, Integer, Bitmap> {
    // Before running code in separate thread
    @Override
    protected Bitmap doInBackground(Void... params) {
        View view;
        Bitmap x = null;
        for (int i = 0; i <= img.size() - 1; i++) {
            try {
                HttpURLConnection connection = (HttpURLConnection) new URL(
                        "url here"
                                + img.get(i).Source.trim())
                        .openConnection();
                connection.connect();
                connection.setReadTimeout(120000);
                InputStream input = connection.getInputStream();
                x = BitmapFactory.decodeStream(input);


            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return x;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        Log.d("","On Post execute: " + result);
        //imgfromWebUrl.setImageBitmap(result);
        propertyImageGallery.setAdapter(new PropertyImageAdapter(Context, result));
    }
}
}

my custom adapter class,

public class PropertyImageAdapter extends BaseAdapter {
private Context ctx;
Bitmap pics;

public PropertyImageAdapter(Context c,Bitmap pics) {
    Log.d("","custom Adapter");
    ctx = c;        
    this.pics=pics;
}

@Override
public int getCount() {

    return 0;
}

@Override
public Object getItem(int arg0) {

    return arg0;
}

@Override
public long getItemId(int arg0) {

    return arg0;
}

@Override
public View getView(int arg0, View paramView, ViewGroup arg2) {

    View localView;
    Log.d("","getView: ");
    if (paramView == null) {
        localView = View.inflate(ctx,
                R.layout.property_image_adapter, null);
    } else {
        localView = paramView;
    }       
    ImageView imgfromWebUrl=(ImageView)localView.findViewById(R.id.imgfromWebUrl);      
    Log.d("", "pics[0]: "+pics);
    imgfromWebUrl.setImageBitmap(pics);
    imgfromWebUrl.setScaleType(ImageView.ScaleType.FIT_XY);
    imgfromWebUrl.setLayoutParams(new Gallery.LayoutParams(150, 150));
    return imgfromWebUrl;
}

 }

Layout,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<Gallery
    android:id="@+id/gallery1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:spacing="10dip" >
</Gallery>

<ImageView
    android:id="@+id/imgfromWebUrl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</ImageView>

</LinearLayout>

I don't where I have implemented wrong, but gallery is shown empty.

Please correct my code!! Any help is appreciated!!

Mahe
  • 2,707
  • 13
  • 50
  • 69
  • You should reindex the gallery to show up new images. Let me know if this helped you. http://stackoverflow.com/questions/16008419/how-to-force-android-to-re-index-all-the-photos-on-the-phone – Sqeezer Jun 19 '13 at 11:34
  • While you are at it, try [this](http://stackoverflow.com/questions/11778140/using-asynctask-to-load-images-into-a-custom-adapter?rq=1) and [this](http://stackoverflow.com/questions/11645026/a-custom-adapter-that-displays-different-images-from-the-web-in-a-list?rq=1) as well – verybadalloc Jun 19 '13 at 11:38

1 Answers1

0

the getCount() is set to ZERO in your adapter, this means that your adapter will show ZERO "things"

i didnt read the code well, but this is a point that may make difference

Ahmed Adel Ismail
  • 2,168
  • 16
  • 23