0

I have created a movie app the app is almost complete but there is one shortcoming where if users click one particular category they have to see all the details of that particular category. I'm new to android so I don't know how to implement this. one of them told we can use sql fetch but i have used retro fit so i dont want to change my backend from json to sql .

I have tried a similar question but did not work uploading my main activity, adapter model class and JSON file data Mainacivity.Java

package com.example.movieapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Filter;

import com.example.movieapp.adapter.BannerMoviesPagerAdapter;
import com.example.movieapp.adapter.ItemRecyclerAdapter;
import com.example.movieapp.adapter.MainRecyclerAdapter;
import com.example.movieapp.listener.OnItemClickLIstener;
import com.example.movieapp.model.AllCategory;
import com.example.movieapp.model.BannerMovies;
import com.example.movieapp.model.CategoryItemList;
import com.example.movieapp.retrofit.RetrofitClient;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;

public class MainActivity extends AppCompatActivity  {

    BannerMoviesPagerAdapter bannerMoviesPagerAdapter;
    TabLayout IndicatorTab,categoryTab;
    ViewPager bannerMoviesViewpager;
    List<BannerMovies> homeBannerList;
    List<BannerMovies> tvBannerList;
    List<BannerMovies> songBannerList;
    List<BannerMovies> audioBannerList;
    NestedScrollView nestedScrollView;
    AppBarLayout appBarLayout;

    MainRecyclerAdapter mainRecyclerAdapter;
    ItemRecyclerAdapter itemRecyclerAdapter;
    RecyclerView mainRecycler;
    List<AllCategory> allCategoryList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Adding this line will prevent taking screenshot in your app
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
                WindowManager.LayoutParams.FLAG_SECURE);
        IndicatorTab =findViewById(R.id.tab_indicator);
        categoryTab =findViewById(R.id.tabLayout);
        nestedScrollView =findViewById(R.id.nested_scroll);
        appBarLayout =findViewById(R.id.appbar);
        homeBannerList=new ArrayList<>();
        tvBannerList=new ArrayList<>();
        songBannerList=new ArrayList<>();
        audioBannerList=new ArrayList<>();
        getBannerData();
        getAllMoviesData(1);




        categoryTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()){
                    case 1:

                        setBannerMoviesPagerAdapter(tvBannerList);
                        getAllMoviesData(2);



                        return;
                    case 2:

                        setBannerMoviesPagerAdapter(songBannerList);
                        getAllMoviesData(3);

                        return;

                        case 3:

                            setBannerMoviesPagerAdapter(audioBannerList);
                            getAllMoviesData(4);
                          return;
                    default:

                        setBannerMoviesPagerAdapter(homeBannerList);
                        getAllMoviesData(1);


                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


        //titles
        allCategoryList=new ArrayList<>();


    }

    private void setBannerMoviesPagerAdapter(List<BannerMovies> bannerMoviesList) {
        bannerMoviesViewpager = findViewById(R.id.banner_viewPager);
        bannerMoviesPagerAdapter = new BannerMoviesPagerAdapter(this, bannerMoviesList);
        bannerMoviesViewpager.setAdapter(bannerMoviesPagerAdapter);
        IndicatorTab.setupWithViewPager(bannerMoviesViewpager);


    }


public void setMainRecycler(List<AllCategory>allCategoryList){
        mainRecycler=findViewById(R.id.main_recycler);
        RecyclerView.LayoutManager layoutManager= new LinearLayoutManager(this,RecyclerView.VERTICAL,false);
mainRecycler.setLayoutManager(layoutManager);
mainRecyclerAdapter =new MainRecyclerAdapter(this,allCategoryList);
mainRecycler.setAdapter(mainRecyclerAdapter);
    }
    private void getBannerData()
    {
        CompositeDisposable compositeDisposable = new CompositeDisposable();
        compositeDisposable.add(RetrofitClient.getRetroFitClient().getAllBanners()
                                 .subscribeOn(Schedulers.io())
                                 .observeOn(AndroidSchedulers.mainThread())
                                     .subscribeWith(new DisposableObserver<List<BannerMovies>>() {
                                         @Override
                                         public void onNext(List<BannerMovies> bannerMovies) {
                                            for ( int i=0; i < bannerMovies.size(); i++ )
                                            {
                                                if(bannerMovies.get(i).getBannerCategoryId().toString().equals("1"))
                                                {
                                                    homeBannerList.add(bannerMovies.get(i));

                                                } else if (bannerMovies.get(i).getBannerCategoryId().toString().equals("2"))
                                                {
                                                    tvBannerList.add(bannerMovies.get(i));

                                                }  else if (bannerMovies.get(i).getBannerCategoryId().toString().equals("3"))
                                                {
                                                    songBannerList.add(bannerMovies.get(i));

                                                } else if  (bannerMovies.get(i).getBannerCategoryId().toString().equals("4"))
                                                {
                                                    audioBannerList.add(bannerMovies.get(i));

                                                }
                                                else
                                                {
                                                    homeBannerList.add(bannerMovies.get(i));
                                                }



                                            }
                                         }

                                         @Override
                                         public void onError(Throwable e) {
                                             Log.d("bannerData",""+e);
                                         }

                                         @Override
                                         public void onComplete() {
                                             setBannerMoviesPagerAdapter(homeBannerList);
                                         }
                                     })

        );
    }

    private void getAllMoviesData(Integer categoryId)
    {
        CompositeDisposable compositeDisposable = new CompositeDisposable();
        compositeDisposable.add(RetrofitClient.getRetroFitClient().getAllCategoryMovies(categoryId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<List<AllCategory>>() {
                    @Override
                    public void onNext(List<AllCategory> allCategoryList) {
                        for ( int i=0; i < allCategoryList.size(); i++ )
                        {

                        }
                        setMainRecycler(allCategoryList);



                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d("bannerData",""+e);
                    }

                    @Override
                    public void onComplete() {

                    }
                })

        );
    }



}

