Java 导致活动召回Firebase Android的事务

Java 导致活动召回Firebase Android的事务,java,android,transactions,firebase,Java,Android,Transactions,Firebase,当我单击我的费率以启动此交易功能时 它工作正常,但在此过程中,每次使用事务时,它都会再次运行我的类活动一次,从而重置所有内容,如我的msg_ID 例如,每次调用该类活动时,由于使用random,我的msg_ID都会更改,因此当我运行事务时,它会工作,但作为回报,它运行类活动,因此也会更改我的msg_ID 下面是一个场景: 因此,当点击这个比率按钮时,它会进行评级 在当前的msg_ID上,但当我再次单击它时,它的评级不同 msg_ID是因为我的get random msg_ID。我认为这是由于 我

当我单击我的费率以启动此交易功能时

它工作正常,但在此过程中,每次使用事务时,它都会再次运行我的类活动一次,从而重置所有内容,如我的msg_ID

例如,每次调用该类活动时,由于使用random,我的msg_ID都会更改,因此当我运行事务时,它会工作,但作为回报,它运行类活动,因此也会更改我的msg_ID

下面是一个场景:

因此,当点击这个比率按钮时,它会进行评级 在当前的msg_ID上,但当我再次单击它时,它的评级不同 msg_ID是因为我的get random msg_ID。我认为这是由于 我调用onComplete方法

当我单击“速率”\u btn

现在该类重新运行,我再次单击rate_btn

它投票支持第一个msg\u id,然后当我再次单击rate\u btn时,它投票支持第二个键,因为它被称为class activity,并且msg\u 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();
        }

有什么想法吗?

因为每次调用事务时消息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();
    }`