I have two RecyclerViews inside a LinearLayout in which I'm trying to display Firebase Storage data on one and some Products data queried from Real-Time Database on the other. While I am able to retrieve the data successfully, I have trouble showing it on the RecyclerView(s).
There are 1 of 3 things that happen spontaneously when I try to display the data:
- Case 1: Data does not show on both RecyclerViews OR
- Case 2: Data is shown for the first RecyclerView(displays storage data) OR
- Case 3: Data shows for both (but can sometimes take a verrryy long time to load even though I have only one data each for both).
For the second RecyclerView, I have written the code to query in-app products data from Google Play Console using the billingclient library and I use the data queried from Real-Time DB to see if there is a match in the product IDs. If there is, I display the matching data on the RecyclerView.
While fetching the data is fine, at various times my code gets stuck on setAdapter() and I don't understand why and I don't know how to get it to work properly?
Here's a reference to my current code:
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
FirebaseStorage storage = FirebaseStorage.getInstance();
FirebaseDatabase database = FirebaseDatabase.getInstance();
storageRef = storage.getReference();
dataRef = database.getReference().child("products");
view = inflater.inflate(R.layout.course_fragment, parent, false);
mainLayout = view.findViewById(R.id.mainLayout);
filesRv = view.findViewById(R.id.file_recycler_view);
productRv = view.findViewById(R.id.product_recycler_view);
//list storage folder/files
listFilesFolders();
//check for premium data on real-time database and see if productID matches with productID on Google Play Console (display data on recycler view if it matches)
checkForPremium();
return view;
}
private void checkForPremium() {
setBillingClient(getContext());
connectToGooglePlay();
}
private void listFilesFolders() {
fileStorageRef.listAll()
.addOnSuccessListener(listResult -> {
for (StorageReference folder : listResult.getPrefixes()) {
parentFolder = folder.getName();
contentList.add(fileStorageRef.child(parentFolder));
}
for (StorageReference file : listResult.getItems()) {
fileName = file.getName();
contentList.add(fileStorageRef.child(fileName));
}
setFilesRecyclerView();
})
.addOnFailureListener(e -> {
// Some code here
})
.addOnCompleteListener(task -> {
// Some code here
});
}
private void setFilesRecyclerView() {
if(pageTitle.equals(rootFolder) || contentList.isEmpty()) {
displayFolders();
}
else {
displayFiles();
}
}
private void displayFolders() {
int columnCount = 2;
DisplayMetrics displayMetrics = view.getResources().getDisplayMetrics();
int width = displayMetrics.widthPixels; //get screen width
//if screen width is large, increase grid columns
if(width > 800 && width < 1500){
columnCount = 3;}
else if(width >= 1500 && width < 1800){
columnCount = 4; }
else if(width >= 1800){
columnCount = 5;
}
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), columnCount);
SubFolderAdapter subFolderAdapter = new SubFolderAdapter(getActivity(), contentList);
filesRv.setLayoutManager(gridLayoutManager);
filesRv.setAdapter(subFolderAdapter);
}
private void displayFiles() {
LinearLayoutManager llm1 = new LinearLayoutManager(getActivity());
fileAdapter = new FileAdapter(getActivity(), contentList);
filesRv.setLayoutManager(llm1);
filesRv.setAdapter(fileAdapter);
}
private void displayPremiumData() {
LinearLayoutManager llm2 = new LinearLayoutManager(getActivity());
productsAdapter = new ProductsAdapter3(getActivity(), billingClient, prodDetailsList, path);
productRv.setLayoutManager(llm2);
productRv.setAdapter(productsAdapter);
}
public void setBillingClient(Context context) {
System.out.println("Set billing client");
billingClient = BillingClient.newBuilder(context) //creates instance of billing client
.enablePendingPurchases()
.setListener(this)
.build();
}
public void connectToGooglePlay() {
//Establish a connection to Google Play
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
// The BillingClient is ready. You can query purchases here.
System.out.println("Billing client ready");
listPremium();
} else System.out.println("Billing client NOT ready");
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
System.out.println("Billing service disconnected");
}
});
}
private void listPremium() {
premiumDBRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
cProductsList.clear();
for (DataSnapshot snap: snapshot.getChildren()) {
Products products = snap.getValue(Products.class);
String sku;
String name;
String url;
if (products != null) {
sku = products.getProductId();
name = products.getName();
url = products.getUrl();
if(sku != null) {
skuList.add(sku);
}
} else {
System.out.println("Product does not exist!");
}
}
for(String sku : skuList) {
productList.add(
QueryProductDetailsParams.Product.newBuilder()
.setProductId(sku)
.setProductType(BillingClient.ProductType.INAPP)
.build()
);
}
if (!productList.isEmpty()) {
querySkuDetails(productList);
} else {
System.out.println("No products available");
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
public void querySkuDetails(List<QueryProductDetailsParams.Product> productList) {
QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder()
.setProductList(productList)
.build();
billingClient.queryProductDetailsAsync(
params,
(billingResult, productDetailsList) -> {
// check billingResult
// process returned productDetailsList
if (billingResult.getResponseCode() != BillingClient.BillingResponseCode.OK) {
System.out.printf(Locale.ENGLISH, "Unable to query sku details: %d - %s%n", billingResult.getResponseCode(), billingResult.getDebugMessage());
} else {
for (ProductDetails details: productDetailsList) {
prodDetailsList.add(details);
}
displayPremiumData();
}
});
}
Note: When I run the code, there are no errors. The issue is that the RecyclerView is not working consistently as intended. I have been trying to get this to work but I have been stuck for days now and I would really appreciate some help please!