1 year ago

#133857

test-img

aAaDesigner

Problem with Recyclerview not always loading items

Greetings to all the experts.

I have the following problem, I have implemented Billing Subscriptions in Android, the code works well, the problem is that the Recyclerview sometimes loads and many others it doesn't, sometimes it loads first, sometimes you have to enter the atricity a few times, I don't understand why it makes that bug, if you could check the code and guide me I would appreciate it

suscription.java

public class suscripcion extends AppCompatActivity implements PurchasesUpdatedListener {

    private TextView statuspremium, rangopremium;

    private BillingClient billingClient;
    private AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener;
    private RecyclerView recyclerView_sub;
    private DocumentReference documentReference;
    private FirebaseFirestore mFirestore;
    private FirebaseUser user;
    private FirebaseAuth fAuth;
    private String userID;
    private MySubAdapter adapter;
    private Button cargarpremium;
    private ImageView ayudapremium, txtclose;
    private String status;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_suscripcion);
        this.setFinishOnTouchOutside(false);

        mFirestore = FirebaseFirestore.getInstance();
        fAuth = FirebaseAuth.getInstance();
        user = fAuth.getCurrentUser();
        ayudapremium = findViewById(R.id.ayudapremium);


        ayudapremium.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(suscripcion.this, ofrecepremium.class));
            }
        });

        if (user !=null)
        {
            userID = fAuth.getCurrentUser().getUid();
        }

        documentReference = mFirestore.collection("users").document(userID);
        documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {

                status = value.getString("status");

            }
        });

        setupSubClient();
        iniciador();


        txtclose = findViewById(R.id.txtclosesub);
        txtclose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                documentReference.update("premiumverificar", "no");
                finish();
                startActivity(new Intent(suscripcion.this, MainActivity.class));
            }
        });
    }

    private void iniciador() {

        statuspremium = findViewById(R.id.statuspremium);
        rangopremium = findViewById(R.id.rangopremium);
        recyclerView_sub = findViewById(R.id.recyclerview_sub);
        recyclerView_sub.setHasFixedSize(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView_sub.setLayoutManager(layoutManager);
        recyclerView_sub.addItemDecoration(new DividerItemDecoration(this, layoutManager.getOrientation()));
        cargarpremium = findViewById(R.id.cargarpremium);

        cargarpremium.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                startActivity(getIntent());
            }
        });

    }

    private void loadAllSubPackage() {

        if (billingClient.isReady())
        {
            Log.d("ERROR20", "onSkuDetailsResponse: ");
            SkuDetailsParams params = SkuDetailsParams
                    .newBuilder()
                    .setSkusList(Arrays.asList("vf_sub_unmes", "vf_sub_tresmes"))
                    .setType(BillingClient.SkuType.SUBS)
                    .build();
            billingClient.querySkuDetailsAsync(params, new SkuDetailsResponseListener() {
                @Override
                public void onSkuDetailsResponse(@NonNull BillingResult billingResult, @Nullable List<SkuDetails> list) {

                    if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK)

                    {

                            adapter = new MySubAdapter(suscripcion.this, list, billingClient);
                            recyclerView_sub.setAdapter(adapter);
                            recyclerView_sub.setVisibility(View.VISIBLE);



                        Log.d("ERROR5", "onSkuDetailsResponse: " + billingResult.getResponseCode());

                    }else
                    {
                        Log.d("ERROR6", "onSkuDetailsResponse: " + billingResult.getResponseCode());

                    }
                }
            });
        }else
        {
            loadAllSubPackage();
            Log.d("ERROR7", "loadAllSubPackage: Billing cliente not ready" );

        }

    }

    private void setupSubClient() {
        acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener() {
            @Override
            public void onAcknowledgePurchaseResponse(@NonNull BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK)
                {
                    Log.d("ERROR1", "onAcknowledgePurchaseResponse: " +
                            billingResult.getResponseCode());
//                    rangopremium.setVisibility(View.VISIBLE);
//                    tiemposub.setVisibility(View.VISIBLE);
                }else
                {
                    Log.d("ERROR21", "onAcknowledgePurchaseResponse: " +
                            billingResult.getResponseCode());
                }
            }
        };

        billingClient = BillingClientSetup.getInstance(this, this);
        billingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingServiceDisconnected() {
                setupSubClient();
                Log.d("ERROR2", "onBillingServiceDisconnected: " );


            }

            @Override
            public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK)
                {
                    Log.d("ERROR3", "onBillingSetupFinished: " +
                            billingResult.getResponseCode());

//                    //Query
//                    List<Purchase> purchases = billingClient.queryPurchases(BillingClient.SkuType.SUBS)
//                            .getPurchasesList();

                    billingClient.queryPurchasesAsync(BillingClient.SkuType.SUBS, new PurchasesResponseListener() {
                        @Override
                        public void onQueryPurchasesResponse(@NonNull BillingResult billingResult, @NonNull List<Purchase> list) {

                            if (list.size() > 0)
                            {

                                for (Purchase purchase:list)

                                    handleSubAlreadyPurchase(purchase);

                                Log.d("ERROR16", "onBillingSetupFinished: " +
                                        billingResult.getResponseCode());

                            }else
                            {

                                loadAllSubPackage();
                                documentReference.update("status", "user");
                                statuspremium.setVisibility(View.VISIBLE);


                                Log.d("ERROR17", "onBillingSetupFinished: " +
                                        billingResult.getResponseCode());
//                            rangopremium.setVisibility(View.GONE);
//                            tiemposub.setVisibility(View.GONE);
//                            statuspremium.setVisibility(View.VISIBLE);
//                            recyclerView_sub.setVisibility(View.VISIBLE);

                            }

                        }
                    });






                }
                else
                {
                    Log.d("ERROR4", "onBillingSetupFinished: " +
                            billingResult.getResponseCode());

                }
            }
        });
    }


    private void handleSubAlreadyPurchase(Purchase purchases) {

        if (purchases.getPurchaseState() == Purchase.PurchaseState.PURCHASED)
        {


            documentReference = mFirestore.collection("users").document(userID);
            documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
                @Override
                public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {

                    if (value != null && value.exists()) {
                        status = value.getString("status");
                        rangopremium.setVisibility(View.VISIBLE);
                        rangopremium.setText(status);
                    }
                }
            });


            Log.d("ERROR11", "handleSubAlreadyPurchase: ");
            if (!purchases.isAcknowledged())
            {
                Log.d("ERROR12", "handleSubAlreadyPurchase: ");
                AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
                        .setPurchaseToken(purchases.getPurchaseToken())
                        .build();
                billingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
            }else
            {
                Log.d("ERROR13", "handleSubAlreadyPurchase: ");

            }
        }
        if (purchases.getPurchaseState() == Purchase.PurchaseState.UNSPECIFIED_STATE){
            Log.d("ERROR14", "handleSubAlreadyPurchase: ");
        }

    }

    @Override
    public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List<Purchase> list) {

        if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && list !=null)
        {

            SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss", Locale.getDefault());
            String currentDateandTime = formatter.format(new Date());

            documentReference = mFirestore.collection("users").document(userID);
            documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
                @Override
                public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {

                    if (value != null && value.exists()){
                        status = value.getString("status");
                        rangopremium.setVisibility(View.VISIBLE);
                        rangopremium.setText(status);
                    }


                }
            });


            statuspremium.setVisibility(View.GONE);
            recyclerView_sub.setVisibility(View.GONE);
            documentReference.update("status", "PREMIUM");
            documentReference.update("bloqueo", 3);
            documentReference.update("premiumfecha", currentDateandTime);
            Log.d("ERROR8", "onPurchasesUpdated: " + billingResult.getResponseCode());

            for (Purchase purchase:list)

                handleSubAlreadyPurchase(purchase);


        }else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED)
        {
            Log.d("ERROR9", "onPurchasesUpdated: " + billingResult.getResponseCode());
            rangopremium.setVisibility(View.GONE);
            statuspremium.setVisibility(View.VISIBLE);
            recyclerView_sub.setVisibility(View.VISIBLE);
            documentReference.update("status", "user");
        }else
        {
            Log.d("ERROR10", "onPurchasesUpdated: " + billingResult.getResponseCode());

        }

    }

    @Override
    public void onBackPressed() {

    }


}

