How to access items inside ExpandableListView?

戏子无情 提交于 2019-12-12 03:18:34

问题


I have an ExpandableListView. When I click on one of these lists, I have several items.

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

Is there a way or not to access to the first item in the first group ? I have

private ExpandableListView mGattServicesList;

SimpleExpandableListAdapter gattServiceAdapter = new SimpleExpandableListAdapter(...);

mGattServicesList.setAdapter(gattServiceAdapter);

I can access to the first group with :

mGattServicesList.getChildAt(0);

I want to go now inside this child to get items but I can't find a way.

Alright here is the function where I want to do this :

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

}

The perform click is to get the group1 extanded, then I want to do a perform click to the 2nd item inside.

Thank you in advance


回答1:


First please post the adapter you are using second getchild function should get groupPosition and childPosition. it seems like you use regular list adapter instead of expendable one.

Edit:

I see you try to get the item via the list instead from the Adapter I believe this will solve your problem.

Also i am stronglly reccomend you to make custom adapter instead of use the basic one just in case you want any change



来源:https://stackoverflow.com/questions/28011806/how-to-access-items-inside-expandablelistview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!