Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
99 views
in Technique[技术] by (71.8m points)

android - Load all Data of the subclass of Firebase

What I mean by the Question is : I'am making an App where you can watch Animes and Movies on an app in which I get the data from firebase to Android app, the data loads perfectly. But the problem is the clickListener, you see that when I click a button that for example loads an Anime and then Anime has around 100 episodes. So how am I suppose to load all 100 episodes, considering that there will other Animes that have above 100 or below 100 episodes.

enter image description here

Check the image, so the "Movie1" has 3 Episodes, but the "Movie2" has 4 episodes.

enter code here

public class SearchView extends AppCompatActivity {

RecyclerView mRecylerView;
FirebaseDatabase mfirebaseDatabase;
DatabaseReference mRef;

private String  mTitle;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_dashboard);


    BottomNavigationView navView = findViewById(R.id.nav_view);

    navView.setSelectedItemId(R.id.navigation_dashboard);

    navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

            switch (menuItem.getItemId()) {
                case R.id.navigation_dashboard:

                    return  true;
                case R.id.navigation_home:
                    startActivity(new Intent(getApplicationContext(), MainActivity.class));
                    overridePendingTransition(0,0);
                    return  true;
                case R.id.navigation_notifications:
                    startActivity(new Intent(getApplicationContext(), Coming_Soon.class));
                    overridePendingTransition(0,0);
                    return  true;
            }

            return false;
        }
    });




    ActionBar actionBar = getSupportActionBar();
    actionBar.setTitle("Post lists");

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

    mRecylerView.setLayoutManager(new LinearLayoutManager(this));

    mfirebaseDatabase = FirebaseDatabase.getInstance();
    mRef = mfirebaseDatabase.getReference("Movies");


}

private void firebaseSearch(String searchText) {




    Query firebaseSearchQuery = mRef.orderByChild("search").startAt(searchText.toLowerCase()).endAt(searchText + "uf8ff");

    FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<Model, ViewHolder>(
                    Model.class,
                    R.layout.row,
                    ViewHolder.class,
                    firebaseSearchQuery
            ) {
                @Override
                protected void populateViewHolder(ViewHolder viewHolder, Model model, int i) {

                    viewHolder.setDetails(getApplicationContext(), model.getTitle(), model.getImage());
                }
                @Override
                public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

                    ViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);

                    viewHolder.setOnClickListener(new ViewHolder.ClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {

                            Intent intent= new Intent(SearchView.this, DetailActivity.class);
                            intent.putExtra("CarKey", getRef(position).getKey());
                            startActivity(intent);

                        }

                        @Override
                        public void onItemLongClick(View view, int position) {

                        }
                    });

                    return viewHolder;
                }
            };

    mRecylerView.setAdapter(firebaseRecyclerAdapter);
}

@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<Model, ViewHolder>(
                    Model.class,
                    R.layout.row,
                    ViewHolder.class,
                    mRef
            ) {
                @Override
                protected void populateViewHolder(ViewHolder viewHolder, Model model, int i) {
                    viewHolder.setDetails(getApplicationContext(), model.getTitle(), model.getImage());
                }

                @Override
                public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

                    ViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);

                    viewHolder.setOnClickListener(new ViewHolder.ClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            Intent intent= new Intent(SearchView.this, DetailActivity.class);
                            intent.putExtra("CarKey", getRef(position).getKey());
                            startActivity(intent);


                        }

                        @Override
                        public void onItemLongClick(View view, int position) {

                        }
                    });

                    return viewHolder;
                }
            };

    mRecylerView.setAdapter(firebaseRecyclerAdapter);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    MenuItem item = menu.findItem(R.id.action_search);
    android.widget.SearchView searchView = (android.widget.SearchView) MenuItemCompat.getActionView(item);
    searchView.setOnQueryTextListener(new android.widget.SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            firebaseSearch(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            firebaseSearch(newText);
            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int id =  item.getItemId();

    if(id == R.id.action_setting) {
        // TODO
        return  true;
    }

    return super.onOptionsItemSelected(item);
}

}

enter code here

public class DetailActivity extends AppCompatActivity {

private Button episodes;
private ImageView imageView;
TextView textView;
DatabaseReference ref;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail_activity);


    imageView = findViewById(R.id.slide_img);
    textView = findViewById(R.id.titleTv);
    episodes = findViewById(R.id.episodes);

    ref = FirebaseDatabase.getInstance().getReference().child("Movies");

    String CarKey = getIntent().getStringExtra("CarKey");
    ref.child(CarKey).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                


                String title = dataSnapshot.child("title").getValue().toString();
                String image = dataSnapshot.child("image").getValue().toString();


                Picasso.get().load(image).into(imageView);
                textView.setText(title);




            }
        }


        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


}

}

question from:https://stackoverflow.com/questions/65879497/load-all-data-of-the-subclass-of-firebase

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...