Shiyan12

GridAdapter.java

Aug 26th, 2021
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.25 KB | None | 0 0
  1. package com.example.youdothemath;
  2.  
  3. import android.content.Context;
  4. import android.content.res.Resources;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.ImageView;
  9.  
  10. import java.util.ArrayList;
  11. import java.util.Collections;
  12.  
  13. public class GridAdapter extends BaseAdapter {
  14.     private Context mContext;
  15.     private Integer mCols, mRows;
  16.  
  17.     private ArrayList<String> arrPict; // массив картинок
  18.     private String PictureCollection; // префикс набора картинок
  19.     private Resources mRes; // ресурсы приложения
  20.  
  21.     private static final int CELL_CLOSE = 0;
  22.     private static final int CELL_OPEN = 1;
  23.     private static final int CELL_DELETE = -1;
  24.  
  25.     private static enum Status {CELL_CLOSE, CELL_OPEN, CELL_DELETE};
  26.  
  27.     private ArrayList<Status> arrStatus; // состояние ячеек
  28.  
  29.     public GridAdapter(Context context, int cols, int rows) {
  30.         mContext = context;
  31.         mCols = cols;
  32.         mRows = rows;
  33.  
  34.         arrPict = new ArrayList<>();
  35.         // определяем префикс
  36.         PictureCollection = "flags";
  37.         // получаем все ресурсы приожение
  38.         mRes = mContext.getResources();
  39.         // метод, заполняющий массив картинок
  40.         makePictArray();
  41.         // метод, устанавливающий всем ячейкам статус CELL_CLOSE
  42.         closeAllCells();
  43.     }
  44.  
  45.     public void checkOpenCells() {
  46.         int first = arrStatus.indexOf(Status.CELL_OPEN);
  47.         int second = arrStatus.lastIndexOf(Status.CELL_OPEN);
  48.         if (first == second)
  49.             return;
  50.         if (arrPict.get(first).equals(arrPict.get(second))) {
  51.             arrStatus.set(first, Status.CELL_DELETE);
  52.             arrStatus.set(second, Status.CELL_DELETE);
  53.         }
  54.         else
  55.         {
  56.             arrStatus.set(first, Status.CELL_CLOSE);
  57.             arrStatus.set(second, Status.CELL_CLOSE);
  58.         }
  59.         return;
  60.     }
  61.  
  62.     public void openCell(int position) {
  63.         if (arrStatus.get(position) != Status.CELL_DELETE)
  64.             arrStatus.set(position, Status.CELL_OPEN);
  65.         notifyDataSetChanged();
  66.         return;
  67.     }
  68.  
  69.     public boolean checkGameOver() {
  70.         if (arrStatus.indexOf(Status.CELL_CLOSE) < 0)
  71.             return true;
  72.         return false;
  73.     }
  74.  
  75.     private void closeAllCells() {
  76.         arrStatus.clear();
  77.         for (int i = 0; i < mCols*mRows; i++)
  78.             arrStatus.add(Status.CELL_CLOSE);
  79.     }
  80.  
  81.     private void makePictArray() {
  82.         // очищаем массив
  83.         arrPict.clear();
  84.         // добавляем
  85.         for (int i = 1; i <= ((mCols * mRows) / 2); i++)
  86.         {
  87.             arrPict.add(PictureCollection + Integer.toString(i));
  88.             arrPict.add(PictureCollection + Integer.toString(i));
  89.         }
  90.         // перемешиваем
  91.         Collections.shuffle(arrPict);
  92.     }
  93.  
  94.     @Override
  95.     public int getCount() {
  96.         return mCols * mRows;
  97.     }
  98.  
  99.     @Override
  100.     public Object getItem(int position) {
  101.         return null;
  102.     }
  103.  
  104.     @Override
  105.     public long getItemId(int position) {
  106.         return 0;
  107.     }
  108.  
  109.     @Override
  110.     public View getView(int position, View convertView, ViewGroup parent) {
  111.         ImageView view; // для вывода картинки
  112.         if (convertView == null)
  113.             view = new ImageView(mContext);
  114.         else
  115.             view = (ImageView)convertView;
  116.         switch (arrStatus.get(position)) {
  117.             case CELL_OPEN:
  118.                 // получаем идентификатор ресурса для картинки,
  119.                 // которая находится в массиве на позиции position
  120.                 Integer drawableId = mRes.getIdentifier(arrPict.get(position), "drawable", mContext.getPackageName());
  121.                 view.setImageResource(drawableId);
  122.                 break;
  123.             case CELL_CLOSE:
  124.                 view.setImageResource(R.drawable.planet3);
  125.                 break;
  126.             default:
  127.                 //view.setImageResource(R.drawable.none);
  128.         }
  129.         return view;
  130.     }
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment