为什么使用 ImageAdapter 无法在 GridView 上显示图像

Why images are not getting shown on GridView using ImageAdapter

我正在尝试使用 ImageAdapter 在 GridView 中的 activity 上显示图像。都是基本功能, 即使我从许多来源匹配代码但我的不是 运行 有什么问题吗? ImageAdapter.java

package com.asad.myflipflopgame;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import java.util.Random;

public class ImageAdapter extends BaseAdapter {

    int imageArray[];
    Context myContext;

    public ImageAdapter(Context context, Boolean shuffle){
        myContext = context;

        imageArray = new int[]{
                R.drawable.placeholder,
                R.drawable.sandle_0,
                R.drawable.sandle_1,
                R.drawable.sandle_2,
                R.drawable.sandle_3,
                R.drawable.sandle_4,
                R.drawable.sandle_5,
                R.drawable.sandle_6,
                R.drawable.sandle_7,
                R.drawable.sandle_8};
        if(shuffle){
            shuffleArray(imageArray);
        }
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return imageArray[position];
    }

    @Override
    public long getItemId(int position) {
        return imageArray[position];
    }

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

            card = new ImageView(myContext);
            card.setImageResource(imageArray[position]);
            card.setLayoutParams(new GridView.LayoutParams(85,85));
            card.setScaleType(ImageView.ScaleType.CENTER_CROP);

        return card;
    }

    private void shuffleArray(int shuffleArray[]) {
        Random random = new Random();
        for(int i=shuffleArray.length-1; i>0; i--){
            int randomNumber = random.nextInt(i+1);
            swap(shuffleArray,randomNumber,i);
        }
    }
    private void swap(int array[],int a, int b){
        int temp = array[a];
        array[a] = array[b];
        array[b] = temp;
    }

}

而 activity 使用 ImageAdapter 是 GameBoard.java

package com.asad.myflipflopgame;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.util.*;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.Random;

public class GameBoard extends AppCompatActivity implements View.OnClickListener {

    private int imagesArray[];
    private int positionArray[];
    private int count = 0;
    private ImageAdapter imageAdapter;
    private GridView gridView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_board);

        gridView = findViewById(R.id.grid_view);
        imageAdapter = new ImageAdapter(this,true);
        gridView.setAdapter(imageAdapter);

       /* for(int i=0 ; i< imageAdapter.getCount(); i++){
            gridView.getAdapter().getView(i,null,gridView).setTag(R.drawable.placeholder);
            ((ImageView)gridView.getAdapter().getView(i,null,gridView)).setImageResource(R.drawable.placeholder);
        }*/

    }
    @Override
    public void onClick(View v) {
        replaceImageOnClick((ImageButton) v);
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            hideSystemUI();
        }
    }
    private void hideSystemUI() {
        // Enables regular immersive mode.
        // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
        // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE
                        // Set the content to appear under the system bars so that the
                        // content doesn't resize when the system bars hide and show.
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        // Hide the nav bar and status bar
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);
    }

    // Shows the system bars by removing all the flags
