【问题标题】:How to get the counter to increment in android studio如何让计数器在android studio中递增
【发布时间】:2020-10-11 17:26:26
【问题描述】:

我试图让 i 的值增加,但它似乎只是在这里重复 2 的值。我认为它应该保持递增 1,因为我在开始时将值设置为 1,但此处未显示,因为它靠近我的班级顶部..

public void onDataChange(@NonNull DataSnapshot snapshot) {
    for (DataSnapshot childSnapshot : snapshot.getChildren()) {

        String clinic_info = String.valueOf(childSnapshot.child("Name").getValue());
        String txt_review = review.getText().toString();
        String txt_dorevitch = dorevitch.getText().toString();
        String txt_skin_cancer = skin_cancer_check.getText().toString();
        String txt_ear_suctioning = ear_suctioning.getText().toString();

        message[0] = message[0].split("\\s*:\\s*")[0]; // use this to get the name of the clinic only..
        //   replace(":" ,"");
        //    message[0] = message[0].replaceAll("[0-9]" ,"");
        //   message[0] = message[0].replace("Shop" ,"");
        //
        assert clinic_info != null;
        if (clinic_info.equals(message[0])) {

            HashMap<String, Object> map = new HashMap<>();
            map.put("Review" + i, txt_review);

            i = (int)snapshot.getChildrenCount();

            i++;

            System.out.println(i);
            FirebaseDatabase.getInstance("https://medical-review-in-australia.firebaseio.com/").getReference().child(message[0]).updateChildren(map);
            Toast.makeText(Add_Review.this, "Review Successfully Submited", Toast.LENGTH_SHORT).show();
        } else {


            Toast.makeText(Add_Review.this, "There was an error while updating the database, please try again.", Toast.LENGTH_SHORT).show();
        }

【问题讨论】:

  • 由于某种原因,它要么停在特定的数字上,要么被覆盖......

标签: java android firebase-realtime-database


【解决方案1】:

问题似乎在于您没有更新i 的值。 i = i + 1 将起作用,因为您没有更改 i 的值。你可以试试打印System.out.println(i++);

【讨论】:

  • 我也试过了,但值只停在 3... 没有继续增加...。
  • 我有一种解决方法,但现在有另一个问题,我将单独发布
  • 当我使用数字增加计数器时,其他用户也会覆盖它,但我设法将用户名附加到数据库的评论名称
猜你喜欢
  • 1970-01-01
  • 2020-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多