如何从arraylist加载图像?

时间:2019-06-15 08:30:35

标签: java android android-recyclerview

我无法显示layout.xml来显示所有图像,它只能显示我添加到android:src="blabla.jpg"中的图像,并且所有列表都使用“ blabla.jpg”

我可以很好地显示arraylist中的文本,但是问题只是图像,所以我尝试了这段代码

public RecyclerViewAdapter(Context mContext,ArrayList<Integer> mImageHolder,ArrayList<String> mItemHolder, ArrayList<String> mDescHolder) {
        this.mImageHolder = mImageHolder;
        this.mItemHolder = mItemHolder;
        this.mDescHolder = mDescHolder;
        this.mContext = mContext;
    }
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
    private static final String TAG = "RecyclerViewAdapter";

    private ArrayList<String> mItemHolder = new ArrayList<>();
    private ArrayList<String> mDescHolder = new ArrayList<>();
    private ArrayList<Integer> mImageHolder = new ArrayList<>();
    private Context mContext;

    public RecyclerViewAdapter(Context mContext,ArrayList<Integer> mImageHolder,ArrayList<String> mItemHolder, ArrayList<String> mDescHolder) {
        this.mImageHolder = mImageHolder;
        this.mItemHolder = mItemHolder;
        this.mDescHolder = mDescHolder;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_list_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        Log.d(TAG, "onBindViewHolder: called.");

        holder.itemHolder.setText(mItemHolder.get(position));

        holder.parentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "onClick: clicked on: " + mItemHolder.get(position));

                //ini ke intent
                Intent intent = new Intent(mContext, ListDetail.class);
                Bundle extras = new Bundle();
                extras.putString("EXTRA_ITEM", mItemHolder.get(position));
                extras.putString("EXTRA_DESC", mDescHolder.get(position));
                extras.putInt("EXTRA_IMAGE", mImageHolder.get(position));
                intent.putExtras(extras);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mItemHolder.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView itemHolder;
        ImageView imageHolder;
        RelativeLayout parentLayout;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageHolder = itemView.findViewById(R.id.ImageItem);
            itemHolder = itemView.findViewById(R.id.NameItem);
            parentLayout = itemView.findViewById(R.id.parent_layout);
        }
    }
}

Any help would be appreciated,

2 个答案:

答案 0 :(得分:0)

在您的onBindViewHolder中添加:

 holder.imageHolder.setImageResource(image);

答案 1 :(得分:0)

使用imageView的setImageresource属性,并从onBindViewHolder中该位置的列表中获取图像。

holder.imageHolder.setImageResource(mImageHolder.get(position));