// except for the ones that make the content appear under the system bars.
    private void showSystemUI() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }

    // We will take two arrays one for images and one for number of positions on Board.
    // This method will take each image number wise and place it at 2 random positions.
    // We will store images in an array number wise eg 9 images in an array of size 9.
    // Then we will run outer loop Images array's length times (i.e 9 times).
    // And inner loop will run 2 times.
    // Inner loop will select a number randomly from number stored in i.e 0 ... 17 .
    // HOW:  It will save numbers from 0 --- 17 in array of size 18 having indices 0 ... 17.
    // And each time we will create a random number withing the range of size  = 18 which will create numbers from 0 to 17.
    // Then we pick the value placed at that randomly generated index.
    // And we will swap the value of that index with the last position of array.
    // And next time we will generate random number within range 0 ... 16 similarly next time 0 ... 15 and so on...

    public void putImagesOnBoard(){
        for(int i =0 ;i<imagesArray.length; i++){
            for(int j =0; j<2; j++){
                int randomPosition = new Random().nextInt(positionArray.length - count);
                String TAG = "HHHHHHHHHHH   ";
                String str = "imageButton_" + Integer.toString(positionArray[randomPosition]);
                Log.v(TAG, str);
                //ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.my_game_board);
                //ImageButton btn = constraintLayout.findViewWithTag(str);
                //btn.setBackgroundResource(imagesArray[i]);

                swap(positionArray, randomPosition, positionArray.length - 1 - count);
                count = count+1;
            }
        }
    }
    public void swap(int array[], int x, int y){
        int temp = array[x];
        array[x]=array[y];
        array[y] = temp;
    }
    public void replaceImageOnClick(ImageButton b){

        Toast.makeText(this, "Pushed", Toast.LENGTH_SHORT).show();
        Drawable drawable= b.getBackground();
//        Drawable backgroundDrawable = b.getBackground();
//        if(backgroundDrawable.getConstantState().equals(R.mipmap.sandle_0)){
//            b.setImageResource(R.mipmap.sandle_0);
//        }
//        else if(backgroundDrawable.getConstantState().equals(R.mipmap.sandle_1))
//        {
//            b.setImageResource(R.mipmap.sandle_1);
//        }
//        else if(backgroundDrawable.getConstantState().equals(R.mipmap.sandle_2))
//        {
//            b.setImageResource(R.mipmap.sandle_2);
//        }
//        else if(backgroundDrawable.getConstantState().equals(R.mipmap.sandle_3))
//        {
//            b.setImageResource(R.mipmap.sandle_3);
//        }
//        else if(backgroundDrawable.getConstantState().equals(R.mipmap.sandle_4))
//        {
//            b.setImageResource(R.mipmap.sandle_4);
//        }
//        else if(backgroundDrawable.getConstantState().equals(R.mipmap.sandle_5))
//        {
//            b.setImageResource(R.mipmap.sandle_5);
//        }
//        else if(backgroundDrawable.getConstantState().equals(R.mipmap.sandle_6))
//        {
//            b.setImageResource(R.mipmap.sandle_6);
//        }
//        else if(backgroundDrawable.getConstantState().equals(R.mipmap.sandle_7))
//        {
//            b.setImageResource(R.mipmap.sandle_7);
//        }
//        else if(backgroundDrawable.getConstantState().equals(R.mipmap.sandle_8))
//        {
//            b.setImageResource(R.mipmap.sandle_8);
//        }
//        else{
//        }
    }

}

布局文件是 activity_game_board.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GameBoard"
    android:id="@+id/my_game_board">
    <TextView
        android:id="@+id/txt_view_time"
        android:layout_margin="4dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Time: "
        android:textSize="20dp"
        android:textColor="#ffffff"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />
    <TextView
        android:id="@+id/txt_view_running_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="00:00:00"
        android:textColor="#ffffff"
        android:layout_toRightOf="@+id/txt_view_time"
        android:layout_alignParentTop="true"
        android:layout_margin="4dp"
        />
    <Button
        android:id="@+id/btn_pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pause"
        android:textSize="20dp"
        android:textColor="#ffffff"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_margin="4dp"/>
    <GridView
        android:id="@+id/grid_view"
        android:layout_width="wrap_content"
        android:numColumns="6"
        android:horizontalSpacing="1dp"
        android:verticalSpacing="1dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_pause"
        android:layout_margin="2dp"/>
</RelativeLayout>

最后AndroidMenifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.asad.myflipflopgame">


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".GameBoard"
            android:screenOrientation="landscape"
            android:configChanges="orientation|keyboardHidden">
        </activity>

        <activity
            android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

getCount() return 为零,尝试将其更改为 return imageArray.length

不应再使用 GridView,改用 RecyclerView