ExpandableList Custom

4

I'm new to Android, but with an ambitious project. I'm trying to make a% custom% with two ExpandableList on items and the one on the left needs to have the background changed as per customer satisfaction.

ItturnsoutthatIjustcannotseemtoputtwoTextViewsontheitemsandchangethebackground.

Couldanyonehelpme?

Followmycodes.

FileTextViews:

packagebr.com.appcorpo.appcorpo;importandroid.content.Context;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseExpandableListAdapter;importandroid.widget.TextView;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;publicclassExpandableAdapterextendsBaseExpandableListAdapter{privateList<String>listGroup;privateHashMap<String,List<String>>listData;privateLayoutInflaterinflater;publicExpandableAdapter(Contextcontext,List<String>listGroup,HashMap<String,List<String>>listData){this.listGroup=listGroup;this.listData=listData;inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);}@OverridepublicintgetGroupCount(){returnlistGroup.size();}@OverridepublicintgetChildrenCount(intgroupPosition){returnlistData.get(listGroup.get(groupPosition)).size();}@OverridepublicObjectgetGroup(intgroupPosition){returnlistGroup.get(groupPosition);}@OverridepublicObjectgetChild(intgroupPosition,intchildPosition){returnlistData.get(listGroup.get(groupPosition)).get(childPosition);}@OverridepubliclonggetGroupId(intgroupPosition){returngroupPosition;}@OverridepubliclonggetChildId(intgroupPosition,intchildPosition){returnchildPosition;}@OverridepublicbooleanhasStableIds(){returnfalse;}@OverridepublicViewgetGroupView(intgroupPosition,booleanisExpanded,ViewconvertView,ViewGroupparent){ViewHolderGroupholderGroup;if(convertView==null){convertView=inflater.inflate(R.layout.header_expandable_list_view,null);holderGroup=newViewHolderGroup();convertView.setTag(holderGroup);holderGroup.tvGroup=(TextView)convertView.findViewById(R.id.tvGroup);}else{holderGroup=(ViewHolderGroup)convertView.getTag();}holderGroup.tvGroup.setText(listGroup.get(groupPosition));returnconvertView;}@OverridepublicViewgetChildView(intgroupPosition,intchildPosition,booleanisLastChild,ViewconvertView,ViewGroupparent){ViewHolderItemholderItem;Stringval=(String)getChild(groupPosition,childPosition);if(convertView==null){convertView=inflater.inflate(R.layout.item_expandable_list_view,null);holderItem=newViewHolderItem();convertView.setTag(holderItem);holderItem.tvItem=(TextView)convertView.findViewById(R.id.tvItem);}else{holderItem=(ViewHolderItem)convertView.getTag();}holderItem.tvItem.setText(val);returnconvertView;}@OverridepublicbooleanisChildSelectable(intgroupPosition,intchildPosition){returnfalse;}classViewHolderGroup{TextViewtvGroup;}classViewHolderItem{TextViewtvItem;TextViewtvItem1;}}

FileExpandableAdapter.java(FragmentpartthatmountsFragmentCardapio.java):

privatevoidexpandableList(finalViewrootView){//TratamentodoExpandableListbuildList();ExpandableListViewexpandableListView=(ExpandableListView)rootView.findViewById(R.id.expandableListView);expandableListView.setAdapter(newExpandableAdapter(rootView.getContext(),listGroup,listData));expandableListView.setOnChildClickListener(newExpandableListView.OnChildClickListener(){@OverridepublicbooleanonChildClick(ExpandableListViewparent,Viewv,intgroupPosition,intchildPosition,longid){returnfalse;}});expandableListView.setOnGroupExpandListener(newExpandableListView.OnGroupExpandListener(){@OverridepublicvoidonGroupExpand(intgroupPosition){//Toast.makeText(rootView.getContext(),"Group (Expand): " + groupPosition, Toast.LENGTH_LONG).show();

            }
        });

        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
//                Toast.makeText(rootView.getContext(), "Group (Collapse): " + groupPosition, Toast.LENGTH_LONG).show();

            }
        });
    }

    //  Metodo que monta a Listagem
    public void buildList() {

        listGroup = new ArrayList<String>();
        listData = new HashMap<String, List<String>>();
        String item;
        String aproveit;

        // Monta cabecalho do item
        for (int i = 0; i < 5; i++) {
            listGroup.add("Cafe da Manha " + (i + 1));

            //Monta item
            List<String> auxList = new ArrayList<String>();
            for (int y = 1; y < 6; y++) {
                item = "Cafe com Leite " + y;
                aproveit = "Comeu tudo";
                auxList.add(item);
                auxList.add(aproveit);
            }
            listData.put(listGroup.get(i), auxList);
        }
    }

File Expandable :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/tvGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:textColor="@color/azul_painel"
        android:textStyle="bold"
        android:textSize="8pt"
        android:layout_marginStart="30dp"/>

</LinearLayout>

File header_expandable_list_view.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/tvItem"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_weight="1"
        android:text="Aqui"
        android:textStyle="italic"
        android:textSize="7pt"/>

    <TextView
        android:id="@+id/tvItem1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="kdajfs"
        android:textStyle="italic"
        android:textSize="7pt"/>

</LinearLayout>

Thank you.

    
asked by anonymous 29.12.2015 / 16:23

1 answer

2

Hey people ...

I ended up solving my problem. I had to first change my listData variable to:

    private HashMap<String, List<List<String>>> listData

and so I was able to pass all my columns to the ExpandableAdapter class.

In the ExpandableAdapter class, in addition to matching the variables, I received the columns as follows:

    String val = listData.get(listGroup.get(groupPosition)).get(childPosition).get(0);

And the color changes went through:

    holderItem.tvItem1.setBackgroundColor(Color.GREEN);

Thank you all !!! : D

    
04.01.2016 / 14:44