简体   繁体   中英

How to add image icon to expandablelistview child list

I tried to add image icon to expandablelistview child list.I couldn't do it correctly. In my application, child list load data from API(JSON) .

I want to add image to that different data. for an example address.png + address like wise.

Can anyone help me to create it?

This is my Main Activity code.

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


    }


}

This is Adapter class

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



}

This is child list xml file

<?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>

You already adding your text to your TextView in your BaseExpandlableListAdapter by the following lines:

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

Do the same for your image:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM