我得到的只是nullPointerException或数据库中的一项。我想显示所有英语单词,它是阿拉伯语对应单词。我必须使用RecylerView之类的东西吗?我还尝试将{MyWords words = postSnapshot.getValue(MyWords.class)}的输出放入ArrayList中,但效果不佳。救命!

 DatabaseReference myRef = database.getReference("My Words");
 TextView englishView, arabicView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.displaying);
    readData();
}


private void readData() {
    englishView = findViewById(R.id.textView);
    arabicView = findViewById(R.id.arabicView1);

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                MyWords words = postSnapshot.getValue(MyWords.class);

                englishView.setText(words.getEnglish());
                arabicView.setText(words.getArabic());
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError)
        {
             Log.d("TAG" , databaseError.getMessage());
        }
    });
}


我的MyWords类别

private String english;
private String arabic;

public MyWords(String english, String arabic) {
    this.english = english;
    this.arabic = arabic;
}

public MyWords() {
}


public String getEnglish() {
    return english;
}

public void setEnglish(String english) {
    this.english = english;
}

public String getArabic() {
    return arabic;
}

public void setArabic(String arabic) {
    this.arabic = arabic;
}


我的杰森在火上

   {
  "My Words" : {
    "office" : {
      "arabic" : "مكتب",
      "english" : "office"
    },
    "pen" : {
      "arabic" : "الغلم",
      "english" : "pen"
    },
    "teacher" : {
      "arabic" : "مدرسة",
      "english" : "teacher"
    },
    "university" : {
      "arabic" : "الجميع",
      "english" : "university"
    }
  }
}

最佳答案

我对您的代码进行了很少的更改,并在其中对错误进行了注释。在textbox变量中,每次都要覆盖内容,因为在for循环内调用了textVeiw.setContent()函数。

DatabaseReference myRef = database.getReference("My Words");
TextView englishView, arabicView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.displaying);
    readData();
}
private void readData() {
    englishView = findViewById(R.id.textView);
    arabicView = findViewById(R.id.arabicView1);
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                MyWords words = postSnapshot.getValue(MyWords.class);

                //here you were overwritting the content of the textbox everytime.
                englishView.setText( englishView.getText()+" "+ words.getEnglish());
                arabicView.setText(arabicView.getText()+" " +words.getArabic());
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.d("TAG" , databaseError.getMessage());
        }
    });
}


或者,也可以使用MyWords类型的arraylist并存储整个json,然后可以将arraylist放在textView中。

关于java - 我如何将Firebase Realtime Database中的所有数据显示到TextView中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59861250/

10-15 03:44