Home > Software engineering >  A movie app like the Amazon prime, I would display the movie category using the CategoryItem. I want
A movie app like the Amazon prime, I would display the movie category using the CategoryItem. I want

Time:08-01

I mixed things up. The initial error I had was this MainActivity.java:130: error: incompatible types: List<com.kuroniczstudio.splashscreen.CategoryItem> cannot be converted to List<com.kuroniczstudio.splashscreen.model.CategoryItem> allCategoryList.add(new AllCategory(1,"VIP Hour", homeCatListItem1)); While try to fix it that was when things started getting confusing. So I am still having this error

MainActivity


import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;


import com.google.android.material.tabs.TabLayout;
import com.kuroniczstudio.splashscreen.adapter.BannerMoviesPagerAdapter;
import com.kuroniczstudio.splashscreen.adapter.MainRecyclerAdapter;
import com.kuroniczstudio.splashscreen.model.AllCategory;
import com.kuroniczstudio.splashscreen.model.BannerMovies;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

    public class MainActivity extends AppCompatActivity {

    BannerMoviesPagerAdapter bannerMoviesPagerAdapter;
    TabLayout indicatorTab, categoryTab;
    ViewPager bannerMoviesViewPager;
    List<BannerMovies> homeBannerList;
    List<BannerMovies> vipHourBannerList;
    List<BannerMovies> prophecyBannerList;
    List<BannerMovies> testimonyBannerList;
    Timer sliderTimer;

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


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

        indicatorTab = findViewById(R.id.tab_indicator);
        categoryTab = findViewById(R.id.tablayout);

        homeBannerList = new ArrayList<>();
        homeBannerList.add(new BannerMovies(1, "Trailer - I cannot give up now!!!", "https://i.pinimg.com/originals/1a/5e/c6/1a5ec63c50513055ac7674ad65c2e1a1.jpg", "https://youtu.be/7w1YKrHW85c"));
        homeBannerList.add(new BannerMovies(2, "Trailer - I must cross the Red Sea", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/88dHJdCyyBA"));
        homeBannerList.add(new BannerMovies(3, "Trailer - I must be better than my Father", "https://i.pinimg.com/originals/ec/60/46/ec604668771e8916466be6835d2ea588.jpg", "https://youtu.be/GsNrJgLSTAM"));

        vipHourBannerList = new ArrayList<>();
        vipHourBannerList.add(new BannerMovies(1, "I will be better than my father", "https://i.pinimg.com/originals/ec/60/46/ec604668771e8916466be6835d2ea588.jpg", "https://youtu.be/ymIUteex694"));
        vipHourBannerList.add(new BannerMovies(3, "I must cross the Red Sea", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/Sbbu536E9vA"));
        vipHourBannerList.add(new BannerMovies(4, "My Hair Will Grow Again", "https://img.yts.mx/assets/images/movies/sharkula_2022/medium-cover.jpg", "https://youtu.be/yau5eQH1K_I"));

        prophecyBannerList = new ArrayList<>();
        prophecyBannerList.add(new BannerMovies(2, "Looking for Jackie", "https://img.yts.mx/assets/images/movies/looking_for_jackie_2009/medium-cover.jpg", "https://www.youtube.com/watch?v=ByYWL1SEe-k"));
        prophecyBannerList.add(new BannerMovies(3, "Khuda Haafiz Chapter II", "https://sdmoviespoint.mba/wp-content/uploads/2022/07/Khuda-Haafiz-Chapter-II-Agni-Pariksha-2022-Full-Movie-Download-Free.jpg", "https://www.youtube.com/watch?v=ByYWL1SEe-k"));
        prophecyBannerList.add(new BannerMovies(4, "Sharkula", "https://img.yts.mx/assets/images/movies/sharkula_2022/medium-cover.jpg", "https://youtu.be/6SOEYXZK6Q4"));
        prophecyBannerList.add(new BannerMovies(5, "Looking for Jackie", "https://img.yts.mx/assets/images/movies/looking_for_jackie_2009/medium-cover.jpg", "https://www.youtube.com/watch?v=ByYWL1SEe-k"));

        testimonyBannerList = new ArrayList<>();
        testimonyBannerList.add(new BannerMovies(1, "See how this little boy survived under the water for 2 hours", "https://i.pinimg.com/originals/14/f1/5c/14f15cc3bbc780abdfecafa0f94024e1.jpg", "https://youtu.be/rRFkBLGQIs8"));
        testimonyBannerList.add(new BannerMovies(2, "Testimony Blessing Nsikak Saved from spiritual attack", "https://i.pinimg.com/originals/14/f1/5c/14f15cc3bbc780abdfecafa0f94024e1.jpg", "https://youtu.be/TdYn24MdD1Q"));
        testimonyBannerList.add(new BannerMovies(3, "See how this man got back his transformer working again", "https://i.pinimg.com/originals/14/f1/5c/14f15cc3bbc780abdfecafa0f94024e1.jpg", "https://youtu.be/HNjxxk3eWKk"));

        //this is default tab selected
        setBannerMoviesPagerAdapter(homeBannerList);

        categoryTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()){
                    case 1:
                        setBannerMoviesPagerAdapter(vipHourBannerList);
                        return;
                    case 2:
                        setBannerMoviesPagerAdapter(prophecyBannerList);
                        return;
                    case 3:
                        setBannerMoviesPagerAdapter(testimonyBannerList);
                        return;
                    default:
                        setBannerMoviesPagerAdapter(homeBannerList);

                }

            }

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

            }

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

            }
        });






        List<CategoryItem> homeCatListItem1 = new ArrayList<>();
        homeCatListItem1.add(new CategoryItem(1, "Trailer - I cannot give up now!!!", "https://i.pinimg.com/originals/1a/5e/c6/1a5ec63c50513055ac7674ad65c2e1a1.jpg", "https://youtu.be/7w1YKrHW85c"));
        homeCatListItem1.add(new CategoryItem(2, "Trailer - I must cross the Red Sea", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/88dHJdCyyBA"));
        homeCatListItem1.add(new CategoryItem(3, "Trailer - I must be better than my Father", "https://i.pinimg.com/originals/ec/60/46/ec604668771e8916466be6835d2ea588.jpg", "https://youtu.be/GsNrJgLSTAM"));
        homeCatListItem1.add(new CategoryItem(4, "My Hair Will Grow Again", "https://i.pinimg.com/originals/14/f1/5c/14f15cc3bbc780abdfecafa0f94024e1.jpg", "https://youtu.be/yau5eQH1K_I"));
        homeCatListItem1.add(new CategoryItem(5, "See how this man got back his transformer working again", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/HNjxxk3eWKk"));

        List<CategoryItem> homeCatListItem2 = new ArrayList<>();
        homeCatListItem2.add(new CategoryItem(1, "Trailer - I cannot give up now!!!", "https://i.pinimg.com/originals/1a/5e/c6/1a5ec63c50513055ac7674ad65c2e1a1.jpg", "https://youtu.be/7w1YKrHW85c"));
        homeCatListItem2.add(new CategoryItem(2, "Trailer - I must cross the Red Sea", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/88dHJdCyyBA"));
        homeCatListItem2.add(new CategoryItem(3, "Trailer - I must be better than my Father", "https://i.pinimg.com/originals/ec/60/46/ec604668771e8916466be6835d2ea588.jpg", "https://youtu.be/GsNrJgLSTAM"));
        homeCatListItem2.add(new CategoryItem(4, "My Hair Will Grow Again", "https://i.pinimg.com/originals/14/f1/5c/14f15cc3bbc780abdfecafa0f94024e1.jpg", "https://youtu.be/yau5eQH1K_I"));
        homeCatListItem2.add(new CategoryItem(5, "See how this man got back his transformer working again", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/HNjxxk3eWKk"));

        List<CategoryItem> homeCatListItem3 = new ArrayList<>();
        homeCatListItem3.add(new CategoryItem(1, "Trailer - I cannot give up now!!!", "https://i.pinimg.com/originals/1a/5e/c6/1a5ec63c50513055ac7674ad65c2e1a1.jpg", "https://youtu.be/7w1YKrHW85c"));
        homeCatListItem3.add(new CategoryItem(2, "Trailer - I must cross the Red Sea", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/88dHJdCyyBA"));
        homeCatListItem3.add(new CategoryItem(3, "Trailer - I must be better than my Father", "https://i.pinimg.com/originals/ec/60/46/ec604668771e8916466be6835d2ea588.jpg", "https://youtu.be/GsNrJgLSTAM"));
        homeCatListItem3.add(new CategoryItem(4, "My Hair Will Grow Again", "https://i.pinimg.com/originals/14/f1/5c/14f15cc3bbc780abdfecafa0f94024e1.jpg", "https://youtu.be/yau5eQH1K_I"));
        homeCatListItem3.add(new CategoryItem(5, "See how this man got back his transformer working again", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/HNjxxk3eWKk"));



        allCategoryList = new ArrayList<>();
        allCategoryList.add(new AllCategory(1,"VIP Hour", homeCatListItem1));
        allCategoryList.add(new AllCategory(2,"VIP Hour", homeCatListItem2));
        allCategoryList.add(new AllCategory(3,"VIP Hour", homeCatListItem3));

        setMainRecycler(allCategoryList);




    }

    private void setBannerMoviesPagerAdapter(List<BannerMovies> bannerMoviesList) {

        bannerMoviesViewPager = findViewById(R.id.banner_viewPager);
        bannerMoviesPagerAdapter = new BannerMoviesPagerAdapter(this, bannerMoviesList);
        bannerMoviesViewPager.setAdapter(bannerMoviesPagerAdapter);
        indicatorTab.setupWithViewPager(bannerMoviesViewPager);

        sliderTimer = new Timer();
        sliderTimer.scheduleAtFixedRate(new AutoSlider(), 4000, 6000);
        indicatorTab.setupWithViewPager(bannerMoviesViewPager, true);

        }

    class AutoSlider extends TimerTask {
        @Override
        public void run() {
            MainActivity.this.runOnUiThread(() ->{

                    if (bannerMoviesViewPager.getCurrentItem() < homeBannerList.size() - 1) {

                        bannerMoviesViewPager.setCurrentItem(bannerMoviesViewPager.getCurrentItem()   1);
                    }
                    else{
                        bannerMoviesViewPager.setCurrentItem(0);
                    }

            });


        }
    }
        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);
        }

}

//Items in the model directory//

CategoryItem

public class CategoryItem {


    Integer id;
    String movieName;
    String imageUrl;
    String fileUrl;

    public CategoryItem(Integer id, String movieName, String imageUrl, String fileUrl) {
        this.id = id;
        this.movieName = movieName;
        this.imageUrl = imageUrl;
        this.fileUrl = fileUrl;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getMovieName() {
        return movieName;
    }

    public void setMovieName(String movieName) {
        this.movieName = movieName;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getFileUrl() {
        return fileUrl;
    }

    public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
    }
}

AllCategory.Class


import java.util.List;

public class AllCategory {



    String categoryTitle;
    Integer categoryId;
    private List<CategoryItem> categoryItemList = null;

    public AllCategory(Integer categoryId, String categoryTitle, List<CategoryItem> categoryItemList) {
        this.categoryTitle = categoryTitle;
        this.categoryId = categoryId;
        this.categoryItemList = categoryItemList;
    }

    public List<CategoryItem> getCategoryItemList() {
        return categoryItemList;
    }

    public void setCategoryItemList(List<CategoryItem> categoryItemList) {
        this.categoryItemList = categoryItemList;
    }

    public String getCategoryTitle(){
        return categoryTitle;
    }

    public void setCategoryTitle(String categoryTitle){
        this.categoryTitle = categoryTitle;
    }

    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }
}

BannerMovies package com.kuroniczstudio.splashscreen.model;

public class BannerMovies {

Integer id;
String movieName;
String imageUrl;
String fileUrl;

public BannerMovies(Integer id, String movieName, String imageUrl, String fileUrl) {
    this.id = id;
    this.movieName = movieName;
    this.imageUrl = imageUrl;
    this.fileUrl = fileUrl;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getMovieName() {
    return movieName;
}

public void setMovieName(String movieName) {
    this.movieName = movieName;
}

public String getImageUrl() {
    return imageUrl;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}

public String getFileUrl() {
    return fileUrl;
}

public void setFileUrl(String fileUrl) {
    this.fileUrl = fileUrl;
}

}

_____________________________________  

**MainRecyclerAdapter**

    package com.kuroniczstudio.splashscreen.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.kuroniczstudio.splashscreen.CategoryItem;
import com.kuroniczstudio.splashscreen.R;
import com.kuroniczstudio.splashscreen.model.AllCategory;

import java.util.List;

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

    Context context;
    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));
    }

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

    }

    @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<CategoryItem> categoryItemList){

       ItemRecyclerAdapter itemRecyclerAdapter = new ItemRecyclerAdapter(context, categoryItemList);
       recyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false));
       recyclerView.setAdapter(itemRecyclerAdapter);

    }

}

ItemRecyclerAdapter

package com.kuroniczstudio.splashscreen.adapter;

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

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

import com.bumptech.glide.Glide;
import com.kuroniczstudio.splashscreen.CategoryItem;
import com.kuroniczstudio.splashscreen.R;

import java.util.List;

public class ItemRecyclerAdapter extends RecyclerView.Adapter<ItemRecyclerAdapter.ItemViewHolder> {

    Context context;
    List<CategoryItem> categoryItemList;
    public ItemRecyclerAdapter(Context context, List<CategoryItem> categoryItemList) {
        this.context = context;
        this.categoryItemList = categoryItemList;
    }

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

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



        Glide.with(context).load(categoryItemList.get(position).getImageUrl()).into(holder.itemImage);
    }

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

    public static final class ItemViewHolder extends RecyclerView.ViewHolder{

        ImageView itemImage;

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

            itemImage = itemView.findViewById(R.id.item_image);
        }
    }
}

