id

ygya80vv  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(209)

我正在制作一个android应用程序。我正在使用firebase realtime database.this我的json文件。

{
  "Data" : {
    "UyhzVqsz1BVFKoePa2NEmlPFu382" : {
      "Budget_Income" : {
        "Bonus" : {
          "stData1" : "Bonus",
          "stData2" : "Extra",
          "stData3" : "Income",
          "stData4" : "5000"
        },
        "Salary" : {
          "stData1" : "Salary",
          "stData2" : "March",
          "stData3" : "Income",
          "stData4" : "10000"
        }
      },
      "Entry_Income" : {
        "Salary" : {
          "-MYkkCYsdhi93FC0VlNl" : {
            "stData1" : "Salary",
            "stData2" : "21 Apr 2021",
            "stData3" : "March",
            "stData4" : "5000"
          },
          "-MYkkSo_Z6GMx1q3y9p6" : {
            "stData1" : "Salary",
            "stData2" : "21 Apr 2021",
            "stData3" : "New",
            "stData4" : "2000"
          }
        },
        "bbbb" : {
          "-MYk3lQuK4X6zcIZtHRD" : {
            "stData1" : "bbbb",
            "stData2" : "20 Apr 2021",
            "stData3" : "ddd",
            "stData4" : "500"
          }
        }
      }
    }
  }
}

现在,我正在尝试以回收器视图获取数据。这是我正在使用的代码。

myRef = myfire.getReference().child("Data").child(strUID).child("Budget_Income");
        //-------------------------------

//------------------------------------------
        Toast.makeText(MainActivity.this, "Please Wait a While !", Toast.LENGTH_LONG).show();

//-------------------------
        options1 = new FirebaseRecyclerOptions.Builder<category>()
                .setQuery(myRef, category.class)
                .build();
        final FirebaseRecyclerAdapter<category, holder1> adapter = new FirebaseRecyclerAdapter<category, holder1>(options1) {
            @SuppressLint("SetTextI18n")
            @Override
            protected void onBindViewHolder(@NotNull final holder1 holder, final int i, @NotNull final category passage) {
                final String title = getRef(i).getKey();
                category=title;
                assert title != null;
                myRef = myfire.getReference().child("Data").child(strUID).child("Budget_Income");
                myRef.child(title).addValueEventListener(new ValueEventListener() {

                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if (dataSnapshot.getValue() == null) {
                            Toast.makeText(getApplicationContext(), "Data Not Available", Toast.LENGTH_LONG).show();
                        } else {

                            final String stData1 = (Objects.requireNonNull(dataSnapshot.child("stData1").getValue())).toString();
                            final String stData2 = (Objects.requireNonNull(dataSnapshot.child("stData2").getValue())).toString();
                            final String stData3 = (Objects.requireNonNull(dataSnapshot.child("stData3").getValue())).toString();
                            final String stData4 = (Objects.requireNonNull(dataSnapshot.child("stData4").getValue())).toString();

                            category basic = new category(stData1, stData2, stData3, stData4);
                            holder.tvOne.setText(stData1);
                            holder.tvTwo.setText(stData4);

                        }

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {
                        throw error.toException(); // never ignore errors
                    }

                });

                myRef = myfire.getReference().child("Data").child(strUID).child("Entry_Income").child(title);
                String uid = myRef.push().getKey();
                assert uid != null;
                myRef.child(uid).addValueEventListener(new ValueEventListener() {

                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if (dataSnapshot.getValue() == null) {
                            Toast.makeText(getApplicationContext(), "Data Not Available", Toast.LENGTH_LONG).show();
                        } else {

                            final String stData1 = (Objects.requireNonNull(dataSnapshot.child("stData1").getValue())).toString();
                            final String stData2 = (Objects.requireNonNull(dataSnapshot.child("stData2").getValue())).toString();
                            final String stData3 = (Objects.requireNonNull(dataSnapshot.child("stData3").getValue())).toString();
                            final String stData4 = (Objects.requireNonNull(dataSnapshot.child("stData4").getValue())).toString();

                            category basic = new category(stData1, stData2, stData3, stData4);
                            holder.tvThree.setText(stData3);
                            holder.tvFour.setText(stData4);

                        }

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {
                        throw error.toException(); // never ignore errors
                    }

                });

            }
            @NonNull
            @Override
            public holder1 onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item1, parent, false);
                return new holder1(v);
            }
        };
        userlist.setAdapter(adapter);
        adapter.startListening();

    }

}

我得到了tvone和tvo2的值。但是tv3和tv4没有显示数据。
如果我尝试用实际自动生成的id替换字符串uid,例如“-mykkcyshi93fc0vlnl”,它工作得很好,但只有一个孩子。我需要在“收入”项下的所有节点,如“工资”和“奖金”,以及它们的子节点,如“-mykkcyshi93fc0vlnl”和“-mykkso_z6gmx1q3y9p6”等。我已经厌倦了解决这个问题的尝试。我已经尽力了知识问题存在于以下几个方面lines:-

myfire.getReference().child("Data").child(strUID).child("Entry_Income").child(title);
                String uid = myRef.push().getKey();
                assert uid != null;
                myRef.child(uid).addValueEventListener(new ValueEventListener() {

请转寄我的代码和修改。
类别类
公共类类别{

private String stData1;
private String stData2;
private String stData3;
private String stData4;

public category() {

}

public category(String stData1, String stData2, String stData3, String stData4) {
    this.stData1 = stData1;
    this.stData2 = stData2;
    this.stData3 = stData3;
    this.stData4 = stData4;
}

public String getStData1() {
    return stData1;
}

public void setStData1(String stData1) {
    this.stData1 = stData1;
}

public String getStData2() {
    return stData2;
}

public void setStData2(String stData2) {
    this.stData2 = stData2;
}

public String getStData3() {
    return stData3;
}

public void setStData3(String stData3) {
    this.stData3 = stData3;
}

public String getStData4() {
    return stData4;
}

public void setStData4(String stData4) {
    this.stData4 = stData4;
}

}


* The Code that worked with an error**

 myRef = myfire.getReference().child("Data").child(strUID).child("Entry_Income").child(title);

                            myRef.addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                                    for (DataSnapshot data : dataSnapshot.getChildren()) {
                                        String value = data.child("stData4").getValue(String.class);

                                    }
                                    holder.tvThree.setText(value);

                                }

                                @Override
                                public void onCancelled(@NonNull DatabaseError databaseError) {
                                    throw databaseError.toException(); // never ignore errors
                                }
                            });

错误是什么?
当我向下滚动“回收器”视图时,“回收器”视图的后面的项目没有数据。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题