Yogesh Mohanraj
Yogesh Mohanraj

Reputation: 11

Initializing or modifying variable in Parse Query

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;
}
}

Upvotes: 1

Views: 48

Answers (1)

Mahdi Rajabi
Mahdi Rajabi

Reputation: 602

The problem has just been arisen since findInBackground() method is Asynchronous method call. there is no guarantee that setting up RecyclerAdaptor's adaptor will executing after terminating findInBackground's job.

pay attention that you make the list clear right in first line of updateRoommatesArray. then you start an asynchronous task. Afterward the main Thread would like to continue it's job and return to onCreate() method to setup ArrayList. It will take place in indefinite time.

Loading data from DataBase, by it's nature, will take longer time then executing simple code in main thread.

So it's mostly probable that recyclerView.setAdapter(adapter); will be executed before the array getting filled inside done() method.

To make sure that adapter will get filled put following code inside done method. by doing so, you will guarantee correct sequence of code execution.

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);

Upvotes: 1

Related Questions