Itemrecycleradpater

package com.example.movieapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.movieapp.MovieDetails;
import com.example.movieapp.R;
import com.example.movieapp.model.CategoryItemList;

import java.util.ArrayList;
import java.util.List;

public class ItemRecyclerAdapter extends RecyclerView.Adapter<ItemRecyclerAdapter.ItemViewHolder> implements Filterable {
    Context context;
    private List<CategoryItemList> categoryItemList;
     private List<CategoryItemList>allCategoryListFull;

    public ItemRecyclerAdapter(Context context, List<CategoryItemList> categoryItemList) {
        this.context = context;
        this.categoryItemList = categoryItemList;
        allCategoryListFull = new ArrayList<>(categoryItemList);
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(context).inflate(R.layout.cardview_movies,parent,false));
    }

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

        Glide.with(context).load(categoryItemList.get(position).getSmallThumb()).placeholder(R.drawable.image12).into(holder.image_item);


       holder.image_item.setOnClickListener(view -> {
               Intent i = new Intent(context, MovieDetails.class);
               i.putExtra("movieId",categoryItemList.get(position).getId());
               i.putExtra("movieName",categoryItemList.get(position).getMovieName());
           i.putExtra("smallThumb",categoryItemList.get(position).getSmallThumb());
               i.putExtra("movieImageUrl",categoryItemList.get(position).getImageUrl());
               i.putExtra("movieFile",categoryItemList.get(position).getFileUrl());
           i.putExtra("MovieDesc",categoryItemList.get(position).getMovieDesc());
           i.putExtra("Language",categoryItemList.get(position).getLanguage());
           i.putExtra("Singers",categoryItemList.get(position).getSingers());
           i.putExtra("TrailerUrl",categoryItemList.get(position).getTrailerUrl());
               context.startActivity(i);

       });

    }


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


    @Override
    public Filter getFilter() {
        return filterNew;
    }

    private Filter filterNew = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            List<CategoryItemList> filteredList = new ArrayList<>();
            if(constraint == null || constraint.length() ==0){
                filteredList.addAll(allCategoryListFull);
            }
            else{
                String filterPattern = constraint.toString().toLowerCase().trim();
                for(CategoryItemList item: allCategoryListFull)
                {
                    if(item.getMovieName().toLowerCase().contains(filterPattern))
                    {
                        filteredList.add(item);
                    }
                }

            }

            FilterResults results  = new FilterResults();
            results.values = filteredList;
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            categoryItemList.clear();
            categoryItemList.addAll((List)results.values);
            notifyDataSetChanged();

        }
    };

    public static final class ItemViewHolder extends RecyclerView.ViewHolder
    {
        ImageView image_item;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            image_item=itemView.findViewById(R.id.image_item);
        }
    }

}

package com.example.movieapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.movieapp.R;
import com.example.movieapp.model.AllCategory;
import com.example.movieapp.model.CategoryItemList;

import java.util.List;


public class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapter.MainViewHolder> {

    Context context;
    private List<AllCategory> allCategoryList;


