我在更新RecyclerView数据时遇到问题,我想notifyDataSetChanged()有问题,但我不知道是什么。 RecyclerView在创建后只是空的,没有任何变化。
这是我的代码:
EditFriends2 (once i have it working i erase the "2" ;) )
public class EditFriendsActivty2 extends AppCompatActivity {
private static final String TAG = EditFriendsActivty2.class.getSimpleName();
private ParseUser mCurrentUser;
private ParseRelation<ParseUser> mFriendsRelation;
private List<ParseUser> mFriends;
private List<ParseUser> mUsers;
private FriendsAdapter adapter;
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_friends_activty2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
adapter = new FriendsAdapter(mUsers, mFriends);
mRecyclerView.setAdapter(adapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(EditFriendsActivty2.this);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setHasFixedSize(true);
showFriendsList();
}
@Override
protected void onResume() {
super.onResume();
showFriendsList();
}
private void showFriendsList() {
mCurrentUser = ParseUser.getCurrentUser();
mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.orderByAscending(ParseConstants.KEY_USERNAME);
query.setLimit(1000);
query.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> list, ParseException e) {
if (e == null) {
// Success
mUsers = list;
downloadFriends();
} else {
Toast.makeText(EditFriendsActivty2.this, R.string.error_toast, Toast.LENGTH_SHORT).show();
Log.i(TAG, e.getMessage());
}
}
});
}
private void downloadFriends() {
mFriendsRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> list, ParseException e) {
if (e == null) {
mFriends = list;
// Here i'm updating the RecyclerView, when i have the data about users and friends
adapter.notifyDataSetChanged();
} else {
Log.i(TAG, e.getMessage());
}
}
});
}
}
我的适配器:
public class FriendsAdapter extends RecyclerView.Adapter<FriendsAdapter.FriendsViewHolder> {
private static final String TAG = FriendsAdapter.class.getSimpleName();
private List<ParseUser> mUsers;
private List<ParseUser> mFriends;
public FriendsAdapter(List<ParseUser> users, List<ParseUser> friends){
mUsers = users;
mFriends = friends;
}
@Override
public FriendsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate((R.layout.friends_list_item), parent, false);
FriendsViewHolder viewHolder = new FriendsViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(FriendsViewHolder holder, int position) {
if(mUsers != null) {
holder.bindFriends(mUsers.get(position));
}
}
@Override
public int getItemCount() {
return 0;
}
public class FriendsViewHolder extends RecyclerView.ViewHolder {
public TextView mNameLabel;
public CheckBox mCheckBox;
public FriendsViewHolder(View itemView) {
super(itemView);
mNameLabel = (TextView) itemView.findViewById(R.id.nameLabel);
mCheckBox = (CheckBox) itemView.findViewById(R.id.checkBox);
}
public void bindFriends (ParseUser user){
mNameLabel.setText(user.getUsername());
mCheckBox.setChecked(false);
for (ParseUser friend : mFriends){
if(friend.getObjectId().equals(user.getObjectId())){
mCheckBox.setChecked(true);
break;
}
}
}
}
}
任何人都可以帮我解决吗?谢谢你!
答案 0 :(得分:1)
最初在创建FriendsAdapter
时,您的mFriends
和mUsers
列表是null
?
当您收到数据回复时,您将使用新引用替换当前的mUsers
和mFriends
列表。虽然RecyclerView将继续指向旧列表。
解决方案1:
确保mFriends
和mUsers
不是null
开头。
然后,您可以{@ 1}}和mFriends.addAll(newFriends)
添加新朋友和用户。然后执行mUsers.addAll(newUsers)
解决方案2:
在adapter.notifyDataSetChanged()
中添加一个方法,告诉RecyclerView您有一个新列表FriendsAdapter
。如果您需要将这些用户作为另一个参数或其他方法调用,请先添加用户。在此适配器方法中,您可以调用adapter.showNewFriends(mFriends)
。
答案 1 :(得分:1)
问题是您实际上从未真正更改适配器列表。查看Activity
类中的这一行:
mFriendsRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> list, ParseException e) {
if (e == null) {
mFriends = list;
// Here i'm updating the RecyclerView, when i have the data about users and friends
adapter.notifyDataSetChanged();
} else {
Log.i(TAG, e.getMessage());
}
}
});
您正在设置的mFriends
对象是Activity中的变量,它与Adapter内的变量不同。我建议在你的适配器中添加这样的方法:
public void swapFriends(List<ParseUser> friends) {
mFriends = friends;
notifyDataSetChanged();
}
然后你可以在你的findInBackground()
方法中调用它:
mFriendsRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> list, ParseException e) {
if (e == null) {
adapter.swapFriends(list);
} else {
Log.i(TAG, e.getMessage());
}
}
});
编辑:另一个问题可能在于您的getItemCount()
方法。您总是返回0.您需要返回要显示的朋友数量。将其更改为:
@Override
public int getItemCount() {
return mFriends.size();
}