I am using expandablelistview in my project,and I want to add different images for group view and also in child view I want to do same,but its not working,following is my code..
MainActivity.java
public class MainActivity extends Activity {
ExpandableTests listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
private List<Integer> group;
private HashMap<Integer, List<Integer>> Child;
Expandableimageadapter imgsadadapter;
public List<Integer> groupImages;
public HashMap<Integer, List<Integer>> childImages;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
expListView.setGroupIndicator(null);
//imgsadadapter=new Expandableimageadapter(this, groupImages, childImages);
// listAdapter = new ExpandableTests(this, listDataHeader, group, listDataChild, Child);
listAdapter = new ExpandableTests(this, listDataHeader, groupImages, listDataChild, childImages);
// setting list adapter
expListView.setAdapter(listAdapter);
//expListView.setAdapter(imgsadadapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
groupImages= new ArrayList<Integer>();
groupImages.add(R.drawable.adminsetting);
groupImages.add(R.drawable.productcategory);
// groupImages.add(R.drawable.drinks);
// groupImages.add(R.drawable.deserts);
childImages = new HashMap<Integer, List<Integer>>();
List<Integer> Maincat = new ArrayList<Integer>();
Maincat.add(R.drawable.productcategory);
Maincat.add(R.drawable.productcategory);
Maincat.add(R.drawable.productcategory);
Maincat.add(R.drawable.productcategory);
Maincat.add(R.drawable.productcategory);
// vegetablesi.add(R.drawable.Onions);
// vegetablesi.add(R.drawable.Garlic);
List<Integer> subcat = new ArrayList<Integer>();
subcat.add(R.drawable.productcategory);
subcat.add(R.drawable.productcategory);
subcat.add(R.drawable.productcategory);
subcat.add(R.drawable.productcategory);
// fruitsi.add(R.drawable.Kiwifruit);
// fruitsi.add(R.drawable.Grape);
/* List<Integer> drinksi = new ArrayList<Integer>();
drinksi.add(R.drawable.Vodka);
drinksi.add(R.drawable.Milk);
drinksi.add(R.drawable.Water);
drinksi.add(R.drawable.CocaCola);
drinksi.add(R.drawable.Sprite);
List<Integer> desertsi = new ArrayList<Integer>();
desertsi.add(R.drawable.Vodka);
desertsi.add(R.drawable.Milk);
desertsi.add(R.drawable.Water);
desertsi.add(R.drawable.CocaCola);
desertsi.add(R.drawable.Sprite);*/
// childImages.put(groupImages.get(0), Maincat);
// childImages.put(groupImages.get(1), subcat);
childImages.put(0, Maincat);
childImages.put(1, subcat);
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Admin Setting");
listDataHeader.add("Product & Category");
listDataHeader.add("Client & Orders");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("State City");
top250.add("Sales Person");
top250.add("City Sales Person");
top250.add("Product Boxtype");
top250.add("Shipping Carrier");
//top250.add("The Dark Knight");
// top250.add("12 Angry Men");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("Product");
nowShowing.add("Category");
nowShowing.add("Category Products");
nowShowing.add("Product Categories");
//nowShowing.add("Red 2");
//nowShowing.add("The Wolverine");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("Client");
comingSoon.add("Client Products");
comingSoon.add("Client Orders");
comingSoon.add("Client Orders Delivery");
//comingSoon.add("Europa Report");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
ExpandableTests.java
public class ExpandableTests extends BaseExpandableListAdapter{
private Context _context;
private List<Integer> _group;
private HashMap<Integer, List<Integer>> _Child;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableTests(Context context, List<String> listDataHeader,List<Integer> group,
HashMap<String, List<String>> listDataChild, HashMap<Integer, List<Integer>> Child) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listDataChild;
this._group=group;
this._Child=Child;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
// this._Child.get(this._group.get(groupPosition)).get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
// final int childimg = (Integer) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
ImageView imgListChild = (ImageView) convertView.findViewById(R.id.childsid);
final int childimg = _Child.get(groupPosition).get(childPosition);
imgListChild.setImageResource(childimg);
//imgListChild.setImageResource(childimg);
/*ImageView imageView = (ImageView) convertView.findViewById(R.id.childsid);
final int imageId = this.childImages.get(this.groupImages.get(groupPosition)).get(childPosition);
imageView.setImageResource(imageId);*/
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
//int headerimg = (Integer) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
// lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
ImageView imgsListHeader = (ImageView) convertView
.findViewById(R.id.gropids);
int headerimg = _group.get(groupPosition);
imgsListHeader.setImageResource(headerimg);
// lblListHeader.setTypeface(null, Typeface.BOLD);
//imgsListHeader.setImageResource(R.drawable.ic_launcher);
/*ImageView imageView = (ImageView) convertView.findViewById(R.id.gropids);
int imageId = this.groupImages.get(groupPosition);
imageView.setImageResource(imageId);*/
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
See Question&Answers more detail:
os