Android 如何访问ExpandableListView中的项目?
我有一个可扩展的列表视图。当我点击其中一个列表时,我有几个项目 是否有办法访问第一组中的第一项? 我有 我可以通过以下方式访问第一组:Android 如何访问ExpandableListView中的项目?,android,android-listview,expandablelistview,Android,Android Listview,Expandablelistview,我有一个可扩展的列表视图。当我点击其中一个列表时,我有几个项目 是否有办法访问第一组中的第一项? 我有 我可以通过以下方式访问第一组: mGattServicesList.getChildAt(0); 我现在想进入这个孩子体内去拿东西,但我找不到办法 好的,这里是我想要做这件事的函数: private void displayGattServices(List<BluetoothGattService> gattServices) { if (gattServices =
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));
}
private void displaytsetservices(列出gattServices){
if(gattServices==null)返回;
字符串uuid=null;
String unknownServiceString=getResources().getString(R.String.unknown\u服务);
String unknownCharaString=getResources().getString(R.String.unknown_特征);
ArrayList gattServiceData=新的ArrayList();
ArrayList gattCharacteristicData
=新的ArrayList();
mGattCharacteristics=新的ArrayList();
//循环通过可用的GATT服务。
用于(蓝牙gattService gattService:gattServices){
HashMap currentServiceData=新HashMap();
uuid=gattService.getUuid().toString();
currentServiceData.put(
LIST_NAME,samplegattatattributes.lookup(uuid,unknownServiceString));
currentServiceData.put(列表UUID,UUID);
gattServiceData.add(currentServiceData);
ArrayList gattCharacteristicGroupData=新的ArrayList();
List gattCharacteristics=gattService.getCharacteristics();
ArrayList charas=新的ArrayList();
//循环浏览可用的特性。
for(蓝牙gattCharacteristic gattCharacteristic:gattCharacteristics){
添加字符(gattCharacteristic);
HashMap currentCharaData=新HashMap();
uuid=gattCharacteristic.getUuid().toString();
currentCharaData.put(列表名称,SampleGattAttributes.lookup(uuid,unknownCharaString));
currentCharaData.put(列表uid,UUID);
gattCharacteristicGroupData.add(currentCharaData);
}
mGattCharacteristics.add(charas);
添加(gattCharacteristicGroupData);
}
SimpleExpandableListAdapter gattServiceAdapter=新SimpleExpandableListAdapter(
这个,gattServiceData,android.R.layout.simple\u expandable\u list\u item\u 2,
新字符串[]{LIST_NAME,LIST_UUID},新int[]{android.R.id.text1,android.R.id.text2},
gattCharacteristicData,android.R.layout.simple\u可扩展\u列表\u项目\u 2,
新字符串[]{LIST_NAME,LIST_UUID},新int[]{android.R.id.text1,android.R.id.text2}
);
mGattServicesList.setAdapter(gattServiceAdapter);
mGattServicesList.performItemClick(mGattServicesList.getChildAt(1),1,mGattServicesList.getItemIdAtPosition(1));
}
执行单击是为了将group1扩展,然后我想对其中的第二项执行单击
提前感谢您首先请发布您正在使用的适配器 第二个getchild函数应该获取groupPosition和childPosition。 看起来您使用的是常规列表适配器,而不是一次性的 编辑: 我看到您尝试通过列表而不是从适配器获取项目,我相信这将解决您的问题
另外,我强烈建议您制作自定义适配器,而不是使用基本适配器,以防您需要任何更改请详细说明。要填充列表,您需要使用适配器,对吗?然后,如果你发布你正在使用的适配器,这将是非常有帮助的。我需要在那里的3点SimpleExpandableListAdapter(…);此外,本指南可能会帮助您@Bob have您看到了从适配器而不是从列表中获取项目所需的编辑。我不明白您的意思。我只能通过列表访问适配器,因此我必须使用它。例如,mGattServicesList.getExpandableListAdapter()如果您有问题,请仍然查看此url,让我知道谢谢您的回复。问题是我的类已经在扩展活动,所以我无法实现getChildView()方法。。
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));
}