我无法在可扩展列表中选择子项

时间:2012-09-13 17:04:38

标签: android list expandable

我让他们可选择扩展列表孩子并获取信息。我还想为每个孩子实施一个数字选择器。但问题是孩子们不选择也不能访问号码选择按钮。 我不工作任何监听器,或使用inflater访问xml。不知道还能做什么。 感谢您的帮助。

代码主类

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

主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>

对不起我的西班牙语代码。

1 个答案:

答案 0 :(得分:0)

在每个需要访问子项的方法中,您应该切换不同的组(假设您知道组的数量及其用途)。 示例

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

在getChildView中,您应该使用视图持有者,因为它更容易使用和附加到单元格。 示例

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

并将它像标签一样用于您的手机:

@覆盖     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;
}

你看到了这个主意吗?这样,您可以为每个组附加不同的细胞类型(如果需要)并有效地进行检索。

现在要管理OnClick,您可以使用方法 onChildClick

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

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

希望它能以某种方式帮助你