如何将图像添加到ExpandableListView的子项?

时间:2017-04-13 16:57:26

标签: java android image expandablelistview

所以我有一个ExpandableListView,每组有多个子节点,我希望能够有选择地将特定图像添加到特定组的特定子节点。

我的list_item xml同时包含TextView和ImageView,我目前每个组的每个孩子都填充一个图像。但是,我无法克服指定哪个孩子获得什么图像的障碍。

我的ExpandableListAdapter.java(仅包含相关代码)

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;
import java.util.HashMap;
import java.util.List;

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> listDataHeader;
    private HashMap<String, List<String>> listHashMap;

    public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
    this.context = context;
    this.listDataHeader = listDataHeader;
    this.listHashMap = listHashMap;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final String childText = (String)getChild(groupPosition, childPosition);
        if(convertView==null){
            LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item,null);
        }

        ImageView childImage = (ImageView)convertView.findViewById(R.id.lblListItemImg);
        childImage.setImageResource(R.drawable.image);

        TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
        txtListChild.setText(childText);
        return convertView;
    }

}

我的list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="55dip">

    <TextView
        android:id="@+id/lblListItem"
        android:textSize="16sp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:linksClickable="true"
        android:autoLink="web"/>

    <ImageView
        android:id="@+id/lblListItemImg"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        />
</LinearLayout>

我尝试按照这里的建议,但我不能让数组[childPosition]工作。我错过了什么? How can i add different images from drawable file in ExpandableListView in android?

Android开发方面的新手,感谢您的帮助。

0 个答案:

没有答案