当我单击我的rate_btn来启动这个事务函数时。
它运行得很好,但是在这个过程中,它再次运行我的类活动(每次使用事务时,我的类活动都会重新运行),因此重置所有东西,比如我的msg_ID。
例如,每次调用这个类活动时,我的msg_ID都会更改(由于使用随机),所以当我运行事务时,它会工作,但作为回报,它也运行类活动,因此也会更改我的msg_ID。
下面是一个场景:
因此,当点击这个“费率”按钮时,它会对当前的msg_ID进行评级,但当我再次单击它时,它会给出一个不同的msg_ID评级,因为我的get随机选择了msg_ID。我认为这是在我调用onComplete方法时造成的。
当我单击rate_btn时
现在,类重新运行,我再次单击rate_btn。
它对第一个msg_id进行表决,然后当我再次单击rate_btn时,它对第二个键进行表决(因为它被重新称为类活动,msg_id也发生了变化)。
以下是事务的代码:
rate_btn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Firebase upvoteref = new Firebase("https://myapp.firebaseio.com/"+msgID+"/upvotes");
upvoteref.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(final MutableData currentData) {
if (currentData.getValue() == null) {
currentData.setValue(1);
} else {
currentData.setValue((Long) currentData.getValue() + 1);
}
return Transaction.success(currentData);
}
public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) {
if (firebaseError != null) {
System.out.println("Firebase counter increment failed.");
} else {
System.out.println("Firebase counter increment succeeded.");
}
}
});
}
以下是检索随机id的代码:
String msgID; //this variable is declare at the start of the class as a global variable.
Firebase ref = new Firebase("https://myapp.firebaseio.com/"+msgID);
ref.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
Iterable<DataSnapshot> ds = snapshot.getChildren();
//getting maximum number of children
long allNum = snapshot.getChildrenCount();
int maxNum = (int)allNum;
//getting the random integer from number of children
int randomNum = new Random().nextInt(maxNum);
Iterator<DataSnapshot> ids = ds.iterator();
int count = 0;
//has next will check if there are values in the next iteration , while count is used as a position substitute.
while(ids.hasNext() && count < randomNum) {
ids.next();
count ++; // used as positioning.
}
Map<String, Object> newPost = (Map<String, Object>) ids.next().getValue(); // ids will take the value in the iterator (iterate at key)
//getting the message from the key
msgID = newPost.get("id").toString();
}
有什么想法吗?
发布于 2015-03-25 00:56:10
因为每次调用事务时消息ID都会更改,这是因为addValueEventListener
(它在调用事务时接受更改)。因此,为了解决这个问题,我添加了addListenerForSingleValueEvent
而不是addValueEventListener
,并且它起了作用。
例如:
ref.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
Iterable<DataSnapshot> ds = snapshot.getChildren();
//getting maximum number of children
long allNum = snapshot.getChildrenCount();
int maxNum = (int)allNum;
//getting the random integer from number of children
int randomNum = new Random().nextInt(maxNum);
Iterator<DataSnapshot> ids = ds.iterator();
int count = 0;
//has next will check if there are values in the next iteration , while count is used as a position substitute.
while(ids.hasNext() && count < randomNum) {
ids.next();
count ++; // used as positioning.
}
Map<String, Object> newPost = (Map<String, Object>) ids.next().getValue(); // ids will take the value in the iterator (iterate at key)
//getting the message from the key
msgID = newPost.get("id").toString();
}`
https://stackoverflow.com/questions/29022566
复制相似问题