【发布时间】:2018-08-12 17:06:03
【问题描述】:
我创建了一个名为“Roommate”的类,它是使用 ParseUser 变量构造的,我尝试创建一个新的 Roommate 对象并将其添加到我的 Roommate arrayList。但是,查询完成后,似乎我所做的任何修改或初始化都会被重置。查询完成后,我的 arrayList 最终为空。有谁知道这是为什么以及我可以做些什么来解决这个问题?
如果这是一个基本问题,我深表歉意。我是一名学生,这是我第一次尝试制作应用程序。感谢所有反馈。
我调用 updateRoommatesArray() 的 ArrayList 和 OnCreate 方法:
// 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 方法:
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");
}
}
});
}
室友班:
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;
}
}
【问题讨论】:
标签: java android parse-server