Issue
What I want is to combine my Categories and Translations tables while creating a data request from the database. or just to write the TextContentIDForName value with the Translation value in my Categories table.
I never want to make any changes in my database. I just want to have it show me the translation texts when I want to write my categories on my page. I designed my database schema in this way, as I knew that the translated texts should be kept in a separate table.
First of all I can't find any property that joins these two tables or looks like this property.
In short, I have a table for my categories and a translation table holding each language. Apart from these two tables, I have a TextContent table that I created to give IDs to the posts.
My query to pull all categories looks like this.
public void getAllCategories() {
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference categoriesRef = db.getReference("categories");
DatabaseReference TranslationsRef = db.getReference("translations");
ArrayList<Categories> categoriesArrayList = new ArrayList<>();
Query query = categoriesRef.orderByChild("parentID").equalTo("categories");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
categoriesArrayList.clear();
for (DataSnapshot data : snapshot.getChildren()) {
Categories categories = data.getValue(Categories.class);
assert categories != null;
categories.setKey(data.getKey());
categoriesArrayList.add(categories);
}
categoriesAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
If I want to add translation texts to my query of all categories.
Creating two queries for the two tables and copying them into a common model would have helped me see all the information, but it's not very flexible. I need to create an extra model to join two tables.
public void getAllCategories() {
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference categoriesRef = db.getReference("categories");
DatabaseReference TranslationsRef = db.getReference("translations");
Query query = categoriesRef.orderByChild("parentID").equalTo("categories");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
categoriesArrayList.clear();
for (DataSnapshot data : snapshot.getChildren()) {
CategoriesAndTranslations categoriesAndTranslations = data
.getValue(CategoriesAndTranslations.class);
assert categoriesAndTranslations != null;
categoriesAndTranslations.setKey(data.getKey());
categoriesArrayList.add(categoriesAndTranslations);
System.out.println("1234" + categoriesAndTranslations);
String textContentID = categoriesAndTranslations.getTextContentID();
Query query = TranslationsRef.orderByChild("textContentID")
.equalTo(textContentID);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot data : snapshot.getChildren()) {
CategoriesAndTranslations categoriesAndTranslations = data
.getValue(CategoriesAndTranslations.class);
System.out.println("123" + categoriesAndTranslations);
categoriesArrayList.add(categoriesAndTranslations);
}
categoriesAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
categoriesAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
All data modeled by this method will have a null value. I do not know why it happened.
Solution
As it is difficult to use a common pattern, after fetching my entire Categories table, I tried to replace the textContentIDForName with the translation in my Translations table.
public void getAllCategories() {
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference categoriesRef = db.getReference("categories");
DatabaseReference TranslationsRef = db.getReference("translations");
Query query = categoriesRef.orderByChild("parentID").equalTo("categories");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
categoriesArrayList.clear();
for (DataSnapshot data : snapshot.getChildren()) {
Categories categories = data
.getValue(Categories.class);
assert categories != null;
categories.setKey(data.getKey());
categoriesArrayList.add(categories);
System.out.println("1234" + categories);
String textContentID = categories.getTextContentIDForName();
Query query = TranslationsRef.orderByChild("textContentID")
.equalTo(textContentID);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot data : snapshot.getChildren()) {
Translations translations = data.getValue(Translations.class);
System.out.println("123" + translations);
categories.setTextContentIDForName(translations.getTranslation());
}
categoriesAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
This method is a solution and it works well.
Now the textContentIDForName value has been replaced with the Translation value from Translations. This way i was able to fetch the translation text.
Answered By - Droidbane
Answer Checked By - Clifford M. (JavaFixing Volunteer)