Ad

How To Add Images In Gridview? I Am Getting Grid Data From API But I Want To Add Images To That GridView

- 1 answer

I'm using grid view in my project. I am getting data from API. But I want to add images to that grid. The below given code is adapter class code

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

        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        ViewHolder mHolder = new ViewHolder();

        ImageView imageView;
        if (convertView == null) {



            convertView = inflater.inflate(R.layout.adapter_category, null);
            mHolder.tvCategoryName = (TextView) convertView.findViewById(R.id.tvCategoryName);
            mHolder.tvCategoryDescription = (TextView) convertView.findViewById(R.id.tvCategoryDescription);
            mHolder.ivItemImage = (ImageView) convertView.findViewById(R.id.ivItemImage);

            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();

        }

        mHolder.tvCategoryName.setText(getItem(position).getName());
        mHolder.tvCategoryDescription.setText("");
        mHolder.ivItemImage.setImageResource(mThumbIds[4]);


        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Log.e("THIS IS CATEGORY TYPE", mCategoryType + "");

                if (mCategoryType == ConstantsInternal.categoryType.MainCategory.getValue()) {
                    Log.e("RENT", "MAINCATEGORY");
                    Bundle mBundle = new Bundle();
                    mBundle.putString("itemName", getItem(position).getName());
                    Log.e("MAINCATEGORY", getItem(position).getName());
                    MyActivity.getInstance().subcategory(mContext, mBundle);
                }


            }
        });

        return convertView;

    }

    private Integer[] mThumbIds = {
            R.drawable.camera ,R.drawable.ic_cart, R.drawable.commericalvehicles, R.drawable.garments
    };

My Expected result is I want images in Gridview. Each and every grid should be different image. I am new to this. Thanks in advance.

Ad

Answer

instead of mHolder.ivItemImage.setImageResource(mThumbIds[4]) .

try this

mHolder.ivItemImage.setImageResource(mThumbIds[position])
Ad
source: stackoverflow.com
Ad