I created a class called "Roommate" that is constructed using a ParseUser variable, and I tried to create a new Roommate object and add it to my Roommate arrayList. However, after the query is complete, it seems that any modification or initializations I make are reset. My arrayList ends up being empty once the query is finished. Does anyone know why this is and what I can do to get around this issue?
I apologize if this is a basic question. I'm a student and this is my first time trying to make an app. All feedback is appreciated.
ArrayList and OnCreate method where I call updateRoommatesArray() :
// variables
private ArrayList<Roommate> roommates = new ArrayList<>();
AddRoommateRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_roommates);
// Set up toolbar
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar_roommate);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Add Roommates");
// Set up floating action button
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//show dialog on click
AddRoommatesDialog addRoommatesDialog = new AddRoommatesDialog();
addRoommatesDialog.show(getSupportFragmentManager(),"add roommates dialog");
}
});
// Initializes roommateNames with data from Parse
updateRoommatesArray();
Toast.makeText(this, Integer.toString(roommates.size()), Toast.LENGTH_SHORT).show();
// Set up Recycler View
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
RecyclerView recyclerView = findViewById(R.id.rm_recycler);
recyclerView.setLayoutManager(layoutManager);
adapter = new AddRoommateRecyclerViewAdapter(this, roommates);
recyclerView.setAdapter(adapter);
}
updateRoommatesArray Method:
public void updateRoommatesArray() {
roommates.clear();
final ParseUser currentUser = ParseUser.getCurrentUser();
ParseRelation<ParseUser> relation = currentUser.getRelation("roommates");
relation.getQuery().findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> objects, ParseException e) {
if (e == null && objects.size() > 0) {
for (ParseUser roommate : objects) {
Roommate current_roommate = new Roommate(roommate);
roommates.add(current_roommate);
}
}
else {
Log.i("Query", "Empty or Not Working");
}
}
});
}
Roommate Class:
public class Roommate {
// Member Variables
String objectId;
String username;
String firstName;
String lastName;
String fullName;
ArrayList<Roommate> roommates;
// Constructor
public Roommate(ParseUser user_in) {
objectId = user_in.getObjectId().toString();
username = user_in.getUsername().toString();
firstName = user_in.get("firstName").toString();
lastName = user_in.get("lastName").toString();
fullName = firstName + " " + lastName;
roommates = new ArrayList<>();
}
public String getObjectId() {
return objectId;
}
public String getUsername() {
return username;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getFullName() {
return fullName;
}
public ArrayList<Roommate> getRoommates() {
return roommates;
}
}