How to load image in Viewholder of MovieAdapter?

淺唱寂寞╮ 提交于 2020-01-20 08:34:08

问题


Main Code

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.movie);
        lv =(ListView)findViewById(R.id.lstMovieData);
          moviename.clear();


      //  tv = (TextView) findViewById(R.id.tv);


        Bundle b = getIntent().getExtras();
        try {
            Title = b.getString("MOVIE");
            t = replace(Title);

        } catch (Exception e) {

        }

        String API = "https://api.cinemalytics.com/v1/movie/title/?value=" + t + "&auth_token=<token>";
        Toast.makeText(getApplicationContext(), Title, Toast.LENGTH_LONG).show();
        OkHttpClient Client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(API).build();
        Call call = Client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(final Response response) throws IOException {
                try {


                    String json = response.body().string();
                    Log.v(TAG, json);
                    if (response.isSuccessful()) {

                        getDATA(json);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {


                                mAdapter = new MovieAdapter(getApplicationContext(),moviename);
                                lv.setAdapter(mAdapter);
                            }

                        });

                    } else {

                    }
                } catch (Exception e) {

                }


            }
        });



    }



      public String replace(String str) {
        return str.replaceAll(" ", "%20");
    }

    private void getDATA(String json) throws JSONException {
           try {

    moviename = new ArrayList<>();
    Currentmovie c = new Currentmovie();
    String story = "About The Story";

    JSONArray values = new JSONArray(json);
    for(int i = 0; i < values.length(); i++) {
        JSONObject jsonObject = values.getJSONObject(i);

        String movieTitle = jsonObject.getString("Title");
        String disc = jsonObject.getString("Description");
        Log.e(TAG,"GIRISH"+movieTitle);
        c= new Currentmovie();
        c.setTitle("Movie Name::"+movieTitle);
        c.setDesc(story+"::\n"+disc);
        if(jsonObject.getString("Description")==null)
        {

            c.setDesc(story+"::Not Available");
        }
        moviename.add(c);

                 } 
                 }
                 catch (Exception e)
                {
           System.out.println("Error in Result as " + e.toString());
            }

               }

2.MovieAdapter.java

           public class MovieAdapter extends BaseAdapter {

        Context context;
        private List<Currentmovie> movieData;
        private static LayoutInflater inflater = null;


        public MovieAdapter( Context context,List<Currentmovie> movieData)
        {
            this.context = context;
            this.movieData = movieData;
           inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return movieData.size();
        }

        @Override
        public Object getItem(int position) {
            return movieData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        public static class ViewHolder{
            public TextView movieTitle,movieDesc;
            public ImageView movieImage;


        }

        public View getView(int position, View convertView, ViewGroup parent)
        {


            View vi = convertView;
            ViewHolder holder;

            if(convertView==null){
                vi = inflater.inflate(R.layout.row, null);

                holder = new ViewHolder();
                holder.movieTitle = (TextView) vi.findViewById(R.id.tv);
                holder.movieDesc=(TextView)vi.findViewById(R.id.tv1);


             vi.setTag( holder );
            }
            else
                holder=(ViewHolder)vi.getTag();



            holder.movieTitle.setText(movieData.get(position).getTitle());
            holder.movieDesc.setText(movieData.get(position).getDesc());



            return vi;
        }

        }

//i can successfully show all data except image

//image link comes with "posterpath" key

//tell me how to load image in viewholder of MovieAdapter

//currentmovie is just a getter and setter class

3.Currentmovie.java

public class Currentmovie {

     private String mTitle;
   private String Description;


    public String getDesc() {
        return Description;
    }

   public void setDesc(String desc) {
       Description = desc;
    }
    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        mTitle = title;
    }
}

回答1:


I am using this library and loading images by

first creating the Display options object by

    DisplayImageOptions builder = new DisplayImageOptions.Builder()
                                .cacheOnDisk(true)
                                .showImageOnLoading(R.drawable.empty_photo)        
                                .showImageForEmptyUri(R.drawable.empty_photo)
                                .build();

Then initialze the image loader by

  ImageLoader imageLoader = ImageLoader.getInstance();

and load images by

 imageLoader.displayImage(url, imageView, builder);

Hope this helps.. also add this to you gradle

  compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

Refer this first

EDIT: Add this to onCreate() of activity

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
        ...
        .build();
    ImageLoader.getInstance().init(config);

or this

   ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(Activity.this‌​));



回答2:


Add getter and setter methods for your Image like

public String getImageUrl() {
        return imageUrl;
    }

   public void setImageUrl(String imgUrl) {
       imageUrl= imgUrl;
    }

Add this code in your adapter class after adding Picasso library to your project:

String imageUrl = movieData.get(position).getImageUrl();

Picasso.with(getContext())
    .load(imageUrl)
    .into(holder.movieImage, new com.squareup.picasso.Callback() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onError() {

                        }
                    });


来源:https://stackoverflow.com/questions/38005918/how-to-load-image-in-viewholder-of-movieadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!