1

我让他们选择可扩展的孩子列表并获取信息。我还想为每个孩子实现一个数字选择器。但问题是孩子们不会选择,我也无法访问数字选择器按钮。我不使用任何侦听器,也不使用充气机访问 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);

代码扩展适配器

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>

可扩展组 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>

可扩展子 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>

对不起我的西班牙代码。

4

1 回答 1

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

并将其用作单元格的标签:

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

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

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

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

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

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

于 2013-06-25T12:54:52.517 回答