如何使用setImageResource通过ViewHolder设置图像

时间:2018-07-27 16:40:32

标签: java arraylist android-viewholder android-imagebutton

有人知道为什么setImageResource无法从我的getImage方法获取图像吗?

它给了我一个错误消息,提示setImageResource无法应用于(jave.lang.string)。

我尝试将movieImage值更改为Int,但随后又给了我另一个错误列表。我应该坚持使用setImageResource还是以其他方式从数组中设置列表视图中的图像?

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

    setupImageLoader();

    //get the movie information
    String movieName = getItem(position).getTitle();
    String movieDuration = getItem(position).getMovieLength();
    String movieRating = getItem(position).getMovieRating();
    String movieImage = getItem(position).getCoverArt();



    //Create the movie object with the information
    objectMovieListView objectMovieListView = new objectMovieListView(movieName, movieDuration, movieRating, movieImage);

    //create the view result for showing the animation
    final View result;

    //ViewHolder object
    ViewHolder holder;

    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(mResource, parent, false);
        holder = new ViewHolder();
        holder.movieName = (TextView) convertView.findViewById(R.id.textViewMovieName);
        holder.movieDuration = (TextView) convertView.findViewById(R.id.textViewMovieDuration);
        holder.movieRating = (TextView) convertView.findViewById(R.id.textViewMovieRating);
        holder.movieImage = (ImageButton) convertView.findViewById(R.id.movieListImageButton);

        result = convertView;
        convertView.setTag(holder);

    }
    else {
        holder = (ViewHolder) convertView.getTag();
        result = convertView;
    }

    ImageLoader imageLoader = ImageLoader.getInstance();

    int defaultImage = mContext.getResources().getIdentifier("@drawable/icons8_image_moviepicture", null,mContext.getPackageName());

    DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
            .cacheOnDisc(true).resetViewBeforeLoading(true)
            .showImageForEmptyUri(defaultImage)
            .showImageOnFail(defaultImage)
            .showImageOnLoading(defaultImage).build();
    imageLoader.displayImage(movieImage, holder.movieImage, options);


    holder.movieName.setText(objectMovieListView.getMovieName());
    holder.movieDuration.setText(objectMovieListView.getMovieDuration());
    holder.movieRating.setText(objectMovieListView.getMovieRating());
    //holder.movieImage.setImageResource(R.drawable.avengers_infinity_war_poster);
    holder.movieImage.setImageResource(objectMovieListView.getMovieImage());

    return convertView;
}

这是我存储所有get和set方法的Class

    public class objectMovieListView {

private String movieImage;


public objectMovieListView(String movieImage) {

    this.movieImage = movieImage;

public String getMovieImage() {
    return movieImage;
}

public void setMovieImage(String movieImage) {
    this.movieImage = movieImage;
}

}

0 个答案:

没有答案