我创建了一个使用ParseUser变量构造的名为“ Roommate”的类,我尝试创建一个新的Roommate对象并将其添加到我的Roommate arrayList中。但是,查询完成后,似乎我所做的任何修改或初始化都已重置。查询完成后,我的arrayList最终为空。有谁知道这是为什么,以及我该如何解决这个问题?
很抱歉,这是一个基本问题。我是学生,这是我第一次尝试制作应用。感谢所有反馈。
ArrayList和OnCreate方法,我将其称为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方法:
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;
}
}
答案 0 :(得分:1)
由于findInBackground()方法是异步方法调用,因此刚刚出现问题。无法保证在终止findInBackground的作业之后执行RecyclerAdaptor适配器的设置。
请注意,您必须在updateRoommatesArray的第一行中将列表弄清楚。然后启动异步任务。之后,主线程想继续它的工作,并返回onCreate()方法来设置ArrayList。这将在不确定的时间内发生。
从本质上说,从数据库中加载数据将花费更长的时间,然后在主线程中执行简单的代码。
因此,大多数情况是recyclerView.setAdapter(adapter);将在数组填充到done()方法内部之前执行。
为确保适配器将被填充,请将以下代码放入done方法中。这样,您将保证正确的代码执行顺序。
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);