samedi 19 janvier 2019

Checkbox in ExpandableList

i am new on Android an try to write an "shopping cart" app.

What i have: My expandablelist shows my items in categories. On the end of each child i have placed a checkbox with an "to buy"-image . When i click on the Checkbox of the first item in the first category, the child is moved to a "bought"-category at the end of the list. In this "bought"-category the image for the Checkbox should be changed to "bought"-image. So far so good.

The Problem: The image on the "bought"-category is not the "bought"-image. Instead the now first item of my list has an "bought"-image.

Does somewon has an idea what wrong??

MainActivity

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.ExpandableListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class MainActivity extends Activity  {

    private ExpandableListView mExpandableList;
    private CheckBox cbChildren;
    private ArrayList<Children> listOfChildren;
    private ArrayList<Parent> arOfParents;

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

        mExpandableList = (ExpandableListView) findViewById(R.id.expandable_list);
        cbChildren = (CheckBox) findViewById(R.id.cbListItemChild);

        ArrayList<Parent> arrayParents = new ArrayList<>();
        ArrayList<Children> arrayChildren;

        ArrayList<Children> testArray = new ArrayList<Children>();
        testArray.add(new Children("Item1", "Cat1", 1, null, true, false,false));
        testArray.add(new Children("Item2", "Cat1", 1, null, true, false,true));
        testArray.add(new Children("Item3", "Cat2", 1, null, true, true,false));
        testArray.add(new Children("Item4", "Cat3", 1, "Pack", true, true,false));
        testArray.add(new Children("Item5", "Cat4", 1, null, true, false,false));
        testArray.add(new Children("Item6", "Cat5", 1, "piece", false, false,false));

        getArrayOfChildsFromDB(testArray);
//        createArrayOfParents(testArray);

        //sets the adapter that provides data to the list.
        mExpandableList.setAdapter(new MyCustomAdapter(MainActivity.this, arOfParents));
        // Always expand the views
        for (int i = 0; i < arOfParents.size(); i++) {
            mExpandableList.expandGroup(i);
        }


    }

    private void getArrayOfChildsFromDB(ArrayList<Children> testArray) {
        listOfChildren =testArray;
        createArrayOfParents(listOfChildren);
    }

    private void createArrayOfParents(ArrayList<Children> listOfChildren) {
        arOfParents = new ArrayList<Parent>();
        HashMap<String, ArrayList<Children>> map = new HashMap<>();


        for (Children child : listOfChildren) {

            if (!map.containsKey(child.getArtikelKategorie())) {
                map.put(child.getArtikelKategorie(), new ArrayList<Children>());
                map.get(child.getArtikelKategorie()).add(child);
            } else {
                map.get(child.getArtikelKategorie()).add(child);
            }
        }

        Set<String> keys = map.keySet();

        for (String key : keys) {
            Parent parent = new Parent();
            parent.setTitle(key);

            parent.setArrayChildren(map.get(key));
            arOfParents.add(parent);
        }


    }

}

MyCustomAdapter

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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

public class MyCustomAdapter extends BaseExpandableListAdapter {

    private LayoutInflater inflater;
    private ArrayList<Parent> mParent;
    private Context Kontext;
    private int count = 0;
    Children child;
    private CheckBox childCheckBox;


    public MyCustomAdapter(Context context, ArrayList<Parent> parent) {
        mParent = parent;
        inflater = LayoutInflater.from(context);
        Kontext = context;
    }

    @Override
    //counts the number of group/parent items so the list knows how many times calls getGroupView() method
    public int getGroupCount() {
        return mParent.size();
    }

    @Override
    //counts the number of children items so the list knows how many times calls getChildView() method
    public int getChildrenCount(int groupPosition) {
        Log.e("EXPANDABLE LIST", "GROUP " + groupPosition + " " + mParent.get(groupPosition).getArrayChildren()
                .size() + "");
        return mParent.get(groupPosition).getArrayChildren().size();
    }

    @Override
    //gets the title of each parent/group
    public Object getGroup(int i) {
        return mParent.get(i).getTitle();
    }

