[Java for Android App Development] alawibh:
Dears,

I have an expandable list view in Android application, I want to achieve the following:

When long click on group item, all its children should be selected.

vehicle_group_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

<TextView
android:id="@+id/vehicle_group_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="25dp"/>

</LinearLayout>

vehicle_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""""
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp" >

<TextView
android:id="@+id/vehicle_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingLeft="25dp" />

<ImageView
android:id="@+id/is_selected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>

</RelativeLayout>

VehicleListAdapter.java:

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class VehicleListAdapter extends BaseExpandableListAdapter {

private Activity context;
private Map<String, List<String>> vehicleGroupCollection;
private List<String> vehicleGroupsList;

public VehicleListAdapter(Activity context, List<String> vehicleGroupsList,
Map<String, List<String>> vehicleGroupCollection) {
this.context = context;
this.vehicleGroupsList = vehicleGroupsList;
this.vehicleGroupCollection = vehicleGroupCollection;
}

public Object getChild(int groupPosition, int childPosition) {
return vehicleGroupCollection.get(vehicleGroupsList.get(groupPosition)).get(childPosition);
}

public Object getChildren(int groupPosition) {
return vehicleGroupCollection.get(vehicleGroupsList.get(groupPosition));
}

public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String vehicle = (String) getChild(groupPosition, childPosition);
LayoutInflater inflater = context.getLayoutInflater();

if (convertView == null) {
convertView = inflater.inflate(R.layout.vehicle_item, null);
}

TextView item = (TextView) convertView.findViewById(R.id.vehicle_name);
ImageView isSelected = (ImageView) convertView.findViewById(R.id.is_selected);

item.setText(vehicle);
return convertView;
}

public int getChildrenCount(int groupPosition) {
return vehicleGroupCollection.get(vehicleGroupsList.get(groupPosition)).size();
}

public Object getGroup(int groupPosition) {
return vehicleGroupsList.get(groupPosition);
}

public int getGroupCount() {
return vehicleGroupsList.size();
}

public long getGroupId(int groupPosition) {
return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {

String groupName = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.vehicle_group_item,
null);
}
TextView item = (TextView) convertView.findViewById(R.id.vehicle_group_name);
item.setTypeface(null, Typeface.BOLD);
item.setText(groupName);
return convertView;
}

public boolean hasStableIds() {
return true;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

}

In main activity OnCreate method:

createGroupList();
createCollection();

MyExpandableList = (ExpandableListView) findViewById(R.id.MyExpandableList);
final ExpandableListAdapter expListAdapter = new VehicleListAdapter(
this, vehicleGroupList, vehicleGroupCollection);
MyExpandableList.setAdapter(expListAdapter);

MyExpandableList.setLongClickable(true);
MyExpandableList.setOnItemLongClickListener(new OnItemLongClickListener() {

[MENTION=439709]override[/MENTION]
public boolean onItemLongClick(AdapterView<?> parent, View v,
int groupPosition, long id) {
int itemType = ExpandableListView.getPackedPositionType(id);

if(itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP){

List<String> mySelectedCollection = (List<String>) ((VehicleListAdapter) expListAdapter).getChildren(groupPosition);

if(mySelectedCollection != null){
for (String vehiclepName : mySelectedCollection) {
Toast.makeText(getBaseContext(), vehiclepName, Toast.LENGTH_LONG).show();
}

return true;
}
else{
return false;
}
}

return true;
}

});

The output of the main activity code is Toasting all children text on screen.

But, what I want to do, is to set "is_selected" in "ImageView" with Drawable image.

After long click on the group item, ALL children of this group get selected.

Regards,