    public MainRecyclerAdapter(Context context, List<AllCategory> allCategoryList) {
        this.context = context;
        this.allCategoryList = allCategoryList;
    }

    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MainViewHolder(LayoutInflater.from(context).inflate(R.layout.main_recycler_row_item, parent, false));

    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        holder.categoryName.setText(allCategoryList.get(position).getCategoryTitle());
        setItemRecycler(holder.itemRecycler,allCategoryList.get(position).getCategoryItemList());


    }

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


    public static final class MainViewHolder extends RecyclerView.ViewHolder {

        TextView categoryName;
        RecyclerView itemRecycler;


        public MainViewHolder(@NonNull View itemView) {
            super(itemView);
            categoryName = itemView.findViewById(R.id.item_category);
            itemRecycler = itemView.findViewById(R.id.item_recycler);

        }
    }

    private void setItemRecycler(RecyclerView recyclerview, List<CategoryItemList> categoryItemList) {

        ItemRecyclerAdapter itemRecyclerAdapter = new ItemRecyclerAdapter(context,categoryItemList);
        recyclerview.setLayoutManager(new LinearLayoutManager(context,RecyclerView.HORIZONTAL,false));
        //recyclerview.setLayoutManager(new GridLayoutManager(context,3));
        recyclerview.setAdapter(itemRecyclerAdapter);

    }
}
  • First, can you plaese confirm following? 1) Based on your JSON i understood that Your API is giving category listing & into the category listing there is categoryItemList (Movie detail) right? 2) Your problem is you want to display the details of the particular category in to other activity right? – KGeeks Sep 08 '21 at 09:31
  • exactly. if first category in main activity is action movies when user press action movies text button the next activity will show all the action movies as a list i don't know how to do that –  Sep 08 '21 at 12:37
  • MainRecyclerAdapter is your "CategoryList" adapter so, call ItemClickListener of adapter & pass your categoryItemList to extras of new screen (Ex. MovieList.java) => You can reuse your "ItemRecyclerAdapter" & related stuff into MovieList.java – KGeeks Sep 09 '21 at 04:23
  • Thanks for the reply. I will pass the category list in the holder of mainactivty using intent then in new activity how can I filter according to category name filter. for example the user is pressing Latest movie button it will take to new recycler view in another activity . so how can i filter in that new activity that recyclerview show only latest movies items ? . I have already tried similar things using intent to pass category name and take same recycler adapter and model but that shows all the recycler data –  Sep 09 '21 at 08:10
  • Also my recycler view is nested recycler view so i am stucked how to call indidual category name in a condition statement . because inside nested recycler all the category name are coming from json file not hardcoded they are showing in run time :( –  Sep 09 '21 at 08:13
  • No, you do not need to pass whole categort list just "itemList" that you are getting into category object. pass only movie list to new screen & into that new screen just dump movie list to recycler view – KGeeks Sep 09 '21 at 10:13
  • & talking about nested recyclerview than this may help you https://github.com/kamrul3288/Expandable-Recyclerview-List-Android – KGeeks Sep 09 '21 at 10:23
  • can you please highlight to me the code that I need to use? I am bit new to this android dev. i can see only private List all category list; in my code . what do you mean by just itemList . –  Sep 09 '21 at 14:51
  • Actually regarding nested recycler view i have made nested recycler view successfully but i dont know how to call indiviual item or object like category name –  Sep 09 '21 at 14:52

2 Answers2

1
package com.example.movieapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.movieapp.R;
import com.example.movieapp.model.AllCategory;
import com.example.movieapp.model.CategoryItemList;

import java.util.List;


public class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapter.MainViewHolder> {

    Context context;
    private List<AllCategory> allCategoryList;
    
    public MainRecyclerAdapter(Context context, List<AllCategory> allCategoryList) {
        this.context = context;
        this.allCategoryList = allCategoryList;
    }

    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MainViewHolder(LayoutInflater.from(context).inflate(R.layout.main_recycler_row_item, parent, false));
    }

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

        // Here is your call to an individual item or object like category name
        holder.categoryName.setText(allCategoryList.get(position).getCategoryTitle());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Stuff to open new activity
                List<CategoryItemList> categoryItemList = allCategoryList.get(position).getCategoryItemList()

                Intent i = new Intent(context, MopvieList.class); //New screen
                i.putExtra("itemList", categoryItemList); //passing serializable object
                //NOTE: CategoryItemList class must implement Serializable
            }
        });

        setItemRecycler(holder.itemRecycler, allCategoryList.get(position).getCategoryItemList());
    }

    @Override
    public int getItemCount() {
        return allCategoryList.size();
    }
    
    public static final class MainViewHolder extends RecyclerView.ViewHolder {

        TextView categoryName;
        RecyclerView itemRecycler;
        
        public MainViewHolder(@NonNull View itemView) {
            super(itemView);
            categoryName = itemView.findViewById(R.id.item_category);
            itemRecycler = itemView.findViewById(R.id.item_recycler);

        }
    }

    private void setItemRecycler(RecyclerView recyclerview, List<CategoryItemList> categoryItemList) {
        ItemRecyclerAdapter itemRecyclerAdapter = new ItemRecyclerAdapter(context, categoryItemList);
        recyclerview.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false));
        //recyclerview.setLayoutManager(new GridLayoutManager(context,3));
        recyclerview.setAdapter(itemRecyclerAdapter);
    }
}
KGeeks
  • 173
  • 6
  • I try and add my result please be patiance because this is totally new and thanks for the working code but i guess there is issue with my json hope you can abide with me and help me one more time –  Sep 10 '21 at 08:32
