如何从Firebase一项一个地检索增量数据?

时间:2019-03-22 07:26:53

标签: android firebase

我有一个数据库,我想按1检索名称1。起初,它仅显示Alex

{
  "parent" : {
    "number1" : {
      "info" : "sadsad",
      "name" : "Alex"
    },
    "number2" : {
      "info" : "sadsad",
      "name" : "Andre"
    },
    "number3" : {
      "info" : "sadsad",
      "name" : "Allen"
    }
  }
}

当我单击nextButton时,Alex将替换为Andre,而当我单击prevButton时,Andre将替换为Alex。如何实现?

FirebaseDatabase.getInstance().setPersistenceEnabled(true);
        root= FirebaseDatabase.getInstance().getReference();
        pariant= root.child("parent");
        number = 1;
        pariant.child("number"+number).child("name").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                String value = dataSnapshot.getValue(String.class);
                editText.setHint("His name is "+ value);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //retrieve the next name here
            }
        });

prevButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //retrieve previous name here
            }
        });

1 个答案:

答案 0 :(得分:-1)

您必须在此类中引入要在方法调用中使用的计数器。

然后单击next,您必须增加1,对于previous按钮,只需将其减小。

// New counter
private static int counter = 1;

// Your method from the question
public void mainMethod(int number) {
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    root = FirebaseDatabase.getInstance().getReference();
    pariant = root.child("parent");
    number = 1;

    // load data (at the beginning)
    loadName(counter);

    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // increase counter
            counter++;
            // load data
            loadName(counter);
        }
    });

    prevButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // To do not load user 0, -1, -2...
            if (counter <= 1) {
                return;
            }

            // decrease counter 
            counter--;
            // load data
            loadName(counter);
        }
    });
}

您可以提取“加载逻辑”到新方法中,并在其中传递数字(counter)并加载数据

private static void loadName(int number) {
    pariant.child("number" + number).child("name").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String value = dataSnapshot.getValue(String.class);
            editText.setHint("His name is " + value);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}