java – Problem with a recyclerview adapter in android?

I am working on a game called Space Mines. I am trying to get a list of planets on the screen, but am having trouble and do not know why. I try to debug the app, but it only goes up to item 6, and then does nothing. It keeps running, but the screen is completely blank. This is my view adapter,

 /******************************************************************************
  * Space Mines Android Game                                                   *
  * Copyright (c) 2022. By Troy Marker Enterprises.                            *
  * All rights Reserved.                                                       *
  * The code in this file was created for the game Space Mines.                *
  * This code may not be used outside this game.                               *
  ******************************************************************************/
  package com.troymarkerenterprises.spacemines.adapters;

  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.TextView;

  import androidx.annotation.NonNull;
  import androidx.recyclerview.widget.RecyclerView;

  import com.troymarkerenterprises.spacemines.R;
  import com.troymarkerenterprises.spacemines.model.Planet;

  import java.util.ArrayList;

  public class GalaxyRVA extends RecyclerView.Adapter<GalaxyRVA.ViewHolder> {
      private final ArrayList<Planet> mDataSet;

      public static class ViewHolder extends RecyclerView.ViewHolder {

          private final ImageView planetIcon;
          private final TextView planetId;
          private final TextView planetName;
          private final TextView planetSize;

          public ViewHolder(View view) {
              super(view);
              planetIcon = view.findViewById(R.id.imgPlanetIcon);
              planetId = view.findViewById(R.id.txtPlanetId);
              planetName = view.findViewById(R.id.txtPlanetName);
              planetSize = view.findViewById(R.id.txtPlanetSize);
          }

          public ImageView getPlanetIcon() {
              return planetIcon;
          }

          public TextView getPlanetId() {
              return planetId;
          }

          public TextView getPlanetName() {
              return planetName;
          }

          public TextView getPlanetSize() {
              return planetSize;
          }
      }

      public GalaxyRVA(ArrayList<Planet> dataSet) {
          mDataSet = dataSet;
      }

      @NonNull
      @Override
      public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
          View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_planet,
                  viewGroup, false);
          return new ViewHolder(view);
      }

      @Override
      public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int       position) {
          int icon = mDataSet.get(position).getIcon();
          int id = mDataSet.get(position).getId();
          String name = mDataSet.get(position).getName();
          int size = mDataSet.get(position).getSize();

          switch(icon) {
              case 1:
                  viewHolder.getPlanetIcon().setImageResource(R.drawable.planet_01);
                  break;
              case 2:
                  viewHolder.getPlanetIcon().setImageResource(R.drawable.planet_02);
                  break;
              case 3:
                  viewHolder.getPlanetIcon().setImageResource(R.drawable.planet_03);
                  break;
              case 4:
                  viewHolder.getPlanetIcon().setImageResource(R.drawable.planet_04);
                  break;
              case 5:
                  viewHolder.getPlanetIcon().setImageResource(R.drawable.planet_05);
                  break;
              case 6:
                  viewHolder.getPlanetIcon().setImageResource(R.drawable.planet_06);
                  break;
              case 7:
                  viewHolder.getPlanetIcon().setImageResource(R.drawable.planet_07);
                  break;
              case 8:
                  viewHolder.getPlanetIcon().setImageResource(R.drawable.planet_08);
                  break;
              case 9:
                  viewHolder.getPlanetIcon().setImageResource(R.drawable.planet_09);
                  break;
              default:
                  throw new IllegalStateException("Unexpected value: " + icon);
          }

          viewHolder.getPlanetId().setText(String.format("Plant ID: %s", id));
          viewHolder.getPlanetName().setText(String.format("Planet Name: %s",name));
          viewHolder.getPlanetSize().setText(String.format("Planet Diameter: %s miles", size));
      }

      @Override
      public int getItemCount() {
          return mDataSet.size();
      }
  }

