lundi 21 janvier 2019

Change Position of Item with checkbox in ExpandableListView

in my ExpandableListView i have some categories with items. Each item has an checkbox at the end. When i click on this checkbox the status should change to checked and then the item should move to another (hardcoded) category at the end of the list. There the checkbox should be checked like i did it in the original category.

The way to move the item i have found on examples. But the items checkbox is not checked in the new category. Instead the item on the old position of the moved item will be checked (without a click) in the original category.

Example (o means unchecked; x means checked):

after starting the app:

  • category 1
    item1 o
    item2 o
    item3 o
  • category 2
    item4 o

after click on checkbox of item1 in category 1

  • category 1
    item2 x
    item3 o
  • category 2
    item4 o
  • hardcoded category
    item1 o

Here is my Adapter-Class

    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 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 v) {
                Toast.makeText(Kontext.getApplicationContext(), "You have selected item : " +mParent.get(groupPosition).getArrayChildren().get
                        (childPosition).name
                        + "", Toast.LENGTH_SHORT).show();

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

Can anybody help to solve this problem? Thanks to everyone who takes the time.




Aucun commentaire:

Enregistrer un commentaire