MySubAdapter.java

public class MySubAdapter extends RecyclerView.Adapter<MySubAdapter.MyViewHolder> {

    AppCompatActivity appCompatActivity;
    List<SkuDetails> skuDetailsList;
    BillingClient billingClient;

    public MySubAdapter(AppCompatActivity appCompatActivity, List<SkuDetails> skuDetailsList, BillingClient billingClient) {
        this.appCompatActivity = appCompatActivity;
        this.skuDetailsList = skuDetailsList;
        this.billingClient = billingClient;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(appCompatActivity.getBaseContext())
                .inflate(R.layout.listado_sub, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        holder.txt_sub_name.setText(skuDetailsList.get(position).getTitle());
        holder.txt_precio_sub.setText(skuDetailsList.get(position).getPrice());

        holder.setListener(new IReciclerClickListener() {
            @Override
            public void onClick(View view, int position) {
                BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
                        .setSkuDetails(skuDetailsList.get(position))
                        .build();
                int reponse = billingClient.launchBillingFlow(appCompatActivity, billingFlowParams)
                        .getResponseCode();

                switch(reponse)
            {
                case BillingClient.BillingResponseCode.BILLING_UNAVAILABLE:
                    Log.d("ERROR11", "BILLING_UNAVAILABLE");
                    break;
                case BillingClient.BillingResponseCode.DEVELOPER_ERROR:
                    Log.d("ERROR12", "DEVELOPER_ERROR");

                    break;
                case BillingClient.BillingResponseCode.FEATURE_NOT_SUPPORTED:
                    Log.d("ERROR13", "FEATURE_NOT_SUPPORTED");

                    break;
                case BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED:
                    Log.d("ERROR14", "ITEM_ALREADY_OWNED");

                    break;
                case BillingClient.BillingResponseCode.SERVICE_DISCONNECTED:
                    Log.d("ERROR15", "SERVICE_DISCONNECTED");

                    break;
                case BillingClient.BillingResponseCode.SERVICE_TIMEOUT:
                    Log.d("ERROR16", "SERVICE_TIMEOUT");

                    break;
                case BillingClient.BillingResponseCode.ITEM_UNAVAILABLE:
                    Log.d("ERROR17", "ITEM_UNAVAILABLE");

                    break;
                default:
                    break;
            }
            }
        });

    }

    @Override
    public int getItemCount() {
        return skuDetailsList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView txt_sub_name, txt_precio_sub;
        IReciclerClickListener listener;

        public void setListener(IReciclerClickListener listener) {
            this.listener = listener;
        }

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            txt_sub_name = itemView.findViewById(R.id.txt_sub_name);
            txt_precio_sub = itemView.findViewById(R.id.txt_precio_sub);

            itemView.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {

            listener.onClick(v, getBindingAdapterPosition());

        }
    }
}

activity_suscription

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/Snackbar_apoyo"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/fondocurva"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:gravity="center">