The list of planets is read in another part of the app, here is the code that reads the planets from the database,

 /******************************************************************************
  * Space Mines Android Game                                                   *
  * Copyright (c) 2022. By Troy Marker Enterprises.                            *
  * All rights Reserved.                                                       *
  * The code in this file was created for the game Space Mines.                *
  * This code may not be used outside this game.                               *
 ******************************************************************************/

 package com.troymarkerenterprises.spacemines.database;

 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_AG;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_AU;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_CU;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_ICON;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_ID;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_MS_AG;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_MS_AU;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_MS_CU;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_MS_PD;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_MS_PT;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_M_AG;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_M_AU;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_M_CU;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_M_PD;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_M_PT;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_NAME;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_PD;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_POP;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_PT;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_SIZE;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_S_AG;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_S_AU;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_S_CU;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_S_PD;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.COL_S_PT;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.DATABASE_NAME;
 import static com.troymarkerenterprises.spacemines.constants.DbCon.PLANETS_TABLE;
 
 import android.annotation.SuppressLint;
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import androidx.annotation.Nullable;
 
 import com.troymarkerenterprises.spacemines.helpers.GeneralHelper;
 import com.troymarkerenterprises.spacemines.helpers.PlanetHelper;
 import com.troymarkerenterprises.spacemines.model.Planet;

 import java.util.ArrayList;

 public class Database extends SQLiteOpenHelper {

     SQLiteDatabase db;
     PlanetHelper ph = new PlanetHelper();
     GeneralHelper gh = new GeneralHelper();
     Context context;

     public Database(@Nullable Context context) {
         super(context, DATABASE_NAME, null, 1);
         this.context = context;
     }

     @Override
     public void onCreate(SQLiteDatabase db) {
         String sql = String.format(
                 "create table %s " +
                         "(%s INTEGER PRIMARY KEY, " +
                         "%s TEXT NOT NULL, " +
                         "%s INTEGER, " +
                         "%s INTEGER," +
                         "%s REAL, " +
                         "%s REAL, " +
                         "%s REAL, " +
                         "%s REAL, " +
                         "%s REAL, " +
                         "%s TEXT, " +
                         "%s TEXT, " +
                         "%s TEXT, " +
                         "%S TEXT, " +
                         "%s TEXT, " +
                         "%s REAL, " +
                         "%s REAL, " +
                         "%s REAL, " +
                         "%s REAL, " +
                         "%S REAL, " +
                         "%s TEXT, " +
                         "%s TEXT, " +
                         "%s TEXT, " +
                         "%S TEXT, " +
                         "%s TEXT, " +
                         "%s INTEGER)",
                 PLANETS_TABLE,
                 COL_ID,
                 COL_NAME,
                 COL_SIZE,
                 COL_POP,
                 COL_CU,
                 COL_AG,
                 COL_AU,
                 COL_PT,
                 COL_PD,
                 COL_S_CU,
                 COL_S_AG,
                 COL_S_AU,
                 COL_S_PT,
                 COL_S_PD,
                 COL_M_CU,
                 COL_M_AG,
                 COL_M_AU,
                 COL_M_PT,
                 COL_M_PD,
                 COL_MS_CU,
                 COL_MS_AG,
                 COL_MS_AU,
                 COL_MS_PT,
                 COL_MS_PD,
                 COL_ICON);
         db.execSQL(sql);
     }

     @Override
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

     }

     public void createGalaxy() {
         db = getWritableDatabase();
         db.delete(PLANETS_TABLE,null,null);
         int size;
         double radius;
         double surfaceArea;
         double minableCrust;
         double crustMass;
         int i = 0;
         while (i < 20) {
             size = ph.getSize();
             radius = size / 2;
             surfaceArea = ph.getSurfaceArea(radius);
             minableCrust = ph.getMinableCrust(surfaceArea);
             crustMass = ph.getCrustMass(minableCrust);
             ContentValues values = new ContentValues();
             values.put(COL_ID, i);
             values.put(COL_NAME, ph.getName());
             values.put(COL_SIZE, size);
             values.put(COL_POP, 0);
             values.put(COL_CU, ph.getCU(crustMass));
             values.put(COL_AG, ph.getAG(crustMass));
             values.put(COL_AU, ph.getAU(crustMass));
             values.put(COL_PT, ph.getPT(crustMass));
             values.put(COL_PD, ph.getPD(crustMass));
             values.put(COL_S_CU, ph.getNamedNumber(ph.getCU(crustMass)));
             values.put(COL_S_AG, gh.getNamedNumber(ph.getAG(crustMass)));
             values.put(COL_S_AU, gh.getNamedNumber(ph.getAU(crustMass)));
             values.put(COL_S_PT, gh.getNamedNumber(ph.getPT(crustMass)));
             values.put(COL_S_PD, gh.getNamedNumber(ph.getPD(crustMass)));
             values.put(COL_M_CU, 0);
             values.put(COL_M_AG, 0);
             values.put(COL_M_AU, 0);
             values.put(COL_M_PT, 0);
             values.put(COL_M_PD, 0);
             values.put(COL_MS_CU, gh.getNamedNumber(0));
             values.put(COL_MS_AG, gh.getNamedNumber(0));
             values.put(COL_MS_AU, gh.getNamedNumber(0));
             values.put(COL_MS_PT, gh.getNamedNumber(0));
             values.put(COL_MS_PD, gh.getNamedNumber(0));
             values.put(COL_ICON, ph.getIcon());
             db.insert(PLANETS_TABLE, null, values);
             i++;
         }
         db.close();
     }

     public ArrayList<Planet> loadGalaxy() {
         ArrayList<Planet> planets = new ArrayList<>();
         SQLiteDatabase db = getReadableDatabase();
         String sql = String.format("SELECT * FROM %s ORDER BY %s", PLANETS_TABLE, 
 COL_ID);
         @SuppressLint("Recycle") Cursor cursor = db.rawQuery(sql, null);
         while(cursor.moveToNext()) {
             int id = cursor.getInt(0);
             String name = cursor.getString(1);
             int size = cursor.getInt(2);
             int population = cursor.getInt(3);
             double t_cu = cursor.getDouble(4);
             double t_ag = cursor.getDouble(5);
             double t_au = cursor.getDouble(6);
             double t_pt = cursor.getDouble(7);
             double t_pd = cursor.getDouble(8);
             String s_cu = cursor.getString(9);
             String s_ag = cursor.getString(10);
             String s_au = cursor.getString(11);
             String s_pt = cursor.getString(12);
             String s_pd = cursor.getString(13);
             double m_cu = cursor.getInt(14);
             double m_ag = cursor.getInt(15);
             double m_au = cursor.getInt(16);
             double m_pt = cursor.getInt(17);
             double m_pd = cursor.getInt(18);
             String m_s_cu = cursor.getString(19);
             String m_s_ag = cursor.getString(20);
             String m_s_au = cursor.getString(21);
             String m_s_pt = cursor.getString(22);
             String m_s_pd = cursor.getString(23);
             int icon = cursor.getInt(24);
             planets.add(new Planet(id, name, size, population, t_cu, t_ag, t_au, 
 t_pt, t_pd, s_cu,
                     s_ag, s_au, s_pt, s_pd, m_cu, m_ag, m_au, m_pt, m_pd, m_s_cu, 
 m_s_ag, m_s_au,
                     m_s_pt, m_s_pd, icon));
         }
 
         db.close();
         return planets;
     }

     public boolean doesTableExist(String tableName) {
         if(db == null || !db.isOpen()) {
             db = getReadableDatabase();
         }
         if(!db.isReadOnly()) {
             db.close();
             db = getReadableDatabase();
         }
         String query = String.format("SELECT DISTINCT * FROM %s", tableName);
         try {
             Cursor cursor = db.rawQuery(query, null);
             if(cursor!=null) {
                 if(cursor.getCount()>0) {
                     cursor.close();
                     return true;
                 }
             }
             return false;
         } catch (Exception ex) {
             ex.printStackTrace();
             return false;
         }
     }
 }

I believe I have included all the relevant code for my problem. If you need to see some other code, I have it on my GitHub account here: https://github.com/troy-marker/Space-Mines.

Thank you for any suggestions and/or help. Troy Marker

Leave a Comment