如何访问ExpandableListView中的项目?

时间:2015-01-18 16:06:06

标签: android android-listview expandablelistview

我有一个ExpandableListView。当我点击其中一个列表时,我有几个项目。

http://img15.hostingpics.net/pics/311437expandablelistview.png

是否有办法访问第一组中的第一项? 我有

private ExpandableListView mGattServicesList;

SimpleExpandableListAdapter gattServiceAdapter = new SimpleExpandableListAdapter(...);

mGattServicesList.setAdapter(gattServiceAdapter);

我可以通过以下方式访问第一组:

mGattServicesList.getChildAt(0);

我现在想在这个孩子里面去取物品,但我找不到办法。

好的,这是我想要这样做的功能:

private void displayGattServices(List<BluetoothGattService> gattServices) {
    if (gattServices == null) return;

    String uuid = null;
    String unknownServiceString = getResources().getString(R.string.unknown_service);
    String unknownCharaString = getResources().getString(R.string.unknown_characteristic);

    ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>();
    ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData
            = new ArrayList<ArrayList<HashMap<String, String>>>();
    mGattCharacteristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>();

    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
        HashMap<String, String> currentServiceData = new HashMap<String, String>();
        uuid = gattService.getUuid().toString();
        currentServiceData.put(
                LIST_NAME, SampleGattAttributes.lookup(uuid, unknownServiceString));
        currentServiceData.put(LIST_UUID, uuid);
        gattServiceData.add(currentServiceData);

        ArrayList<HashMap<String, String>> gattCharacteristicGroupData =new ArrayList<HashMap<String, String>>();

        List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();

        ArrayList<BluetoothGattCharacteristic> charas = new ArrayList<BluetoothGattCharacteristic>();

        // Loops through available Characteristics.
        for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
            charas.add(gattCharacteristic);
            HashMap<String, String> currentCharaData = new HashMap<String, String>();
            uuid = gattCharacteristic.getUuid().toString();
            currentCharaData.put(LIST_NAME, SampleGattAttributes.lookup(uuid, unknownCharaString));
            currentCharaData.put(LIST_UUID, uuid);
            gattCharacteristicGroupData.add(currentCharaData);
        }
        mGattCharacteristics.add(charas);
        gattCharacteristicData.add(gattCharacteristicGroupData);
    }

    SimpleExpandableListAdapter gattServiceAdapter = new SimpleExpandableListAdapter(
            this,gattServiceData,android.R.layout.simple_expandable_list_item_2,
            new String[] {LIST_NAME, LIST_UUID},new int[] { android.R.id.text1, android.R.id.text2 },
            gattCharacteristicData, android.R.layout.simple_expandable_list_item_2,
            new String[] {LIST_NAME, LIST_UUID},new int[] { android.R.id.text1, android.R.id.text2 }
    );
    mGattServicesList.setAdapter(gattServiceAdapter);
    mGattServicesList.performItemClick(mGattServicesList.getChildAt(1), 1, mGattServicesList.getItemIdAtPosition(1));

}

执行点击是为了获得group1的扩展,然后我想点击内部的第二个项目。

提前谢谢

1 个答案:

答案 0 :(得分:0)

首先请发布您正在使用的适配器 第二个getchild函数应该得到groupPosition和childPosition。 看起来你使用常规列表适配器而不是消耗型适配器。

修改

我看到你尝试通过列表而不是适配器获取项目我相信这将解决您的问题。

此外,我强烈建议您制作自定义适配器,而不是使用基本适配器,以防您需要任何更改