1

I make them selectable expandable list the children and take information. I also want to implement a number picker for each child. But the problem is that children do not select nor I can access the number picker buttons. I do not work any listener, or using the inflater to access the xml. Do not know what else to do. Thanks for all the help.

Code Main Class

final ControlClientMenu adapt = new ControlClientMenu(ClientMenu.this, detalls);
        lstComandes = (ExpandableListView) findViewById(R.id.lstComandes); //Creo la lista
        lstComandes.setLongClickable(true);

        lstComandes.setOnGroupClickListener(new OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                parent.expandGroup(groupPosition);
                parent.collapseGroup(groupPosition);
                return false;
            }
        });
        lstComandes.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
            {
                id_comanda = id.get(arg2);
                return false;
            }

        });

        lstComandes.setAdapter(adapt); //Le asigano el XML
        registerForContextMenu(lstComandes);

Code ExpandableAdapter

public class ControlClientMenu extends BaseExpandableListAdapter
{
Context context;
DetallsComandes[] detalls;
private ArrayList<String> grupos = new ArrayList<String>();
private ArrayList<ArrayList<ArrayList<String>>> hijos = new ArrayList<ArrayList<ArrayList<String>>>();
private ArrayList<ArrayList<ArrayList<String>>> hijosU = new ArrayList<ArrayList<ArrayList<String>>>();

public ControlClientMenu(Context context, DetallsComandes[] detalls)
{
    this.context = context;
    this.detalls = detalls;
    cargarDatos();
}

public void cargarDatos() //Funcion para separar los grupos de los hijos
{
    for(int i = 0; i<detalls.length;i++)
    {
        Log.d("De", detalls[i].getComanda());
        grupos.add(detalls[i].getComanda());
        hijos.add(new ArrayList<ArrayList<String>>());
        hijosU.add(new ArrayList<ArrayList<String>>());
        Vector aux = detalls[i].getVecProducte();
        Vector aux2 = detalls[i].getVecPreu();
        Vector aux3 = detalls[i].getVecUnitats();
        for(int j = 0; j<aux.size(); j++)
        {
            Log.d("CONT", aux.get(j).toString()+"   "+aux2.get(j).toString());
            hijos.get(i).add(new ArrayList<String>());
            hijosU.get(i).add(new ArrayList<String>());
            hijos.get(i).get(j).add(aux.get(j).toString()+"   "+aux2.get(j).toString());
            hijosU.get(i).get(j).add(aux3.get(j).toString());
        }
    }
}


public ArrayList<String> getChild(int groupPosition, int childPosition)
{
    return hijos.get(groupPosition).get(childPosition);
}

public ArrayList<String> getChild1(int groupPosition, int childPosition,  ArrayList<ArrayList<ArrayList<String>>> hijosU)
{
    return hijosU.get(groupPosition).get(childPosition);
}

public long getChildId(int groupPosition, int childPosition)
{
    return childPosition;
}

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 
{
    String hijo = (String) ((ArrayList<String>)getChild(groupPosition, childPosition)).get(0);
    String hijo2 = (String) ((ArrayList<String>)getChild1(groupPosition, childPosition, hijosU)).get(0);
    if (convertView == null) 
    {
        LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.tabla_comanda_product_item, null);
    }
    TextView hijotxt = (TextView) convertView.findViewById(R.id.item_prod);
    TextView hijoU = (TextView) convertView.findViewById(R.id.txtUnitats);
    hijotxt.setText(hijo);
    hijoU.setText(hijo2);
    return convertView;
}

public int getChildrenCount(int groupPosition) 
{
    return hijos.get(groupPosition).size();
}


public String getGroup(int groupPosition)
{
    return grupos.get(groupPosition);
}


public int getGroupCount()
{
    return grupos.size();
}


public long getGroupId(int groupPosition)
{
    return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
{
    String group = (String)getGroup(groupPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.tabla_comanda, null);
    }

    TextView grouptxt = (TextView) convertView.findViewById(R.id.lblComanda);
    grouptxt.setText(group);
    return convertView;
}

public boolean hasStableIds()
{
    return true;
}

public boolean isChildSelectable(int groupPosition, int childPosition) 
{
    return true;
}
}

