lundi 22 mai 2017

How can I do: Two chained ListViews where the checkbox father select all children

I have this problem a long time and I can't find a way to deal with this. I have two ListViews: The first one has the movies categories and checkboxs to select that category The second one, has the movies and checkboxs to select the movie

I need that when I select the checkbox of any category, all movies inside that cateory will be selected too, but when I select any category, only the items of the last category are selected and your values are exchanged for the values of the item that should have been selected.

The CategoryAdapter:

    public class CategoriesAdapter extends BaseAdapter {
        final Holder h = new Holder();
        ArrayList<Movie> movies;
        static ArrayList<String> categories = new ArrayList<>();
        Context context;
        movieAdapter mAdapter;
        private static LayoutInflater inflater = null;

        public CategoriesAdapter(int tipoAdap, ArrayList<Produto> produtos, Context context) {
            this.tipoAdap = tipoAdap;
            this.produtos = produtos;
            categorias = listaCategorias(produtos);
            this.context = context;
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

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

        //...

        public class Holder {
            TextView catName;
            CheckBox checkBox;
            ListView moviesList;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
               View v;
            v = inflater.inflate(R.layout.movies_list, null);
            h.catName = (TextView) v.findViewById(R.id.catname);
            h.moviesList = (ListView) v.findViewById(R.id.movies);
            h.checkBox = (CheckBox) v.findViewById(R.id.select);


            h.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    MoviesAdapter moviesAdapter = new MoviesAdapter(isChecked, movies, context);
                    h.moviesList.setAdapter(moviesAdapter);
                    moviesAdapter.notifyDataSetChanged();
                }
            });
}

MoviesAdater:

public class MoviesAdater extends BaseAdapter {

    Context context;
    ArrayList<Movie> moviesList = new ArrayList<>();
    private static LayoutInflater inflater = null;
    boolean isChecked;

    public MoviesAdater(boolean isChecked,Context context, ArrayList<Movie> moviesList) {
        this.isChecked = isChecked;
        this.context = context;
        this.moviesList = moviesList;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
         //find items..

                if (isChecked) {
                    h.check.setChecked(true);
                    ActivityCart.refreshCart(moviesList.get(position), isChecked, context);
                }else{
                    h.check.setChecked(false);
                    ActivityCart.refreshCart(moviesList.get(position), isChecked, context);
                }

                h.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            ActivityCart.refreshCart(moviesList.get(position), isChecked, context);
                    }
                });
}

ActivityCart:

 public static void refreshCart(Movie movie, boolean isChecked, Context context) {
            if(isChecked) {
                moviesSelected.add(movie);
                price += movie.getPrice();
            }else{
                if(moviesSelected.contains(movie)){
                    price -= movie.getPrice();
                    moviesSelected.remove(movie);
                }
            }
            price.setText(context.getString(R.string.price) +  "$ " + price);
        }

I'm sorry for my english, I hope that you understand. Any doubt can ask me that I'll try explain better.




Aucun commentaire:

Enregistrer un commentaire