2017-07-30 102 views
0

我试图从一个阵列场解析删除一个字符串的单个实例的单个实例:删除存储在的parseObject数组列表的字符串

final ParseObject post = mPosts.get(position); 
List<String> repliedToByList = post.getList("repliedToBy"); // Retrieve current list 
ParseUser currentUser = ParseUser.getCurrentUser().getObjectId(); 
repliedToByList.remove(currentUserObjectId); // Remove first instance of specified objectid 
post.remove("repliedToBy"); // Clear the entire list 
post.addAllUnique("repliedToBy", Collections.singletonList(repliedToByList)); // Add the new list 

该字符串是当前用户的ObjectID,当用户回复 “后” 被添加,因为这样的:

post.addAll("repliedToBy", Collections.singletonList(ParseUser.getCurrentUser().getObjectId())); 

例如,阵列包含:

["1RtqEgy1ct","f4qEWY8UOM","f4qEWY8UOM"] 

是否有任何方法可以删除f4qEWY8UOM的单个实例?另一种方法是使用增量/减量来做所有事情,但这对我来说并不理想。

+0

将列表转换为ArrayList的实例并遍历其条目而不是调用List.remove() –

+0

在服务器上,我将数组存储为变量,然后通过查找我的字符串进行迭代,如果找到它从数组中删除该索引的值+跳出循环,然后将该变量存储为右键的对象值,而不是使用它们的添加/删除方法。 缺点是这可能会导致数据库重新索引数组,这可能需要一段时间,具体取决于您使用此项的索引数量。 –

回答

0

递增我replyCount很简单:

// Increment replyCount 
postObject.increment("replyCount", 1); 
postObject.saveInBackground(); 

但有一些错误与Android解析SDK的陈述递减法,即postObject.increment("replyCount", -1),所以我尝试了以下内容:

代替存储的列表objectIds在一个列表中,然后计算它的大小,我简单地按照以下方式递增/递减,总是获取前一个replyCount,减去1,然后保存新的put call:

// Decrement replyCount 
int newReplyCount = postObject.getInt("replyCount") - 1; 
postObject.put("replyCount", newReplyCount); 
postObject.saveInBackground();