Java 如何在可展开的Listview中为每个组添加不同的图像

Java 如何在可展开的Listview中为每个组添加不同的图像,java,android,expandablelistview,Java,Android,Expandablelistview,在我的可展开列表视图中,有三个组。我想为该组添加不同的三个图像。我尝试了很多代码,但都不起作用 我的代码: 主要活动: HashMap<String, List<String>> rightDrawerListDetail = getData(); List<String> rightDrawerListTitle = new ArrayList<String>(rightDrawerListDetail.keySet()); adapter

在我的可展开列表视图中,有三个组。我想为该组添加不同的三个图像。我尝试了很多代码,但都不起作用

我的代码:

主要活动:

HashMap<String, List<String>> rightDrawerListDetail = getData();
List<String> rightDrawerListTitle = new ArrayList<String>(rightDrawerListDetail.keySet());
   adapterR = new CustomExpandableListAdapter(this, rightDrawerListTitle,rightDrawerListDetail);
    mRightDrawerList.setAdapter(adapterR);


private HashMap<String, List<String>> getData(){

    HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
    ArrayList<String> arrayList = mydb.getAllAddress();

    List asRider = new ArrayList();
    asRider.add("hello");

    List asRidee = new ArrayList();
    asRidee.add("hai");

    List recent = new ArrayList();
    recent.add("Success");

    expandableListDetail.put("As a Rider",asRider);
    expandableListDetail.put("As a Ridee",asRidee);
    expandableListDetail.put("My Recent Activities",recent);


    return expandableListDetail;
}
HashMap rightDrawerListDetail=getData();
List rightDroperListTitle=newArrayList(rightDroperListDetail.keySet());
Adapter=新的CustomExpandableListAdapter(此,rightDrawerListTitle,rightDrawerListDetail);
mRightDrawerList.setAdapter(适配器错误);
私有HashMap getData(){
HashMap expandableListDetail=新HashMap();
ArrayList ArrayList=mydb.getAllAddress();
List asRider=new ArrayList();
asRider.add(“你好”);
List asRidee=new ArrayList();
asRidee.添加(“hai”);
列表最近=新的ArrayList();
最近。添加(“成功”);
expandableListDetail.put(“作为骑手”,作为骑手);
expandableListDetail.put(“作为一个Ridee”,asRidee);
expandableListDetail.put(“我最近的活动”,最近);
返回expandableListDetail;
}
CustomExpandableListAdapter.java:

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

private Context mContext;
private List<String> mGroups;
private LayoutInflater mInflater;
private HashMap<String, List<String>> mexpandableListDetail;

public CustomExpandableListAdapter(Context context, List<String> groups,HashMap<String, List<String>> expandableListDetail) {
    mContext = context;
    mGroups = groups;
    mexpandableListDetail = expandableListDetail;
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getGroupCount() {
    return mGroups.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return mexpandableListDetail.get(mGroups.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return mGroups.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return mexpandableListDetail.get(mGroups.get(groupPosition)).get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return 0;
}

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

@Override
public boolean hasStableIds() {
    return false;
}

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

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.right_drawer_group, null);
    }

    // Get the group item
    String listTitle = (String) getGroup(groupPosition);
    // Set group name
    TextView textView = (TextView) convertView.findViewById(R.id.textGroup);
    textView.setText(listTitle);
    ImageView indicator = (ImageView) convertView.findViewById(R.id.groupIndicator);
    if (isExpanded) {
        indicator.setImageResource(R.drawable.arrowup);
    } else {
        indicator.setImageResource(R.drawable.arrowdown);
    }

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {


    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.right_drawer_child, null);
    }

    // Get child name
    String children = (String) getChild(groupPosition, childPosition);

    // Set child name
    TextView text = (TextView) convertView.findViewById(R.id.textChild);
    text.setText(children);

    /*convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(mContext, children, Toast.LENGTH_SHORT).show();
        }
    });*/

    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
公共类CustomExpandableListAdapter扩展了BaseExpandableListAdapter{
私有上下文;
私人名单管理组;
私人停车场;
私有HashMap mexpandableListDetail;
公共CustomExpandableListAdapter(上下文上下文、列表组、HashMap expandableListDetail){
mContext=上下文;
mGroups=组;
mexpandableListDetail=expandableListDetail;
mInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u充气机\u服务);
}
@凌驾
public int getGroupCount(){
返回mGroups.size();
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回mexpandablelistdail.get(mGroups.get(groupPosition)).size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回mGroups.get(groupPosition);
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
返回mexpandablelistdail.get(mGroups.get(groupPosition)).get(childPosition);
}
@凌驾
公共长getGroupId(int-groupPosition){
返回0;
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回0;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共视图getGroupView(int groupPosition、布尔isExpanded、视图convertView、视图组父级){
if(convertView==null){
convertView=mInflater.充气(右布局右抽屉组,空);
}
//获取组项目
String listTitle=(String)getGroup(groupPosition);
//设置组名
TextView TextView=(TextView)convertView.findViewById(R.id.textGroup);
setText(listTitle);
ImageView指示器=(ImageView)convertView.findViewById(R.id.groupIndicator);
如果(扩展){
指示器.setImageResource(R.drawable.arrowup);
}否则{
指示器.setImageResource(R.可绘制.箭头向下);
}
返回视图;
}
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、布尔isLastChild、视图convertView、视图组父级){
if(convertView==null){
convertView=mInflater.inflate(右布局右抽屉子项,空);
}
//获取子名称
String children=(String)getChild(groupPosition,childPosition);
//设置子名称
TextView text=(TextView)convertView.findViewById(R.id.textChild);
text.setText(儿童);
/*convertView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Toast.makeText(mContext,children,Toast.LENGTH_SHORT).show();
}
});*/
返回视图;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
}

请任何人帮帮我


提前感谢

在您右侧的\u drawer\u group.xml中添加一个图像视图,将您的组图像放置在该视图中,例如groupImage

在getGroupView中:

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

if (convertView == null) {
    convertView = mInflater.inflate(R.layout.right_drawer_group, null);
}
//Set group image
ImageView groupImage = (ImageView)convertView.findViewById(R.id.groupImage);
switch((String)getGroup(groupPosition))
{
 case "As a Rider":
  //groupImage set correct image
  break;
 case "As a Ridee":
  //groupImage set correct image
  break;
 case "My Recent Activities":
  //groupImage set correct image
  break;
}

// Get the group item
String listTitle = (String) getGroup(groupPosition);
// Set group name    
TextView textView = (TextView) convertView.findViewById(R.id.textGroup);
textView.setText(listTitle);
ImageView indicator = (ImageView) convertView.findViewById(R.id.groupIndicator);
if (isExpanded) {
    indicator.setImageResource(R.drawable.arrowup);
} else {
    indicator.setImageResource(R.drawable.arrowdown);
}

return convertView;
}
这不是最简洁的解决方案,因为如果您更改组名,这将导致中断,但它现在可以工作。更好的解决方案是将组作为包含getName和getImageId的对象传递