    @Override
    //gets the name of each item
    public Object getChild(int i, int i1) {
        return mParent.get(i).getArrayChildren().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public int getChildTypeCount() {
        return 6;
    }

    @Override
    //in this method you must set the text to see the parent/group on the list
    public View getGroupView(int groupPosition, boolean b, View convertView, ViewGroup viewGroup) {

        ViewHolder holder = new ViewHolder();
        holder.groupPosition = groupPosition;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item_parent, viewGroup, false);
            holder.groupTitle = (TextView) convertView.findViewById(R.id.list_item_text_view);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.groupTitle.setText(getGroup(groupPosition).toString());

        //return the entire view
        return convertView;
    }

    @Override
    //in this method you must set the text to see the children on the list
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup viewGroup) {

//        final ViewHolder holder;
        final ViewHolderChild holder;
        child = mParent.get(groupPosition).getArrayChildren().get
                (childPosition);

        if (convertView == null) {
            holder = new ViewHolderChild();
            convertView = inflater.inflate(R.layout.child_list_layout, viewGroup, false);
            holder.childTitle = (TextView) convertView.findViewById(R.id.list_item_text_child);
            holder.childCheckBox = (CheckBox) convertView.findViewById((R.id.cbListItemChild));

            convertView.setTag(holder);
        } else {
            holder = (ViewHolderChild) convertView.getTag();
        }

        holder.childTitle.setText(mParent.get(groupPosition).getArrayChildren().get(childPosition).getArtikelName());

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Kontext.getApplicationContext(), "Gekauft : " +mParent.get(groupPosition).getArrayChildren().get
                        (childPosition).gekauft
                        + "", Toast.LENGTH_SHORT).show();


            }
        });

        holder.childCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(Kontext.getApplicationContext(), "You have selected item : " +mParent.get(groupPosition).getArrayChildren().get
                        (childPosition).name
                        + "", Toast.LENGTH_SHORT).show();

                mParent.get(groupPosition).getArrayChildren().get
                        (childPosition).setIstArtikelGekauft(true);

//                holder.childCheckBox.setChecked(true);
//                notifyDataSetChanged();

//                    if (mParent.get(groupPosition).getArrayChildren().get(childPosition).getIsChecked()) {
//                    holder.childCheckBox.setChecked(true);
//                    notifyDataSetChanged();
//                } else {
//                    holder.childCheckBox.setChecked(false);
//                    notifyDataSetChanged();
//                }

                addChildToGroup(mParent.get(groupPosition).getArrayChildren().get
                        (childPosition), mParent.size() - 1, "Parent 25 (Hardcoded)");
                removeChildFromGroup(groupPosition, childPosition);
                notifyDataSetChanged();

            }
        });


        //return the entire view
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    public void removeGroup(int groupPosition) {
        mParent.remove(groupPosition);
        Log.e("removeGroup", "group size " + mParent.size());
        notifyDataSetChanged();
    }

    /**
     * Removes child at childPosition from group at groupPosition. If it is the last child in group
     * the group will also be deleted.
     *
     * @param groupPosition Position of the group in which the child exists
     * @param childPosition Position of the child in the group
     */
    public void removeChildFromGroup(int groupPosition, int childPosition) {
        Parent parent = mParent.get(groupPosition);

        parent.getArrayChildren().remove(childPosition);

        if (parent.getArrayChildren().isEmpty()) {
            removeGroup(groupPosition);
        } else {
            notifyDataSetChanged();
        }
    }

    public void addChildToGroup(Children childName, int groupPosition, String sectionTitle) {
        Parent parent = mParent.get(groupPosition);
        List<Children> arrayChildren = parent.getArrayChildren();


        if (!parent.getTitle().equals(sectionTitle)) {
            parent = new Parent();
            arrayChildren = new ArrayList<>();

            parent.setTitle(sectionTitle);

            mParent.add(parent);
        }

        arrayChildren.add(childName);
        parent.setArrayChildren(arrayChildren);
        notifyDataSetChanged();
    }


    protected class ViewHolder {
        protected int groupPosition;
        protected TextView groupTitle;
        protected TextView childTitle;
    }

    private class ViewHolderChild {

        TextView childTitle;
        CheckBox childCheckBox;
        View viewDivider;
    }
}




Aucun commentaire:

Enregistrer un commentaire