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

时间:2014-01-07 09:13:15

标签: android android-layout android-listview expandablelistview

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

我想将图像添加到不同的数据中。例如address.png +地址就像明智的一样。

任何人都可以帮我创建吗?

这是我的主要活动代码。

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



}

这是子列表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>

1 个答案:

答案 0 :(得分:0)

您已按以下方式将文字添加到TextView BaseExpandlableListAdapter中:

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

为您的形象做同样的事情:

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