Store and fetch a 2-D List of different sub-classes to Google Firebase
I have been building a multiplayer checker game for my class project.
The below code is in Room class because every game should have a checkerboard.
I converted to 2D List because Firebase does not serialize arrays.
Each of object stored in the list is a subclass of Checker, including nullc (a substitute for null). The reason why I store nullc in the list is that the list has two rows consist of only nulls, and Firebase does not store the index of those two rows.
this.checkerList = new ArrayList<List<Checker>>();
Checker nullc = new NullChecker();
Checker checkerArray = new Checker
nullc, new RedChecker(0,1), nullc, new RedChecker(0,3),
nullc, new RedChecker(0,5), nullc, new RedChecker(0,7),
new RedChecker(1,0), nullc, new RedChecker(1,2),nullc, new RedChecker(1,4), nullc, new RedChecker(1,6), nullc,
nullc, new RedChecker(2,1), nullc, new RedChecker(2,3), nullc, new RedChecker(2,5), nullc, new RedChecker(2,7),
nullc, nullc, nullc, nullc,nullc, nullc,nullc, nullc,
nullc, nullc, nullc, nullc,nullc, nullc,nullc, nullc,
new BlackChecker(5,0), nullc, new BlackChecker(5,2),nullc, new BlackChecker(5,4), nullc, new BlackChecker(5,6), nullc,
nullc, new BlackChecker(6,1), nullc, new BlackChecker(6,3), nullc, new BlackChecker(6,5), nullc, new BlackChecker(6,7),
new BlackChecker(7,0), nullc, new BlackChecker(7,2),nullc, new BlackChecker(7,4), nullc, new BlackChecker(7,6), nullc;
for(int r = 0; r < checkerArray.length; r++)
List <Checker> row = new ArrayList <Checker>();
for(int c = 0; c < checkerArray[r].length; c++)
row.add(checkerArray[r][c]);
this.checkerList.add(row);
The problem I have now is the list stores fine in Firebase database. However, when I retrieve the whole list from the database, the subclasses are not subclasses anymore.
refThisRoom.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
if(dataSnapshot.getValue(Room.class).getPlayer2() != null)
turn = dataSnapshot.getValue(Room.class).getTurn();
checkerList = dataSnapshot.getValue(Room.class).getCheckerList();
for(int r = 0; r < checkerList.size(); r++)
Log.d("checkerList", "row" + String.valueOf(r));
Log.d("checkerList", "rowSize" + String.valueOf(checkerList.size()));
for(int c = 0; c < checkerList.get(r).size(); c++)
Log.d("checkerList", "Column" + String.valueOf(c));
Log.d("checkerList", "ColumnSize" + String.valueOf(checkerList.get(r).size()));
Log.d("checkerList","hello: "+checkerList.get(r).get(c).getClass().getSimpleName());
Toast.makeText(BlackCheckerActivity.this,"Player Entered",Toast.LENGTH_LONG).show();
updateAllButtons();
disableButtons();
else
Toast.makeText(BlackCheckerActivity.this,"Waiting for another player",Toast.LENGTH_LONG).show();
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
Instead of returning BlackChecker, RedChecker, and NullChecker, it is just returning checker. I am stuck now because I am using instanceof to determine each checker's move.
java android firebase android-studio android-gson
add a comment |
I have been building a multiplayer checker game for my class project.
The below code is in Room class because every game should have a checkerboard.
I converted to 2D List because Firebase does not serialize arrays.
Each of object stored in the list is a subclass of Checker, including nullc (a substitute for null). The reason why I store nullc in the list is that the list has two rows consist of only nulls, and Firebase does not store the index of those two rows.
this.checkerList = new ArrayList<List<Checker>>();
Checker nullc = new NullChecker();
Checker checkerArray = new Checker
nullc, new RedChecker(0,1), nullc, new RedChecker(0,3),
nullc, new RedChecker(0,5), nullc, new RedChecker(0,7),
new RedChecker(1,0), nullc, new RedChecker(1,2),nullc, new RedChecker(1,4), nullc, new RedChecker(1,6), nullc,
nullc, new RedChecker(2,1), nullc, new RedChecker(2,3), nullc, new RedChecker(2,5), nullc, new RedChecker(2,7),
nullc, nullc, nullc, nullc,nullc, nullc,nullc, nullc,
nullc, nullc, nullc, nullc,nullc, nullc,nullc, nullc,
new BlackChecker(5,0), nullc, new BlackChecker(5,2),nullc, new BlackChecker(5,4), nullc, new BlackChecker(5,6), nullc,
nullc, new BlackChecker(6,1), nullc, new BlackChecker(6,3), nullc, new BlackChecker(6,5), nullc, new BlackChecker(6,7),
new BlackChecker(7,0), nullc, new BlackChecker(7,2),nullc, new BlackChecker(7,4), nullc, new BlackChecker(7,6), nullc;
for(int r = 0; r < checkerArray.length; r++)
List <Checker> row = new ArrayList <Checker>();
for(int c = 0; c < checkerArray[r].length; c++)
row.add(checkerArray[r][c]);
this.checkerList.add(row);
The problem I have now is the list stores fine in Firebase database. However, when I retrieve the whole list from the database, the subclasses are not subclasses anymore.
refThisRoom.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
if(dataSnapshot.getValue(Room.class).getPlayer2() != null)
turn = dataSnapshot.getValue(Room.class).getTurn();
checkerList = dataSnapshot.getValue(Room.class).getCheckerList();
for(int r = 0; r < checkerList.size(); r++)
Log.d("checkerList", "row" + String.valueOf(r));
Log.d("checkerList", "rowSize" + String.valueOf(checkerList.size()));
for(int c = 0; c < checkerList.get(r).size(); c++)
Log.d("checkerList", "Column" + String.valueOf(c));
Log.d("checkerList", "ColumnSize" + String.valueOf(checkerList.get(r).size()));
Log.d("checkerList","hello: "+checkerList.get(r).get(c).getClass().getSimpleName());
Toast.makeText(BlackCheckerActivity.this,"Player Entered",Toast.LENGTH_LONG).show();
updateAllButtons();
disableButtons();
else
Toast.makeText(BlackCheckerActivity.this,"Waiting for another player",Toast.LENGTH_LONG).show();
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
Instead of returning BlackChecker, RedChecker, and NullChecker, it is just returning checker. I am stuck now because I am using instanceof to determine each checker's move.
java android firebase android-studio android-gson
add a comment |
I have been building a multiplayer checker game for my class project.
The below code is in Room class because every game should have a checkerboard.
I converted to 2D List because Firebase does not serialize arrays.
Each of object stored in the list is a subclass of Checker, including nullc (a substitute for null). The reason why I store nullc in the list is that the list has two rows consist of only nulls, and Firebase does not store the index of those two rows.
this.checkerList = new ArrayList<List<Checker>>();
Checker nullc = new NullChecker();
Checker checkerArray = new Checker
nullc, new RedChecker(0,1), nullc, new RedChecker(0,3),
nullc, new RedChecker(0,5), nullc, new RedChecker(0,7),
new RedChecker(1,0), nullc, new RedChecker(1,2),nullc, new RedChecker(1,4), nullc, new RedChecker(1,6), nullc,
nullc, new RedChecker(2,1), nullc, new RedChecker(2,3), nullc, new RedChecker(2,5), nullc, new RedChecker(2,7),
nullc, nullc, nullc, nullc,nullc, nullc,nullc, nullc,
nullc, nullc, nullc, nullc,nullc, nullc,nullc, nullc,
new BlackChecker(5,0), nullc, new BlackChecker(5,2),nullc, new BlackChecker(5,4), nullc, new BlackChecker(5,6), nullc,
nullc, new BlackChecker(6,1), nullc, new BlackChecker(6,3), nullc, new BlackChecker(6,5), nullc, new BlackChecker(6,7),
new BlackChecker(7,0), nullc, new BlackChecker(7,2),nullc, new BlackChecker(7,4), nullc, new BlackChecker(7,6), nullc;
for(int r = 0; r < checkerArray.length; r++)
List <Checker> row = new ArrayList <Checker>();
for(int c = 0; c < checkerArray[r].length; c++)
row.add(checkerArray[r][c]);
this.checkerList.add(row);
The problem I have now is the list stores fine in Firebase database. However, when I retrieve the whole list from the database, the subclasses are not subclasses anymore.
refThisRoom.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
if(dataSnapshot.getValue(Room.class).getPlayer2() != null)
turn = dataSnapshot.getValue(Room.class).getTurn();
checkerList = dataSnapshot.getValue(Room.class).getCheckerList();
for(int r = 0; r < checkerList.size(); r++)
Log.d("checkerList", "row" + String.valueOf(r));
Log.d("checkerList", "rowSize" + String.valueOf(checkerList.size()));
for(int c = 0; c < checkerList.get(r).size(); c++)
Log.d("checkerList", "Column" + String.valueOf(c));
Log.d("checkerList", "ColumnSize" + String.valueOf(checkerList.get(r).size()));
Log.d("checkerList","hello: "+checkerList.get(r).get(c).getClass().getSimpleName());
Toast.makeText(BlackCheckerActivity.this,"Player Entered",Toast.LENGTH_LONG).show();
updateAllButtons();
disableButtons();
else
Toast.makeText(BlackCheckerActivity.this,"Waiting for another player",Toast.LENGTH_LONG).show();
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
Instead of returning BlackChecker, RedChecker, and NullChecker, it is just returning checker. I am stuck now because I am using instanceof to determine each checker's move.
java android firebase android-studio android-gson
I have been building a multiplayer checker game for my class project.
The below code is in Room class because every game should have a checkerboard.
I converted to 2D List because Firebase does not serialize arrays.
Each of object stored in the list is a subclass of Checker, including nullc (a substitute for null). The reason why I store nullc in the list is that the list has two rows consist of only nulls, and Firebase does not store the index of those two rows.
this.checkerList = new ArrayList<List<Checker>>();
Checker nullc = new NullChecker();
Checker checkerArray = new Checker
nullc, new RedChecker(0,1), nullc, new RedChecker(0,3),
nullc, new RedChecker(0,5), nullc, new RedChecker(0,7),
new RedChecker(1,0), nullc, new RedChecker(1,2),nullc, new RedChecker(1,4), nullc, new RedChecker(1,6), nullc,
nullc, new RedChecker(2,1), nullc, new RedChecker(2,3), nullc, new RedChecker(2,5), nullc, new RedChecker(2,7),
nullc, nullc, nullc, nullc,nullc, nullc,nullc, nullc,
nullc, nullc, nullc, nullc,nullc, nullc,nullc, nullc,
new BlackChecker(5,0), nullc, new BlackChecker(5,2),nullc, new BlackChecker(5,4), nullc, new BlackChecker(5,6), nullc,
nullc, new BlackChecker(6,1), nullc, new BlackChecker(6,3), nullc, new BlackChecker(6,5), nullc, new BlackChecker(6,7),
new BlackChecker(7,0), nullc, new BlackChecker(7,2),nullc, new BlackChecker(7,4), nullc, new BlackChecker(7,6), nullc;
for(int r = 0; r < checkerArray.length; r++)
List <Checker> row = new ArrayList <Checker>();
for(int c = 0; c < checkerArray[r].length; c++)
row.add(checkerArray[r][c]);
this.checkerList.add(row);
The problem I have now is the list stores fine in Firebase database. However, when I retrieve the whole list from the database, the subclasses are not subclasses anymore.
refThisRoom.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
if(dataSnapshot.getValue(Room.class).getPlayer2() != null)
turn = dataSnapshot.getValue(Room.class).getTurn();
checkerList = dataSnapshot.getValue(Room.class).getCheckerList();
for(int r = 0; r < checkerList.size(); r++)
Log.d("checkerList", "row" + String.valueOf(r));
Log.d("checkerList", "rowSize" + String.valueOf(checkerList.size()));
for(int c = 0; c < checkerList.get(r).size(); c++)
Log.d("checkerList", "Column" + String.valueOf(c));
Log.d("checkerList", "ColumnSize" + String.valueOf(checkerList.get(r).size()));
Log.d("checkerList","hello: "+checkerList.get(r).get(c).getClass().getSimpleName());
Toast.makeText(BlackCheckerActivity.this,"Player Entered",Toast.LENGTH_LONG).show();
updateAllButtons();
disableButtons();
else
Toast.makeText(BlackCheckerActivity.this,"Waiting for another player",Toast.LENGTH_LONG).show();
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
Instead of returning BlackChecker, RedChecker, and NullChecker, it is just returning checker. I am stuck now because I am using instanceof to determine each checker's move.
java android firebase android-studio android-gson
java android firebase android-studio android-gson
edited Nov 14 '18 at 15:49
user9281765
asked Nov 14 '18 at 3:29
user9281765user9281765
92
92
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I solved the issue by adding another field called "type" of type string to the parent class in the constructor. For the subclasses, when I create a subclass, I wrote
new RedChecker(row, column, "RedChecker")
new BlackChecker(row, column, "BlackerChecker")
new NullChecker(row, column, "NullChecker").
When retrieving the list of Checker class back, I replaced all the superclass in the object with newly initiated subclasses. Something like
if(checkerList.get(r).get(c).getType().equals("Red/Black/NullChecker"))
checkerList.get(r).set(c, new Red/Black/NullChecker(checkerList.get(r).get(c).getRow(), checkerList.get(r).get(c).getColumn(), checkerList.get(r).get(c).getType())
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292776%2fstore-and-fetch-a-2-d-list-of-different-sub-classes-to-google-firebase%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I solved the issue by adding another field called "type" of type string to the parent class in the constructor. For the subclasses, when I create a subclass, I wrote
new RedChecker(row, column, "RedChecker")
new BlackChecker(row, column, "BlackerChecker")
new NullChecker(row, column, "NullChecker").
When retrieving the list of Checker class back, I replaced all the superclass in the object with newly initiated subclasses. Something like
if(checkerList.get(r).get(c).getType().equals("Red/Black/NullChecker"))
checkerList.get(r).set(c, new Red/Black/NullChecker(checkerList.get(r).get(c).getRow(), checkerList.get(r).get(c).getColumn(), checkerList.get(r).get(c).getType())
add a comment |
I solved the issue by adding another field called "type" of type string to the parent class in the constructor. For the subclasses, when I create a subclass, I wrote
new RedChecker(row, column, "RedChecker")
new BlackChecker(row, column, "BlackerChecker")
new NullChecker(row, column, "NullChecker").
When retrieving the list of Checker class back, I replaced all the superclass in the object with newly initiated subclasses. Something like
if(checkerList.get(r).get(c).getType().equals("Red/Black/NullChecker"))
checkerList.get(r).set(c, new Red/Black/NullChecker(checkerList.get(r).get(c).getRow(), checkerList.get(r).get(c).getColumn(), checkerList.get(r).get(c).getType())
add a comment |
I solved the issue by adding another field called "type" of type string to the parent class in the constructor. For the subclasses, when I create a subclass, I wrote
new RedChecker(row, column, "RedChecker")
new BlackChecker(row, column, "BlackerChecker")
new NullChecker(row, column, "NullChecker").
When retrieving the list of Checker class back, I replaced all the superclass in the object with newly initiated subclasses. Something like
if(checkerList.get(r).get(c).getType().equals("Red/Black/NullChecker"))
checkerList.get(r).set(c, new Red/Black/NullChecker(checkerList.get(r).get(c).getRow(), checkerList.get(r).get(c).getColumn(), checkerList.get(r).get(c).getType())
I solved the issue by adding another field called "type" of type string to the parent class in the constructor. For the subclasses, when I create a subclass, I wrote
new RedChecker(row, column, "RedChecker")
new BlackChecker(row, column, "BlackerChecker")
new NullChecker(row, column, "NullChecker").
When retrieving the list of Checker class back, I replaced all the superclass in the object with newly initiated subclasses. Something like
if(checkerList.get(r).get(c).getType().equals("Red/Black/NullChecker"))
checkerList.get(r).set(c, new Red/Black/NullChecker(checkerList.get(r).get(c).getRow(), checkerList.get(r).get(c).getColumn(), checkerList.get(r).get(c).getType())
answered Nov 15 '18 at 3:54
user9281765user9281765
92
92
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292776%2fstore-and-fetch-a-2-d-list-of-different-sub-classes-to-google-firebase%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown