'why this code isn't working in real-time pleace cheack this code [closed]

this code isn't working in real-time.. it works only whenever I restart/refresh the app. I am using Cloud Firestore. plzz change code am noob help me. & sorry For My bad English

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 public class Category_List_Activity extends BaseActivity {
    
        List<CardModel> list = new ArrayList<>();
        RecyclerView recyclerView;
        RecyclerAdapter adapter;
        android.app.AlertDialog dialog;
        private static final String TAG = "CategoryListActivity";
        FirebaseFirestore db;
        String name, id,node,lastid,time;
        boolean islast;
        SessionManager sessionManager;
        LinearLayout banner;
        private InterstitialAd interstitial;
        TextView settitel;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.category_list);
            FirebaseApp.initializeApp(this);
            db = FirebaseFirestore.getInstance();
            Bundle bundle = getIntent().getExtras();
            banner = findViewById(R.id.banner_bottom_main);
            sessionManager=new SessionManager(this);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
    
            StartAppAd.showAd(this);
    
            new AdsHelper().createBanner(this, banner, sessionManager.getBanner());
    
            if (bundle != null) {
                name = bundle.getString("name");
                id = bundle.getString("id");
                lastid = bundle.getString("lastid");
                node=bundle.getString("node");
                islast=bundle.getBoolean("islast");
            }
    ///////////////////////////Action bar custiom//////////////////////////////////////////
            ImageView imgFavorite = (ImageView) findViewById(R.id.previous);
            imgFavorite.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
    
            ImageView imgFavorsite = (ImageView) findViewById(R.id.refrashing);
            imgFavorsite.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getData();
                }
            });
    
    
            settitel = findViewById(R.id.event_name);
            settitel.setText(name);
    ///////////////////////Action bar custiom end///////////////////////
    
    
            recyclerView = findViewById(R.id.rec);
            adapter = new RecyclerAdapter(list, this);
            recyclerView.setHasFixedSize(true);
            AutoFitGridLayoutManager layoutManager = new AutoFitGridLayoutManager(this, 10000);
            recyclerView.setLayoutManager(layoutManager);
    //        recyclerView.setLayoutManager(new GridLayoutManager(this,1));
    
            recyclerView.setAdapter(adapter);
    
    
            getData();
    
            if(isCastApiAvailable()) {
                castContext = CastContext.getSharedInstance(this);
                if (castContext.getCastState() == CastState.CONNECTED) {
                    findViewById(R.id.castMiniController).setVisibility(View.VISIBLE);
                }else{
                    findViewById(R.id.castMiniController).setVisibility(View.GONE);
                }
            }else{
                findViewById(R.id.castMiniController).setVisibility(View.GONE);
            }
    
    
    
        }
    
        CastContext castContext;
    
    
        @Override
        protected void onResume() {
            super.onResume();
    
            try {
                if (HelperClass.vpn(this)) {
                    finish();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
    
        }
    
    
    
        @Override
        public boolean onCreateOptionsMenu(@NonNull Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    
        @Override
        public boolean onOptionsItemSelected(@NonNull MenuItem item) {
            if(item.getItemId() == R.id.refrish){
                getData();
                return true;
            }
    
            else if (item.getItemId() == android.R.id.home) {
                onBackPressed();
            }
    
            return super.onOptionsItemSelected(item);
        }
    
    
    //    @Override
    //    public boolean onOptionsItemSelected(MenuItem item) {
    //
    //        if (item.getItemId() == android.R.id.home) {
    //            onBackPressed();
    //        }
    //
    //        return true;
    //    }
    
        //////////////////sports diglo start///////////////
        @Override
        public void onDestroy() {
            dismissProgressDialog();
            super.onDestroy();
        }
        private void showProgressDialog() {
            if (dialog == null) {
                dialog = new SpotsDialog.Builder().setContext(this).build();
                dialog.setMessage("Please wait...");
                dialog.setCancelable(false);
            }
            dialog.show();
        }
    
        private void dismissProgressDialog() {
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    //////////////////sports diglo end///////////////
    
    
    
    
        private void getData() {
            showProgressDialog();
            Query query;
            if (islast) {
                query = db.collection(node)
                        .document(lastid)
                        .collection("list")
                        .document(id)
                        .collection("list");
            } else {
                query = db.collection(node)
                        .document(id)
                        .collection("list");
            }
    
            query.get()
                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            dismissProgressDialog();
                            if (task.isSuccessful()) {
    
                                list.clear();
                                int i = 0;
                                boolean match = false;
                                try {
                                    for (DocumentSnapshot snapshot : task.getResult().getDocuments()) {
                                        CardModel mainModel = snapshot.toObject(CardModel.class);
                                        mainModel.setId(snapshot.getId());
                                        if (mainModel.enable) {
                                            boolean onOff=mainModel.cOnOff;
                                            if (mainModel.countries != null
                                                    && mainModel.countries.size() > 0) {
                                                if (!sessionManager.getLocation().isEmpty()) {
                                                    if(onOff) {
                                                        if (mainModel.countries.contains(sessionManager.getLocation())) {
                                                            match = true;
                                                            list.add(mainModel);
                                                        } else {
                                                            list.remove(mainModel);
                                                        }
    
                                                    }else{
                                                        if (mainModel.countries.contains(sessionManager.getLocation())) {
                                                            match = true;
                                                            list.remove(mainModel);
                                                        } else {
                                                            list.add(mainModel);
                                                        }
                                                    }
                                                } else {
                                                    list.add(mainModel);
                                                }
                                            } else {
                                                list.add(mainModel);
                                            }
                                        } else {
                                            list.remove(mainModel);
                                        }
                                        Log.d("view imgs", "data is " + mainModel.toString());
    
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                adapter.notifyDataSetChanged();
    
                            }
                            adapter.notifyDataSetChanged();
                            dismissProgressDialog();
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                          dismissProgressDialog();
                            Toast.makeText(Category_List_Activity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    })
            ;
        }
    
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source