Home > Blockchain >  Can't filter by genre
Can't filter by genre

Time:07-20

I have a hardcoded list of movies and I want to filter them by their genre (which is a string), however only the All filter works as of now and for the other genres the result is the same as for the All filter. Can anyone help me out

class HomePresenter:

public class HomePresenter {

List<Film> filmList = Repository.getHardcodedList();


public List<Film> filterByGenre(String genre){
    if (genre.equals("All"))
        return filmList;
    else if (genre.equals("Drama"))
        return filmList;
    else if (genre.equals("Adventure"))
        return filmList;
    else if (genre.equals("Animation"))
        return filmList;
    else if (genre.equals("Horror"))
        return filmList;
    else if (genre.equals("Comedy"))
        return filmList;
    else if (genre.equals("Romance"))
        return filmList;
    else if (genre.equals("Documentary"))
        return filmList;


    List<Film> filteredFilmList = new ArrayList<>();

    for (Film f : filmList){

        if(f.getGenre().equals(genre)){

            filteredFilmList.add(f);
        }
    }

    return filteredFilmList;
}

Class Repository:

     public class Repository {
        static List<Film> filmList = new ArrayList<>();
        static List<Reservation> reservationList = new ArrayList<>();
        public static List<Film> favoriteList = new ArrayList<>();
        private static HashMap<String, HashMap<Time, List<Boolean>>> program;
        static List<Boolean> cinemaPlaces;
    
        public static List<Film> getHardcodedList() {
       

 List<Film> filmList = Arrays.asList(
            new Film("Despicable Me", "Animation", "better. ", 7.7, R.mipmap.despicable),
            new Film("The Martian", "Adventure", "alive. ", 8.0, R.mipmap.martian),
            new Film("Ted", "Comedy", "Lori. ", 6.9, R.mipmap.ted),
            new Film("Alita", "Adventure", "is. ", 7.6, R.mipmap.alita),
            new Film("Girl", "Drama", "ballerina. ", 7.2, R.mipmap.girl),
            new Film("A Star Is Born", "Romance", "spiral. ", 7.9, R.mipmap.star),
    );
        return filmList;
    }

Update with the spinner that I use for when choosing a genre of the movie:

HomeFragment:

Spinner spinner = (Spinner) view.findViewById(R.id.filter_spinner);
        ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this.getContext(),
                R.array.genres_array, android.R.layout.simple_spinner_item);
        arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(arrayAdapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedGenre = (String)parent.getItemAtPosition(position);
                List<Film> filteredFilmlist = homeActivityPresenter.filterByGenre(selectedGenre);
                final FilmAdapter adapter = new FilmAdapter(pointerSaver);
                recyclerView.setAdapter(adapter);
                adapter.setFilmlist(filteredFilmlist);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });

CodePudding user response:

Why don't you just use a stream?

List<Film> result = filmList.stream().filter(f -> genre.equals("All") || f.getGenre().equals(genre)).collect(Collectors.toList())

CodePudding user response:

change

public List<Film> filterByGenre(String genre){
if (genre.equals("All"))
    return filmList;
else if (genre.equals("Drama"))
    return filmList;
else if (genre.equals("Adventure"))
    return filmList;
else if (genre.equals("Animation"))
    return filmList;
else if (genre.equals("Horror"))
    return filmList;
else if (genre.equals("Comedy"))
    return filmList;
else if (genre.equals("Romance"))
    return filmList;
else if (genre.equals("Documentary"))
    return filmList;


List<Film> filteredFilmList = new ArrayList<>();

for (Film f : filmList){

    if(f.getGenre().equals(genre)){

        filteredFilmList.add(f);
    }
}

return filteredFilmList;}

to

public List<Film> filterByGenre(String genre){
if (genre.equals("All")) {
    return filmList;
}


List<Film> filteredFilmList = new ArrayList<>();

for (Film f : filmList){

    if(f.getGenre().equals(genre)){

        filteredFilmList.add(f);
    }
}

return filteredFilmList;
}

you can clean this up further by creating an enumeration for the genre, rather than using magic strings, and if you are using Java 8 then you can utilise Streams to create a filtered version of the filmList i.e. return filmList.stream().filter(film -> genre.equals(film.getGenre())).collect(Collectors.toList());

CodePudding user response:

You are returning the same List with every genre comparison in the if statements.

Change to this:

List<Film> filmList = Repository.getHardcodedList();


public List<Film> filterByGenre(String genre){
    if (genre.equals("All"))
        return filmList;


    List<Film> filteredFilmList = new ArrayList<>();

    for (Film f : filmList){

        if(f.getGenre().equals(genre)){

            filteredFilmList.add(f);
        }
    }

    return filteredFilmList;
}
  • Related