一尘不染

如何在MovieAdapter的Viewholder中加载图像?

json

主要代号

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;
        }

        }

//我可以成功显示除图片以外的所有数据

//图片链接带有“ posterpath”键

//告诉我如何在MovieAdapter的Viewholder中加载图像

// currentmovie只是一个getter和setter类

3.当前电影

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;
    }
}

阅读 183

收藏
2020-07-27

共1个答案

一尘不染

我正在使用库并通过以下方式加载图像

首先通过创建显示选项对象

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

然后通过以下方式初始化图像加载器

  ImageLoader imageLoader = ImageLoader.getInstance();

并通过加载图像

 imageLoader.displayImage(url, imageView, builder);

希望这会有所帮助..也将此添加到您的gradle

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

首先参考这个

编辑: 添加到onCreate()活动

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

或这个

   ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(Activity.this‌​));
2020-07-27