BannerMoviesPagerAdapter

package com.kuroniczstudio.splashscreen.adapter;

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

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.kuroniczstudio.splashscreen.R;
import com.kuroniczstudio.splashscreen.model.BannerMovies;

import java.util.List;

public class BannerMoviesPagerAdapter extends PagerAdapter {

    Context context;
    List<BannerMovies> bannerMoviesList;

    public BannerMoviesPagerAdapter(Context context, List<BannerMovies> bannerMoviesList) {
        this.context = context;
        this.bannerMoviesList = bannerMoviesList;
    }

    @Override
    public int getCount() {
        return bannerMoviesList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
       container.removeView((View) object);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        View view = LayoutInflater.from(context).inflate(R.layout.banner_movie_layout, null);

        ImageView bannerImage = view.findViewById(R.id.banner_image);


        Glide.with(context).load(bannerMoviesList.get(position).getImageUrl()).into(bannerImage);
        container.addView(view);
        return view;




    }
}

CodePudding user response:

The error itself is telling you where you are messing up. You have 2 CategoryItem classes in your project with the following hierarchies:

  1. com.kuroniczstudio.splashscreen.CategoryItem
  2. com.kuroniczstudio.splashscreen.model.CategoryItem

You're trying to use both these which is creating this conflict. Make sure you are using only one class.

CodePudding user response:

In your application you have TWO classes called CategoryItem. One has the full name:

com.kuroniczstudio.splashscreen.CategoryItem

The other has the full name:

com.kuroniczstudio.splashscreen.model.CategoryItem

They are different classes, and you cannot convert from one class to the other one.

We cannot tell from the code that you have shown us why this is happening. For a start, you haven't show us both versions of CategoryItem. And you have left out all of the package statements. (So we can't tell which version of CategoryItem the AllCategory constructor expects ...)

But the compilation error message clearly proves that it is happening.

You probably shouldn't have two CategoryItem classes at all ...

  • Related