在Android Studio中,GridView上的第一个方格是空白的吗?

huangapple 未分类评论47阅读模式
英文:

First square on GridView is blank in AndroidStudio?

问题

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".StartGameActivity">

    <Button
        android:id="@+id/resignButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:text="@string/resign"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/drawButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:text="@string/draw"
        app:layout_constraintBottom_toTopOf="@+id/gridViewBoard"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/gameText"
        android:layout_width="197dp"
        android:layout_height="36dp"
        android:layout_marginStart="18dp"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="16dp"
        android:text="@string/game_text"
        android:textAlignment="center"
        android:textColor="#000000"
        android:textSize="25sp"
        app:layout_constraintEnd_toStartOf="@+id/resignButton"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@+id/drawButton"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/aiButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ai"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/undoButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:text="@string/undo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <GridView
        android:id="@+id/gridViewBoard"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginBottom="49dp"
        android:columnWidth="60dp"
        android:gravity="center_horizontal"
        android:horizontalSpacing="0dp"
        android:numColumns="8"
        android:padding="0dp"
        android:stretchMode="columnWidth"
        android:verticalSpacing="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/drawButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.chess;

import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.*;

public class RecordedGamesActivity extends AppCompatActivity implements OnItemClickListener
{

    private Chess chess;
    private static boolean RUN_ONCE = false;
    private SquareAdapter adapter;
    private GridView board;
    private ArrayList<Move> moves;
    private ListIterator<Move> listIterator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recorded_games);

        if (!RUN_ONCE) {

            RUN_ONCE = true;
            this.chess = new Chess();
            moves = Games.gamesPlayed.get(Games.index);
            listIterator = moves.listIterator();
            adapter = new SquareAdapter(this, chess.getBoard());

        }

        final GridView boardView = (GridView)findViewById(R.id.gridViewBoard);
        boardView.setAdapter(adapter);
        this.board = boardView;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        if (!listIterator.hasNext())
        {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Replay Complete");
            builder.setMessage("Quit replay?");

            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    startActivity(new Intent(RecordedGamesActivity.this, MainActivity.class));
                    RUN_ONCE = false;
                    finish();
                }
            });
        }
        Move move = listIterator.next();
        chess.move(move.getStart(), move.getEnd());
        adapter.notifyDataSetChanged();
        board.setAdapter(adapter);
    }

    @Override
    public void onBackPressed() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Exit");
        builder.setMessage("Quit replay?");

        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                startActivity(new Intent(RecordedGamesActivity.this, MainActivity.class));
                RUN_ONCE = false;
                finish();
            }
        });

        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
}
英文:

So I tried to make an app that plays Chess. However, for some reason, the first box on my gridview for my chessboard is blank (where the black Rook is) when the game starts. But once I make the first move, it appears with the black rook. Has anyone had an issue with GridView in Android Studio like this before? I will attach pictures below as well as my xml code for the chessboard screen and my code for the GridView.

[GridView Before First Move][1]
[GridView After First Move][2]
[1]: https://i.stack.imgur.com/tcKZc.png
[2]: https://i.stack.imgur.com/Tyyun.png

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.StartGameActivity&quot;&gt;


    &lt;Button
        android:id=&quot;@+id/resignButton&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginEnd=&quot;4dp&quot;
        android:text=&quot;@string/resign&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

    &lt;Button
        android:id=&quot;@+id/drawButton&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginBottom=&quot;1dp&quot;
        android:text=&quot;@string/draw&quot;
        app:layout_constraintBottom_toTopOf=&quot;@+id/gridViewBoard&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/gameText&quot;
        android:layout_width=&quot;197dp&quot;
        android:layout_height=&quot;36dp&quot;
        android:layout_marginStart=&quot;18dp&quot;
        android:layout_marginTop=&quot;12dp&quot;
        android:layout_marginEnd=&quot;16dp&quot;
        android:text=&quot;@string/game_text&quot;
        android:textAlignment=&quot;center&quot;
        android:textColor=&quot;#000000&quot;
        android:textSize=&quot;25sp&quot;
        app:layout_constraintEnd_toStartOf=&quot;@+id/resignButton&quot;
        app:layout_constraintHorizontal_bias=&quot;1.0&quot;
        app:layout_constraintStart_toEndOf=&quot;@+id/drawButton&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

    &lt;Button
        android:id=&quot;@+id/aiButton&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;@string/ai&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;

    &lt;Button
        android:id=&quot;@+id/undoButton&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginEnd=&quot;4dp&quot;
        android:text=&quot;@string/undo&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot; /&gt;

    &lt;GridView
        android:id=&quot;@+id/gridViewBoard&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;0dp&quot;
        android:layout_marginStart=&quot;1dp&quot;
        android:layout_marginEnd=&quot;1dp&quot;
        android:layout_marginBottom=&quot;49dp&quot;
        android:columnWidth=&quot;60dp&quot;
        android:gravity=&quot;center_horizontal&quot;
        android:horizontalSpacing=&quot;0dp&quot;
        android:numColumns=&quot;8&quot;
        android:padding=&quot;0dp&quot;
        android:stretchMode=&quot;columnWidth&quot;
        android:verticalSpacing=&quot;0dp&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toBottomOf=&quot;@+id/drawButton&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

And here's the code that creates the Board

package com.example.chess;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;
import com.example.chess.Model.Chess;
import com.example.chess.Model.Piece;
import com.example.chess.Model.Games;
import com.example.chess.Model.Move;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
//import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;
import java.util.*;


public class RecordedGamesActivity extends AppCompatActivity implements OnItemClickListener
{

    private Chess chess;
    private static boolean RUN_ONCE = false;
    private SquareAdapter adapter;
    private GridView board;
    private ArrayList&lt;Move&gt; moves;
    private ListIterator&lt;Move&gt; listIterator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recorded_games);

        if (!RUN_ONCE) {

            RUN_ONCE = true;
            this.chess = new Chess();
            moves = Games.gamesPlayed.get(Games.index);
            listIterator = moves.listIterator();
            adapter = new SquareAdapter(this, chess.getBoard());

        }

        final GridView boardView = (GridView)findViewById(R.id.gridViewBoard);
        boardView.setAdapter(adapter);
        this.board = boardView;
    }

    @Override
    public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id)
    {
        if (!listIterator.hasNext())
        {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(&quot;Replay Complete&quot;);
            builder.setMessage(&quot;Quit replay?&quot;);

            builder.setPositiveButton(&quot;Yes&quot;, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    startActivity(new Intent(RecordedGamesActivity.this, MainActivity.class));
                    RUN_ONCE = false;
                    finish();
                }
            });
        }
        Move move = listIterator.next();
        chess.move(move.getStart(), move.getEnd());
        adapter.notifyDataSetChanged();
        board.setAdapter(adapter);
    }

    @Override
    public void onBackPressed() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(&quot;Exit&quot;);
        builder.setMessage(&quot;Quit replay?&quot;);

        builder.setPositiveButton(&quot;Yes&quot;, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                startActivity(new Intent(RecordedGamesActivity.this, MainActivity.class));
                RUN_ONCE = false;
                finish();
            }
        });

        builder.setNegativeButton(&quot;No&quot;, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
}

huangapple
  • 本文由 发表于 2020年5月5日 07:50:14
  • 转载请务必保留本文链接:https://java.coder-hub.com/61603530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定