    <ImageView
        android:contentDescription="@string/app_name"
        android:id="@+id/txtclosesub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_close_black"
        android:layout_gravity="start"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:gravity="center"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/statuspremium"
            style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:gravity="center"
            android:shadowColor="@color/negro"
            android:shadowDx="2"
            android:shadowDy="5"
            android:visibility="gone"
            android:shadowRadius="10"
            android:text="@string/no_premium"
            android:textColor="@color/colorAccent"
            android:textSize="25sp"
            android:textStyle="bold|italic" />

        <com.hanks.htextview.rainbow.RainbowTextView
            android:id="@+id/rangopremium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:visibility="gone"
            android:textSize="25sp"
            android:textStyle="bold"
            app:colorSpace="150dp"
            app:colorSpeed="10dp"/>

        <ImageView
            android:contentDescription="@string/app_name"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:src="@drawable/vinoayuda"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/haste_premium"
            android:textSize="18sp"
            android:textColor="@color/colorAccent"
            android:layout_gravity="center"
            android:gravity="center"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:layout_marginBottom="20dp"
            android:textStyle="bold"
            android:fontFamily="sans-serif-smallcaps"
            android:shadowColor="@color/negro"
            android:shadowRadius="10"
            android:shadowDx="2"
            android:shadowDy="5"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:gravity="center"
            android:layout_gravity="center"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/que_ofrece_el_premium"
                android:textSize="16sp"
                android:textColor="@color/blanco"
                android:layout_gravity="center"
                android:gravity="center"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:textStyle="bold"
                android:fontFamily="sans-serif-smallcaps" />


            <ImageView
                android:id="@+id/ayudapremium"
                android:background="@drawable/curvas"
                android:contentDescription="@string/app_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginEnd="10dp"
                android:src="@drawable/ic_baseline_help"/>

        </LinearLayout>

        <Button
            android:id="@+id/cargarpremium"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="@string/cargar_botones"
            android:padding="10dp"
            android:textColor="@color/colorPrimary"
            android:background="@drawable/btn_desing"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview_sub"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_marginBottom="20dp">

        </androidx.recyclerview.widget.RecyclerView>



    </LinearLayout>


</LinearLayout>

PD: I have placed a button to reload the Activity, and only then sometimes it loads and sometimes it doesn't, just to half solve that bug, but the ideal is that the recyclerview loads me the first time

android-recyclerview

android-billing

0 Answers

Your Answer

Accepted video resources