0
 holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            List<CategoryItemList> categoryItemList = allCategoryList.get(position).getCategoryItemList();
            Intent i = new Intent(context, MovieCat.class); //my activity for showing all category
            i.putExtra("itemList", (Serializable) categoryItemList);
            context.startActivity(i);
        }

public class AllCategory implements Serializable {}// as instructed made it serializable.

public class MovieCat extends AppCompatActivity  {
    TextView MovieCat;
    String movieNameCat;

    ArrayList<CategoryItemList> catItems;
    RecyclerView recyclerView;
    MainRecyclerAdapter mainRecyclerAdapter;

Button button;
    String cat; //for item_list intent 
    String catId;// for intent
    int categoryId;// for get all movies data
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_cat);
        MovieCat = findViewById(R.id.textView2);
        movieNameCat = getIntent().getStringExtra("categoryTitle");
        cat = getIntent().getStringExtra("itemList");
        button=findViewById(R.id.view_all_btn);
        MovieCat.setText(movieNameCat);
      catId = getIntent().getStringExtra("categoryId"); getAllMoviesData(categoryId);
        getAllMoviesData(categoryId);
    }




    private void getAllMoviesData(int categoryId) {
        {
            CompositeDisposable compositeDisposable = new CompositeDisposable();
            compositeDisposable.add(RetrofitClient.getRetroFitClient().getAllCategoryMovies(categoryId)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new DisposableObserver<List<AllCategory>>() {
                        @Override
                        public void onNext(List<AllCategory> itemList) {
                            for (int i = 0; i < itemList.size(); i++) {
                                setMainRecycler(itemList);

                            }
                        }
                        @Override
                        public void onError(Throwable e) {
                            Log.d("bannerData", "" + e);
                        }
                        @Override
                        public void onComplete() {
                        }
                    })

            );
        }
    }

    public void setMainRecycler(List<AllCategory> catItems) {
        recyclerView = findViewById(R.id.item_cycle);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        mainRecyclerAdapter = new MainRecyclerAdapter(this, catItems);
        recyclerView.setAdapter(mainRecyclerAdapter);


    }

Updated my code @KGeeks

  • From the video, I think there is a problem with your layout, try n make your layout match_parent & why ? ArrayList catItems; I don't understand this You have to create a list of type categoryItemList like ArrayList catItems – KGeeks Sep 10 '21 at 09:00
  • https://res.cloudinary.com/trailervideos/image/upload/v1631265339/error_ug34m3.png if i remove that i get this error I guess that parcelable was auto created when i use suggested fix –  Sep 10 '21 at 09:15
  • https://www.sitepoint.com/transfer-data-between-activities-with-android-parcelable/ – KGeeks Sep 10 '21 at 09:39
  • can you please tell me how to use switch or if statement to get different data item inside json query or inside button on click event . because " getAllMoviesData(2);" this is the code that display data if i change int value from 2 to 3 i get different data so i think i need some codition to check if current name is christian movies and christian movies from json object is equal then perform some action like getAllMoviesData(2); –  Sep 10 '21 at 09:44
  • In MovieCat you have made getAllMoviesData(4); function call as static here "4" is category id so instead of the static value you please get category id from intent like, String catId = getIntent().getStringExtra("id"); getAllMoviesData(catId); – KGeeks Sep 10 '21 at 09:53
  • can you please tell me what am missing :E/RecyclerView: No adapter attached; skipping layout –  Sep 10 '21 at 10:23
  • https://res.cloudinary.com/djdle8unh/video/upload/v1631269335/Movieapp_ajk9qi.mp4 –  Sep 10 '21 at 10:23
  • error shows you haven't attach layout manager to the recyclerview – KGeeks Sep 10 '21 at 10:32
  • but i attached main recycler inside getdata in json codepublic void onNext(List itemList) { for (int i = 0; i < itemList.size(); i++) { setMainRecycler(itemList); –  Sep 10 '21 at 10:38
  • I think you have to clear your basic knowledge about recyclerview first https://developer.android.com/guide/topics/ui/layout/recyclerview – KGeeks Sep 10 '21 at 11:02
  • Thanks bro/sis for your help. Finally i was able to retreve the data from json using the last few steps and now there is few more step thats from json side. I think i can handle that. thanks once again for answering patiently and the link you provided was useful and made me to understand about the recycerview . thanks have a good day. I marked the answer as accepted –  Sep 10 '21 at 14:25