【问题标题】:How to access items inside ExpandableListView?如何访问 ExpandableListView 中的项目?
【发布时间】:2015-01-18 16:06:06
【问题描述】:

我有一个 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,然后我想对里面的第二个项目执行点击。

提前谢谢你

【问题讨论】:

标签: android android-listview expandablelistview


【解决方案1】:

首先请发布您正在使用的适配器 第二个 getchild 函数应该获取 groupPosition 和 childPosition。 似乎您使用常规列表适配器而不是消耗性的。

编辑:

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

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

【讨论】:

  • 你能详细点吗?
  • 填充您需要使用适配器的列表,对吗?那么如果您发布您正在使用的适配器会很有帮助
  • 几乎我需要三个点 SimpleExpandableListAdapter(...);本指南也可能对您有所帮助tutorialsbuzz.com/2014/07/…
  • @Bob 你已经看到了你需要从适配器而不是从列表对象中获取项目的编辑
  • 我真的不明白你的意思。我只能通过列表访问适配器,所以我必须使用它。例如 mGattServicesList.getExpandableListAdapter()
猜你喜欢
  • 1970-01-01
  • 2015-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多