0

I am sure this is not a new question. Unfortunately, after fighting for this activity over 20 hours, I'm still cannot find out the solution. Could you please give me a helping hand. I will be very appreciate.

Hey is my question. I would like to show Image from different urls to a girdView layout. However, my code didn't work so the screen show nothing. Could you guy please help me to solve this?

Below is my MainActivity Class

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String [] imgUrlList = {
            "http://image.tmdb.org/t/p/w500/cGOPbv9wA5gEejkUN892JrveARt.jpg",
            "http://image.tmdb.org/t/p/w500/6FxOPJ9Ysilpq0IgkrMJ7PubFhq.jpg",
            "http://image.tmdb.org/t/p/w500/z09QAf8WbZncbitewNk6lKYMZsh.jpg",
            "http://image.tmdb.org/t/p/w500/kqjL17yufvn9OVLyXYpvtyrFfak.jpg"
    };


    mMovieAdapter = new MyAdapter(this, imgUrlList);
    GridView gridview = (GridView) findViewById(R.id.gridView);
    gridview.setAdapter(mMovieAdapter);

}

public class MyAdapter extends ArrayAdapter<String>{
    private final Activity context;
    private final String[] imgUrlList;

    public MyAdapter (Activity context, String[] imgUrlList){
        super(context, R.layout.image_item);
        this.context = context;
        this.imgUrlList = imgUrlList;
    }

    public View getView (int position, View view, ViewGroup parent){
        LayoutInflater inflater = context.getLayoutInflater();
        View rootView = inflater.inflate(R.layout.image_item, null, true);

        new DownloadImageTask((ImageView) rootView.findViewById(R.id.imageItem)).execute(imgUrlList[position]);
        ImageView imageView = (ImageView) rootView.findViewById(R.id.imageItem);
        imageView.setImageURI(Uri.parse(imgUrlList[position]));

        return rootView;
    }
}

private class DownloadImageTask extends AsyncTask <String, Void, Bitmap>{

    ImageView poster;

    public DownloadImageTask(ImageView poster) {
        this.poster = poster;
    }

    protected Bitmap doInBackground(String... urls){
        String imageUrls = urls[0];
        Bitmap moviePoster = null;
        try {
            InputStream in = new java.net.URL(imageUrls).openStream();
            moviePoster = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return moviePoster;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        poster.setImageBitmap(result);
    }
}

Below is my xml (activity_main) file

<GridView
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnWidth="185dp"
    android:numColumns="auto_fit"/>

I also have a imageView file call image_item

Chow Day
  • 23
  • 2

2 Answers2

0

Honestly, I don't know why. But use this constructor fix this problem:

 super(context,0,imgUrlList);

instead of

super(context, R.layout.image_item);
Phoenix Wang
  • 2,357
  • 1
  • 11
  • 17
  • And frankly I don't really like your code style. Little mistakes everywhere. – Phoenix Wang Jul 13 '16 at 14:17
  • Thanks for your reply. I'm just start to learn coding 10day before. I know that there are lot of thing can be improve. However, I have tried to use the code you provided. But still cannot work in my phone. – Chow Day Jul 14 '16 at 00:48
  • Could you please tell me how to find the little mistakes? – Chow Day Jul 14 '16 at 00:53
-2

You may use picaso library to server your purpose. https://github.com/square/picasso

You just need to initialize Picasso class and set url.

niyasc
  • 4,440
  • 1
  • 23
  • 50
ViratBhavsar
  • 104
  • 4