我想从列表中删除一个用户,但它似乎不起作用。我的项目中有一个 Feeds.class 我有:
public class Feeds {
private List<BackendlessUser> usersThatLike;
public List<BackendlessUser> getUsersThatLike() {
return usersThatLike;
}
public void setUsersThatLike(List<BackendlessUser> usersThatLike) {
this.usersThatLike = usersThatLike;
}
}
在我的适配器中,我尝试像这样删除当前用户:
feed.getUsersThatLike().remove(Backendless.UserService.CurrentUser);
除了保存 Feeds 类并在 handleResponse 中,我将列表打印为日志窗口中的字符串,并且当前用户仍在列表中。无法弄清楚它有什么问题。需要帮忙。谢谢
适配器
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.FeedsHolder> {
private List<Feeds> list;
private Context context;
private static List<Long> selectedIds;
List<BackendlessUser> backendlessUserList;
private List<Comments> cake = new ArrayList<>();
private static String TAG = "MainActivity";
public FeedAdapter(Context context, List<Feeds> list) {
this.context = context;
this.list = list;
}
@Override
public FeedsHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_item, parent, false);
final FeedsHolder holder = new FeedsHolder(view);
//Query
QueryOptions options = new QueryOptions();
options.setRelated(Arrays.asList("usersThatLike"));
BackendlessDataQuery query = new BackendlessDataQuery();
query.setQueryOptions(options);
Backendless.Data.of(Feeds.class).find(query, new AsyncCallback<BackendlessCollection<Feeds>>() {
@Override
public void handleResponse(BackendlessCollection<Feeds> response) {
myFeeds = response;
String userId = Backendless.UserService.CurrentUser().getObjectId();
for (Feeds feed : response.getData()) {
feed = list.get(holder.getAdapterPosition());
List<BackendlessUser> likedUsers = feed.getUsersThatLike();
backendlessUserList = likedUsers;
for (BackendlessUser user : likedUsers)
if (user.getObjectId().equals(userId)) {
setBackground(holder.like, ContextCompat.getDrawable(context, R.drawable.ic_favorite_black_24dp));
} else if (!user.getObjectId().equals(userId)) {
setBackground(holder.like, ContextCompat.getDrawable(context, R.drawable.ic_favorite_border_black_24dp));
}
}
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
}
});
return holder;
}
@Override
public void onBindViewHolder(final FeedsHolder holder, final int position) {
//Starting Feeds
final Feeds feeds = list.get(position);
holder.like.setBackground(ContextCompat.getDrawable(context, R.drawable.ic_favorite_border_black_24dp));
holder.like.setOnClickListener.......
//Skipped for brevity
feeds.getUsersThatLike().remove(Backendless.UserService.CurrentUser);
//SAVE FEEDS HERE
}
}
//Name
holder.name.setText(feeds.getOwner());
@Override
public int getItemCount() {
return list.size();
}
public class FeedsHolder extends RecyclerView.ViewHolder {
//Setup Views
CardView card;
TextView name;
ImageView like;
public FeedsHolder(View itemView) {
super(itemView);
//Assign views by ID
card = (CardView) itemView.findViewById(R.id.cv);
name = (TextView) itemView.findViewById(R.id.commenter);
like = (ImageView) itemView.findViewById(R.id.like);
}
}
}