J'ai créé un ExpandableListView, mais le OnClickListener ne fonctionne pas. déjà essayé d'autres solutions, et tous suivent le même modèle de ce que j'ai fait. L'une de ces solutions trouvées, est que How to get onChildClick value from expandablelistview in android?OnChildClick dans ExpandableList ne fonctionne pas
Mon activité:
public class GroupRoutesActivity extends AppCompatActivity {
public static ExpandableListView expandableListView;
private static AdapterExpandableListGroups expandableListViewAdapter;
ArrayList<Group> listProcess;
Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group_routes);
ctx = GroupRoutesActivity.this;
expandableListView = (ExpandableListView) findViewById(R.id.expandable_group_routes);
expandableListView.setGroupIndicator(null);
expandableListView.setChildIndicator(null);
listProcess = new ArrayList<Group>();
for(int i = 0; i<=2; i++){
Group obj = new Group();
obj.setId(String.valueOf(i));
obj.setGroup(String.valueOf("Group "+i));
obj.setCod("Cod Group "+String.valueOf(i));
listProcess.add(obj);
}
HashMap<Group, List<Equipament>> allChildItems = returnGroupedChildItems();
expandableListViewAdapter =
new AdapterExpandableListGroups(GroupRoutesActivity.this,listProcess, allChildItems);
expandableListView.setAdapter(expandableListViewAdapter);
expandableListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Equipament e = ((Equipament)expandableListViewAdapter.getChild(groupPosition, childPosition));
Toast.makeText(ctx, e.getCodEquipament(), Toast.LENGTH_LONG).show();
return true;
}
});
}
private HashMap<Group, List<Equipament>> returnGroupedChildItems() {
HashMap<Group, List<Equipament>> childContent = new HashMap<Group, List<Equipament>>();
List<Equipament> equipaments = new ArrayList<Equipament>();
for(int i=0; i<=2; i++){
Equipament e = new Equipament();
e.setId(String.valueOf(i));
e.setType("Equipamemt "+i);
e.setCodEquipament("Cod Equip. "+String.valueOf(i));
equipaments.add(e);
childContent.put(listProcess.get(i), equipaments);
}
return childContent;
}
AdapterExpandableList:
public class AdapterExpandableListGroups extends BaseExpandableListAdapter{
private Context context;
private ArrayList<Group> list;
private HashMap<Group, List<Equipament>> childDataSource;
private static Activity act;
public static int groupPosition = 0, childPosition = 0;
public AdapterExpandableListGroups(Context context, ArrayList<Group> list, HashMap<Group, List<Equipament>> child) {
this.context = context;
this.list = list;
this.childDataSource = child;
}
@Override
public int getGroupCount() {
return this.list.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return this.childDataSource.get(this.list.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this.childDataSource.get(list.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
static class ViewHolderParentGroup {
LinearLayout linearBackground;
TextView textCod;
TextView textDesc;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolderParentGroup viewHolder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout_row_group_expandable, null);
viewHolder = new ViewHolderParentGroup();
viewHolder.linearBackground = (LinearLayout) convertView.findViewById(R.id.linear_background_exapandable_group);
viewHolder.textCod = (TextView) convertView.findViewById(R.id.text_cod_group);
viewHolder.textDesc = (TextView) convertView.findViewById(R.id.text_desc_group);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolderParentGroup) convertView.getTag();
}
viewHolder.textCod.setText(list.get(groupPosition).getCod());
viewHolder.textDesc.setText(list.get(groupPosition).getGroup());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolderParentGroup viewHolder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout_row_equipament_expandable, null);
viewHolder = new ViewHolderParentGroup();
viewHolder.linearBackground = (LinearLayout) convertView.findViewById(R.id.linear_background_exapandable_equipament);
viewHolder.textCod = (TextView) convertView.findViewById(R.id.text_cod_equipament);
viewHolder.textDesc = (TextView) convertView.findViewById(R.id.text_desc_equipament);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolderParentGroup) convertView.getTag();
}
Equipament e = ((Equipament)getChild(groupPosition, childPosition));
viewHolder.textCod.setText(e.getCodEquipament());
viewHolder.textDesc.setText(e.getType());
viewHolder.linearBackground.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Equipament e = ((Equipament)getChild(AdapterExpandableListGroups.groupPosition, AdapterExpandableListGroups.childPosition));
//Toast toast = Toast.makeText(context, e.getCodEquipament(),Toast.LENGTH_SHORT);
//toast.show();
}
});
this.groupPosition = groupPosition;
this.childPosition = childPosition;
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
et ma mise en page de ExpandableList:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9">
<ExpandableListView
android:id="@+id/expandable_group_routes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:childDivider="#000"
android:dividerHeight="0sp"
android:divider="#000"/>
</LinearLayout>