Ad

How Can I Do To Insert Bitmaps Into Gridview?

I am creating a Puzzle game, and I want to have a GridView that contains images of my puzzle pieces.

I looked a lot of solutions, but none of them are working in my case. I can't see my gridView.

I'm using an ImageAdapter class to put my puzzle piece Bitmaps in my GridView, and I have an ArrayList which contains all my Bitmaps.

In my main class, I have this

GridView gridView = findViewById(R.id.gridPiece);
                gridView.setAdapter(new GridViewAdapter(PuzzleActivity.this, R.layout.row_grid, images));
                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        System.out.println("WORKING");
                    }
                });

I have my GridViewAdapter class

@SuppressWarnings("ALL")
public class GridViewAdapter extends ArrayAdapter<Item> {

    private Context mContext;
    private int layoutResourceId;
    private ArrayList<Item> images;

    public GridViewAdapter(Context c, int layoutResourceId, ArrayList<Item> images) {
        super(c, layoutResourceId, images);
        mContext = c;
        this.layoutResourceId = layoutResourceId;
        this.images = images;
    }
    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Item getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        RecordHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new RecordHolder();
            holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
            row.setTag(holder);
        } else {
            holder = (RecordHolder) row.getTag();
        }

        Item item = images.get(position);
        holder.imageItem.setImageBitmap(item.getImage());
        return row;

    }

    static class RecordHolder {
        TextView imageTitle;
        ImageView imageItem;
    }
}

My gridView

<GridView
        android:id="@+id/gridPiece"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:columnWidth="100dp"
        android:drawSelectorOnTop="true"
        android:horizontalSpacing="10dp"
        android:numColumns="1"
        android:paddingHorizontal="10dp"
        android:paddingVertical="10dp"
        android:stretchMode="columnWidth"
        android:verticalSpacing="5dp"
        tools:ignore="MissingConstraints,UnusedAttribute"
        tools:layout_editor_absoluteY="0dp" />

and each image in my gridView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/item_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginRight="10dp"
        tools:ignore="ContentDescription,RtlHardcoded">
    </ImageView>

</LinearLayout>

If someone could help me, it would be very grateful, Thank you

Ad

Answer

Instead of returning zero for getCount you should return count of your images like this:

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

and in getItem() function you should return the item instead of null:

@Override
public Item getItem(int position) {
    return this.images.get(position);
}
Ad
source: stackoverflow.com
Ad