简体   繁体   中英

RecyclerView not shown Data before pressing back button

Guys I am working with firebase realtime database.. I create search tolbar with search button to displays data process with recycler view .data can appear as it should, but I have problem the data is not being displayed unless i press back button. If i don't press back button data not shown .I want the data to show after hitting enter or When Click search button ,data shown without press back button

searchactivity.java

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_movies_series);
       
        Typeface font2 = Typeface.createFromAsset(getAssets(), "fonts/NunitoBold.ttf");
        SS = new SpannableStringBuilder(getString(R.string.search));
        SS.setSpan(new CustomTypefaceSpan("NunitoBold", font2), 0, SS.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        getSupportActionBar().setTitle(SS);

        dbMovies = FirebaseDatabase.getInstance().getReference().child("Movies");
        dbSeries = FirebaseDatabase.getInstance().getReference().child("Series");
        dbtvChannels = FirebaseDatabase.getInstance().getReference().child("Channels");

        recyclerView = findViewById(R.id.rcvSearch);
        recyclerView.setHasFixedSize(true);

        mLayoutManager = new GridLayoutManager(SearchActivity.this,3
                , LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(mLayoutManager);

        lnSearch=findViewById(R.id.lnSearch);
        edtSearch=findViewById(R.id.edtSearch);
        rbMovie=findViewById(R.id.rbMovie);
        rbSerie=findViewById(R.id.rbSerie);
        lnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(!TextUtils.isEmpty(edtSearch.getText().toString())) {

                     if (rbMovie.isChecked())
                        showList(dbMovies, edtSearch.getText().toString());
                    else if (rbSerie.isChecked())
                        showList(dbSeries, edtSearch.getText().toString());
                    else
                        Toast.makeText(SearchActivity.this, getString(R.string.chk), Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(SearchActivity.this, getString(R.string.xsearc), Toast.LENGTH_SHORT).show();
                }

            }
        });
    }


    private void showList(final DatabaseReference data,String edt) {

        final Query qrMyimages = data.orderByChild("name").startAt(edt.toUpperCase()).endAt(edt.toLowerCase()+"\uf8ff");
        firebaseRecyclerAdapterList= new FirebaseRecyclerAdapter<Genre, latestMoviesViewHolder>(
                Genre.class,
                R.layout.layout_movies_ver,
                latestMoviesViewHolder.class,
                qrMyimages
        ) {

            @Override
            protected void populateViewHolder(final latestMoviesViewHolder viewHolder, final Genre model, int position) {

                final String listPostKey = getRef(position).getKey();

                data.child(listPostKey).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {


                        String thumb_picture = String.valueOf(dataSnapshot.child("imageurl").getValue());
                        final String movieName = String.valueOf(dataSnapshot.child("name").getValue());
                        final String release_date = String.valueOf(dataSnapshot.child("release_date").getValue());

                        viewHolder.setImage(thumb_picture);

                        viewHolder.tvMovieName.setText(movieName);
                        viewHolder.tvMovieYear.setText(release_date);

                        viewHolder.imgvMovie.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                if(data.getKey().equals("Movies")){
                                    Intent movie = new Intent(SearchActivity.this, DetailsActivity.class);
                                    movie.putExtra("id_watch", listPostKey);
                                    movie.putExtra("key_watch", "movies");
                                    movie.putExtra("name_watch", viewHolder.tvMovieName.getText().toString());
                                    startActivity(movie);
                                }else
                                    if(data.getKey().equals("Series")){
                                        Intent movie = new Intent(SearchActivity.this, DetailsActivity.class);
                                        movie.putExtra("id_watch", listPostKey);
                                        movie.putExtra("key_watch", "series");
                                        movie.putExtra("name_watch", viewHolder.tvMovieName.getText().toString());
                                        startActivity(movie);
                                    }else{
                                        Intent movie = new Intent(SearchActivity.this, DetailsActivity.class);
                                        movie.putExtra("id_watch", listPostKey);
                                        movie.putExtra("key_watch", "channels");
                                        movie.putExtra("name_watch", viewHolder.tvMovieName.getText().toString());
                                        startActivity(movie);
                                    }

                            }
                        });

                    }

You should set the firebaseRecyclerAdapterList as the recyclerView adapter after you create the new FirebaseRecyclerAdapter instance.

Inside the function showList and after the FirebaseRecyclerAdapter decleration simply add:

recyclerView.setAdapter(firebaseRecyclerAdapterList)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM