Загрузка данных


Конечно. Я внимательно изучил все четыре фотографии и переписал код точь-в-точь, как он указан в методичке.

Обратите внимание: в тексте методички на третьем фото есть опечатка в объявлении массива (написано int[] dice, хотя хранятся ссылки на ресурсы, должно быть int[] для ID ресурсов, но я переписал как на фото). Также в коде MainActivity отсутствует закрывающая фигурная скобка для самого класса (это видно на 4-м фото, где код обрывается). Я добавил её в самом конце, чтобы код компилировался.

1. Файл activity_main.xml (Склеено со 1-го и 2-го фото)

```xml
<?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"
    android:background="@color/white">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="DICE"
        android:textColor="@color/black"
        android:textSize="32sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textColor="@color/black"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="@+id/firstDice"
        app:layout_constraintStart_toStartOf="@+id/firstDice"
        app:layout_constraintTop_toTopOf="@+id/firstDice" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Player B"
        android:textColor="@color/black"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="@+id/secondDice"
        app:layout_constraintStart_toStartOf="@+id/secondDice"
        app:layout_constraintTop_toTopOf="@+id/secondDice" />

    <ImageView
        android:id="@+id/firstDice"
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:padding="10dp"
        android:src="@drawable/dice6"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/secondDice"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/secondDice"
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:padding="10dp"
        android:src="@drawable/dice6"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/firstDice"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/rollButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="48dp"
        android:backgroundTint="@color/black"
        android:text="ROLL"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/firstDice" />

    <TextView
        android:id="@+id/winnerTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Draw"
        android:textColor="@color/black"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/rollButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/firstDice" />

</androidx.constraintlayout.widget.ConstraintLayout>
```

---

2. Файл MainActivity.java (Склеено с 3-го и 4-го фото)

```java
package org.geeksforgeeks.demo;

import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private ImageView firstDice;
    private ImageView secondDice;
    private TextView winnerTextView;
    private Random random = new Random();

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

        // array to store dice images
        int[] dice = {
                R.drawable.dice1, R.drawable.dice2, R.drawable.dice3,
                R.drawable.dice4, R.drawable.dice5, R.drawable.dice6
        };

        // get the references of views
        button = findViewById(R.id.rollButton);
        firstDice = findViewById(R.id.firstDice);
        secondDice = findViewById(R.id.secondDice);
        winnerTextView = findViewById(R.id.winnerTextView);

        // call the on click function
        button.setOnClickListener(v -> {
            // generate two random numbers
            // using Random function
            int num1 = random.nextInt(6);
            int num2 = random.nextInt(6);

            // set the images from the array by the index
            // position of the numbers generated
            firstDice.setImageResource(dice[num1]);
            secondDice.setImageResource(dice[num2]);

            if (num1 > num2) {
                winnerTextView.setText("Player A Wins");
            } else if (num1 < num2) {
                winnerTextView.setText("Player B Wins");
            } else {
                winnerTextView.setText("Draw");
            }
        });
    }
}
```

(Примечание к коду: В самом конце 4-го фото не хватает } для закрытия класса MainActivity. Я её добавил в самом конце кода, чтобы всё было синтаксически верно).