Android 如何将图像图标添加到expandablelistview子列表

Android 如何将图像图标添加到expandablelistview子列表,android,android-layout,android-listview,expandablelistview,Android,Android Layout,Android Listview,Expandablelistview,我试图将图像图标添加到expandablelistview子列表。我无法正确执行此操作。 在我的应用程序中,子列表从API(JSON)加载数据 我想将图像添加到不同的数据中。例如address.png+类似wise的地址 有人能帮我创造它吗 这是我的主要活动代码 public void ListDrwaer() { listDataHeader = new ArrayList<String>(); listDataChild = new HashMa

我试图将图像图标添加到expandablelistview子列表。我无法正确执行此操作。 在我的应用程序中,子列表从API(JSON)加载数据

我想将图像添加到不同的数据中。例如address.png+类似wise的地址

有人能帮我创造它吗

这是我的主要活动代码

public void ListDrwaer() {

        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();
        List<String> restData;

        try {
            JSONObject jsonResponse = new JSONObject(strJson1);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("restaurants");



            for (int i = 0; i < jsonMainNode.length(); i++) {

                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String restName = jsonChildNode.optString("name");
                String address = "Address: "+jsonChildNode.optString("address");
                String mobile = "Contact No: "+jsonChildNode.optString("mobile");
                String direction = "Direction: "+jsonChildNode.optString("direction");
                String bestTime = "BestTime to visite: "+jsonChildNode.optString("bestTime");
                String food = "Food: "+jsonChildNode.optString("food");
                String dress = "Dress: "+jsonChildNode.optString("dress");
                String priceRange = "Price Range: "+jsonChildNode.optString("priceRange");
                //String url = jsonChildNode.optString("priceRange");

                listDataHeader.add(restName);
                restData = new ArrayList<String>();
                restData.add(address);
                restData.add(mobile);
                restData.add(direction);
                restData.add(bestTime);
                restData.add(food);
                restData.add(dress);
                restData.add(priceRange);

                listDataChild.put(restName, restData);

            }


        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), "Error..." + e.toString(),
                    Toast.LENGTH_LONG).show();
        }

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);


    }


}
public void ListDrwaer(){
listDataHeader=新的ArrayList();
listDataChild=newHashMap();
列出数据;
试一试{
JSONObject jsonResponse=新的JSONObject(strJson1);
JSONArray jsonMainNode=jsonResponse.optJSONArray(“餐厅”);
for(int i=0;i
这是适配器类

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    ImageView image;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    Integer[] images = { R.drawable.address,
            R.drawable.clock, R.drawable.location };

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.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);

        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);
        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);
        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);

        return convertView;
    }

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

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



}
公共类ExpandableListAdapter扩展了BaseExpandableListAdapter{
私人语境(private Context)(私人语境);;
私有列表_listDataHeader;//头标题
//标题标题、子标题格式的子数据
私有HashMap_listDataChild;
图像视图图像;
公共ExpandableListAdapter(上下文、列表listDataHeader、,
HashMap listChildData){
这._context=context;
这。_listDataHeader=listDataHeader;
这。_listDataChild=listChildData;
}
整数[]图像={R.drawable.address,
R.drawable.clock,R.drawable.location};
@凌驾
公共对象getChild(int-groupPosition、int-ChildPosition){
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.get(childpositionon);
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共视图getChildView(int groupPosition,final int childPosition,
布尔值isLastChild、视图转换视图、视图组父级){
最终字符串childText=(字符串)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_项,空);
}
TextView txtListChild=(TextView)convertView
.findviewbyd(R.id.lblListItem);
setText(childText);
返回视图;
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回此。_listDataChild.get(此。_listDataHeader.get(groupPosition))
.size();
}
@凌驾
公共对象getGroup(int-groupPosition){
返回此。\u listDataHeader.get(groupPosition);
}
@凌驾
public int getGroupCount(){
返回此值。_listDataHeader.size();
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共视图getGroupView(int-groupPosition,布尔值isExpanded,
视图(视图、视图组父级){
字符串头文件=(字符串)getGroup(groupPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)this.\u上下文
.getSystemService(上下文布局\充气机\服务);
convertView=infalInflater.充气(R.layout.list_组,空);
}
TextView lblistheader=(TextView)convertView
.findviewbyd(R.id.lblistheader);
lblListHeader.setTypeface(null,Typeface.BOLD);
lblListHeader.setText(标题);
返回视图;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
}
这是子列表xml文件

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


    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:textColor="#daac56"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"/>

</LinearLayout>

您已经通过以下行将文本添加到
baseexpandablelistatadapter
中的
TextView

TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);    
txtListChild.setText(childText);
对您的图像执行相同的操作:

ImageView imgViewChild = (ImageView ) convertView
                .findViewById(R.id.img);    
imgViewChild.setImageResource(R.drawable.address);

您已经将地址添加到
文本视图中了。这与
图像视图
的方式相同。只需找到视图并设置图像。我真的不明白你有什么问题。你能解释一下“我做得不对”是什么意思吗?是的。我想添加图像视图,然后添加文本视图。我不明白如何将imageview添加到此子列表。@Anurudhika您找到