Main XML

<TabHost
android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TabWidget>
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
                <ExpandableListView android:id="@+id/lstComandes"
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_marginTop="10px"
                                    android:paddingTop="5px">
                </ExpandableListView>
            </LinearLayout>
     </FrameLayout>
 </LinearLayout>
</TabHost>

ExpandableGroup XML

<?xml version="1.0" encoding="utf-8"?>
<!--VISTA DE LA EXPANDIBLELIST-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <!--CODIGO Y NOMBRE DEL CLIENTE-->
    <TextView android:id="@+id/lblComanda"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="italic"
        android:textSize="11dp"
        android:layout_marginLeft="53px"
        android:gravity="center_vertical"
        android:paddingTop="15px"
        android:paddingBottom="15px"/>
</LinearLayout>
<!--     <ListView android:id="@+id/lstProd" -->
<!--            android:layout_width="fill_parent" -->
<!--            android:layout_height="wrap_content" /> -->
<!--     <TextView android:id="@+id/lblTabla2" -->
<!--         android:layout_width="wrap_content" -->
<!--         android:layout_height="wrap_content"/> -->
</LinearLayout>

ExpandableChild XML

<?xml version="1.0" encoding="utf-8"?>
<!-- VISTA DE LOS SUBLISTA DE LA EXPANDIBLE LIST -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="13px"
android:paddingBottom="15px"
android:paddingLeft="20px">
<!--PRODUCTO PRECIO SUBTOTAL-->
<TextView android:id="@+id/item_prod"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"/>
<!-- NUMBERPICKER -->
<LinearLayout android:gravity="right"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <Button android:id="@+id/btnDown"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/down"/>
    <TextView android:id="@+id/txtUnitats"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
    </TextView>
    <Button android:id="@+id/btnUp"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="@string/up"/>
</LinearLayout>
</LinearLayout>

Sorry for my Spanish Code.

1 Answers1

0

In every method that need to access childs you should switch over the diferent groups (assuming you know the number of group and their purpose). Example :

@Override
    public Object getChild(int groupPosition, int childPosition) {

        switch (groupPosition) {
            case GROUP_TYPE:
                return _list1.get(childPosition);
            case GROUP_TARIF:
                return _list2.get(childPosition);
            case GROUP_PROMO:
                return _list3.get(childPosition);
        }
        return null;
    }

In getChildView you should use a view holder, as it is really easier to use and to attach to a cell. Example :

public class CellHolder {
    public CheckBox cellCheck;
    public TextView cellText;
}

And use it like a tag to your cell:

@Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    CellHolder cellHolder = null;
    if (convertView == null) {
        convertView = _inflater.inflate(R.layout.cell_layout, null);
        //
        cellHolder = new CellHolder();
        cellHolder.cellText = (TextView) convertView.findViewById(R.id.text_item);
        cellHolder.cellCheck = (CheckBox) convertView.findViewById(R.id.checkBox_item);
        cellHolder.cellCheck.setEnabled(false);
        convertView.setTag(filtreCellHolder);
    }
    else {
        cellHolder = (CellHolder) convertView.getTag();
    }
    switch (groupPosition) {
        case GROUP_TYPE:
            cellHolder.cellCheck.setChecked(_list1.get(childPosition).isInFiltre());
            cellHolder.cellText.setText(_list1.get(childPosition).getLibelle());
            break;
        case GROUP_TARIF:
            cellHolder.cellCheck.setChecked(_list2.get(childPosition).isInFiltre());
            cellHolder.cellText.setText(_list2.get(childPosition).text());
            break;
        case GROUP_PROMO:
            cellHolder.cellCheck.setChecked(_list3.get(childPosition).isInFiltre());
            cellHolder.cellText.setText(_list3.get(childPosition).getLibelle());
            break;
    }
    return convertView;
}

Do you see the idea. This way you can attach diferent cell type for each group (if needed) and retreive it efficiently.

Now to manage OnClick you can use the method onChildClick :

@Override
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {

    return super.onChildClick(parent, v, groupPosition, childPosition, id);
}

Hope it can help you in some way

An-droid
  • 6,433
  • 9
  • 48
  • 93