Shiyan12

GridAdapter.java

Sep 2nd, 2021
873
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.39 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 boolean openCell(int position) {
  63.         if (arrStatus.get(position) == Status.CELL_DELETE || arrStatus.get(position) != Status.CELL_OPEN)
  64.             return false;
  65.         if (arrStatus.get(position) != Status.CELL_DELETE)
  66.             arrStatus.set(position, Status.CELL_OPEN);
  67.         notifyDataSetChanged();
  68.         return true;
  69.     }
  70.  
  71.     public boolean checkGameOver() {
  72.         if (arrStatus.indexOf(Status.CELL_CLOSE) < 0)
  73.             return true;
  74.         return false;
  75.     }
  76.  
  77.     private void closeAllCells() {
  78.         arrStatus.clear();
  79.         for (int i = 0; i < mCols*mRows; i++)
  80.             arrStatus.add(Status.CELL_CLOSE);
  81.     }
  82.  
  83.     private void makePictArray() {
  84.         // очищаем массив
  85.         arrPict.clear();
  86.         // добавляем
  87.         for (int i = 1; i <= ((mCols * mRows) / 2); i++)
  88.         {
  89.             arrPict.add(PictureCollection + Integer.toString(i));
  90.             arrPict.add(PictureCollection + Integer.toString(i));
  91.         }
  92.         // перемешиваем
  93.         Collections.shuffle(arrPict);
  94.     }
  95.  
  96.     @Override
  97.     public int getCount() {
  98.         return mCols * mRows;
  99.     }
  100.  
  101.     @Override
  102.     public Object getItem(int position) {
  103.         return null;
  104.     }
  105.  
  106.     @Override
  107.     public long getItemId(int position) {
  108.         return 0;
  109.     }
  110.  
  111.     @Override
  112.     public View getView(int position, View convertView, ViewGroup parent) {
  113.         ImageView view; // для вывода картинки
  114.         if (convertView == null)
  115.             view = new ImageView(mContext);
  116.         else
  117.             view = (ImageView)convertView;
  118.         switch (arrStatus.get(position)) {
  119.             case CELL_OPEN:
  120.                 // получаем идентификатор ресурса для картинки,
  121.                 // которая находится в массиве на позиции position
  122.                 Integer drawableId = mRes.getIdentifier(arrPict.get(position), "drawable", mContext.getPackageName());
  123.                 view.setImageResource(drawableId);
  124.                 break;
  125.             case CELL_CLOSE:
  126.                 view.setImageResource(R.drawable.planet3);
  127.                 break;
  128.             default:
  129.                 //view.setImageResource(R.drawable.none);
  130.         }
  131.         return view;
  132.     }
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment