Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-如何为ExpandableListView的每个子级设置不同的图标_Android - Fatal编程技术网

Android-如何为ExpandableListView的每个子级设置不同的图标

Android-如何为ExpandableListView的每个子级设置不同的图标,android,Android,我有这个java代码,我有两个问题: 我想为每个家长的每个孩子设置一个不同的图标,但使用此代码,我只能为所有孩子设置相同的图标,或者为位于X位置的孩子设置一个图标,为另一个孩子设置另一个图标 例如,当我按下名为“Apple”的项目时,我是如何开始一项活动的 我怎样才能解决我的问题?多谢各位 public class sedactivity extends ExpandableListActivity { ExpandableListAdapter mAdapter; priva

我有这个java代码,我有两个问题:

  • 我想为每个家长的每个孩子设置一个不同的图标,但使用此代码,我只能为所有孩子设置相同的图标,或者为位于X位置的孩子设置一个图标,为另一个孩子设置另一个图标

  • 例如,当我按下名为“Apple”的项目时,我是如何开始一项活动的

  • 我怎样才能解决我的问题?多谢各位

    public class sedactivity extends ExpandableListActivity { 
        ExpandableListAdapter mAdapter;
        private final static String NAME = "NAME";
        private final static String SURNAME = "SURNAME";
        private Resources res;
        private Drawable photo, photo2, photo3, photo4;
        private List<Drawable> albumCovers;
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.list_main);
    
                // Create a List of Drawables to insert into the Expandable List
                // This can be completely dynamic
                res = this.getResources();
                photo = (Drawable) res.getDrawable(R.drawable.bee);
                photo2 = (Drawable) res.getDrawable(R.drawable.astro);
                photo3 = (Drawable) res.getDrawable(R.drawable.bomb);
                photo4 = (Drawable) res.getDrawable(R.drawable.apple);
                albumCovers = new ArrayList<Drawable>();
                albumCovers.add(photo);
                albumCovers.add(photo2);
    
                // The following code simply generates the Expandable Lists content (Strings)
                List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
                List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
                Map<String, String>
    
    
                //List A
                curGroupMap = new HashMap<String, String>();
                groupData.add(curGroupMap);
    
                curGroupMap.put(NAME, "A");
                curGroupMap.put(SURNAME, "(2 Photos)");
    
    
    
                List<Map<String, String>> children = new ArrayList<Map<String, String>>();
                Map<String, String> curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
    
    
                curChildMap.put(NAME, "Apple");
    
    
                curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
    
                curChildMap.put(NAME, "Astro");
    
                curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
    
    
                childData.add(children);
    
    
                //List B
                curGroupMap = new HashMap<String, String>();
                groupData.add(curGroupMap);
    
    
                curGroupMap.put(NAME, "B");
                curGroupMap.put(SURNAME, "(2 Photos)");
    
                children = new ArrayList<Map<String, String>>();
    
                curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
    
                curChildMap.put(NAME, "Bee");
    
                curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
    
                curChildMap.put(NAME, "Bomb");
    
                curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
    
    
                childData.add(children);
    
    
                //List C
                curGroupMap = new HashMap<String, String>();
                groupData.add(curGroupMap);
    
    
                curGroupMap.put(NAME, "C");
                curGroupMap.put(SURNAME, "(0 Photo)");
    
                children = new ArrayList<Map<String, String>>();
    
    
                childData.add(children);
    
                // Set up our adapter
                mAdapter = new MyExpandableListAdapter(
                                this,
                                groupData,
                                R.layout.list_parent,
                                new String[] { NAME, SURNAME },
                                new int[] { R.id.rowText1, R.id.rowText2,R.id.photoAlbumImg },
                                childData,
                                R.layout.list_child,
                                new String[] { NAME, SURNAME },
                                new int[] { R.id.rowText1, R.id.photoAlbumImg }
                        );
                setListAdapter(mAdapter);
                registerForContextMenu(getExpandableListView());
        }
    
    
        /**
         * A simple adapter which allows you to bind data to specific
         * Views defined within the layout of an Expandable Lists children
         * (Implement getGroupView() to define the layout of parents)
         */
    
    
        public class MyExpandableListAdapter extends SimpleExpandableListAdapter {
    
                private List<? extends List<? extends Map<String, ?>>> mChildData;
                private String[] mChildFrom;
                private int[] mChildTo;
    
                public MyExpandableListAdapter(Context context,
                                List<? extends Map<String, ?>> groupData, int groupLayout,
                                String[] groupFrom, int[] groupTo,
                                List<? extends List<? extends Map<String, ?>>> childData,
                                int childLayout, String[] childFrom, int[] childTo) {
                        super(context, groupData, groupLayout, groupFrom, groupTo,
                              childData, childLayout, childFrom, childTo);
    
                        mChildData = childData;
                        mChildFrom = childFrom;
                        mChildTo = childTo;
    
                }
    
                public View getChildView(int groupPosition, int childPosition,
                        boolean isLastChild, View convertView, ViewGroup parent) {
    
                View v;
                if (convertView == null) {
                        v = newChildView(isLastChild, parent);
                } else {
                        v = convertView;
                }
                bindView(v, mChildData.get(groupPosition).get(childPosition), mChildFrom,
                                mChildTo, groupPosition, childPosition);
                return v;
    
                }
    
             // This method binds my data to the Views specified in the child xml layout
                private void bindView(View view, Map<String, ?> data, String[] from, int[] to, int groupPosition, int childPosition) {
                        int len = to.length - 1;
                        // Apply TextViews
                        for (int i = 0; i < len; ++i) {
                                TextView v = (TextView) view.findViewById(to[i]);
                                if (v != null) {
                                        v.setText((String) data.get(from[i]));
                                }
                                // Apply ImageView
                                ImageView imgV = (ImageView) view.findViewById(to[1]);
                                if (imgV != null) {
                                    if(childPosition % 1 == 0) imgV.setImageDrawable(albumCovers.get(0));
                                    else imgV.setImageDrawable(albumCovers.get(1));
                            }
    
    
                        }
    
                }
        }
    }
    
    公共类sedactivity扩展了ExpandableListActivity{
    可扩展列表适配器;
    私有最终静态字符串NAME=“NAME”;
    私有最终静态字符串姓氏=“姓氏”;
    私人资源;
    私人可画照片,照片2,照片3,照片4;
    私人名单封面;
    @凌驾
    创建时的公共void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_main);
    //创建要插入到可展开列表中的可展开列表
    //这可以是完全动态的
    res=this.getResources();
    photo=(Drawable)res.getDrawable(R.Drawable.bee);
    photo2=(可绘制)res.getDrawable(R.Drawable.astro);
    photo3=(可绘制)res.getDrawable(R.Drawable.bomb);
    photo4=(可绘制)res.getDrawable(R.Drawable.apple);
    albumCovers=新的ArrayList();
    相册封面。添加(照片);
    专辑封面。添加(照片2);
    //下面的代码只是生成可扩展的列表内容(字符串)
    List groupData=new ArrayList();
    List childData=new ArrayList();
    地图
    //列举
    curGroupMap=newhashmap();
    groupData.add(curGroupMap);
    curGroupMap.put(名称“A”);
    curGroupMap.put(姓氏,“(2张照片)”);
    List children=new ArrayList();
    Map curChildMap=newhashmap();
    添加(curChildMap);
    curChildMap.put(名称“Apple”);
    curChildMap=newhashmap();
    添加(curChildMap);
    curChildMap.put(名称“Astro”);
    curChildMap=newhashmap();
    添加(curChildMap);
    添加(子项);
    //名单B
    curGroupMap=newhashmap();
    groupData.add(curGroupMap);
    curGroupMap.put(名称“B”);
    curGroupMap.put(姓氏,“(2张照片)”);
    children=newarraylist();
    curChildMap=newhashmap();
    添加(curChildMap);
    curChildMap.put(名称“Bee”);
    curChildMap=newhashmap();
    添加(curChildMap);
    curChildMap.put(名称“炸弹”);
    curChildMap=newhashmap();
    添加(curChildMap);
    添加(子项);
    //清单C
    curGroupMap=newhashmap();
    groupData.add(curGroupMap);
    curGroupMap.put(名称“C”);
    curGroupMap.put(姓氏,“(0张照片)”);
    children=newarraylist();
    添加(子项);
    //设置适配器
    mAdapter=新的MyExpandableListAdapter(
    这
    分组数据,
    R.layout.list\u父级,
    新字符串[]{名称,姓氏},
    新int[]{R.id.rowText1,R.id.rowText2,R.id.photoAlbumImg},
    儿童数据,
    R.layout.list_子对象,
    新字符串[]{名称,姓氏},
    新int[]{R.id.rowText1,R.id.photoAlbumImg}
    );
    setListAdapter(mAdapter);
    registerForContextMenu(getExpandableListView());
    }
    /**
    *一个简单的适配器,允许您将数据绑定到特定的
    *在可展开列表的布局中定义的视图列出了子视图
    *(实现getGroupView()以定义父级的布局)
    */
    公共类MyExpandableListAdapter扩展了SimpleExpandableListAdapter{
    私人列表>>麦克希尔达;
    私有字符串[]mChildFrom;
    私人机构【】mChildTo;
    公共MyExpandableListAdapter(上下文,
    列表>组数据,int-groupLayout,
    字符串[]groupFrom,int[]groupTo,
    列表>>儿童数据,
    int childLayout,字符串[]childFrom,int[]childTo){
    超级(上下文、groupData、groupLayout、groupFrom、groupTo、,
    childData、childLayout、childFrom、childTo);
    mChildData=childData;
    mChildFrom=childFrom;
    mChildTo=childTo;
    }
    公共视图getChildView(int-groupPosition、int-childPosition、,
    布尔值isLastChild、视图转换视图、视图组父级){
    观点五;
    if(convertView==null){
    v=newChildView(isLastChild,父级);
    }否则{
    v=转换视图;
    }
    bindView(v,mChildData.get(groupPosition).get(childPosition),mChildFrom,
    mChildTo、groupPosition、childPosition);
    返回v;
    }
    //此方法将我的数据绑定到子xml布局中指定的视图
    私有void bindView(视图视图,映射数据,字符串[]from,int[]to,int groupPosition,int childPosition){
    int len=to.length-1;
    //应用文本视图
    对于(int i=0;iimgV.setImageDrawable(albumCovers.get(1))
    
    view.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // TODO: Implement the tasks to be done here 
        }
    });
    
    for (int i = 0; i < len; ++i) {
            TextView v = (TextView) view.findViewById(to[i]);
            if (v != null) {
                    v.setText((String) data.get(from[i]));
            }
            ImageView imgV = (ImageView) view.findViewById(to[1]);
            imgV.setImageResource(getResources().getIdentifier((String) 
                data.get(from[i]), "drawable", "yourdefaultpackage"));
        }
    }
    
    <?xml version="1.0" encoding="UTF-8"?>
    <data>
        <group letter="A">
            <child name="American Bull" img="american_bull" />
            <child name="Arco's" img="arcos" />
        </group>
        <group letter="B">
            <child name="Barter" img="barter" />
            <child name="Bitter B." img="bitter_b" />
            <child name="Bull Hunter" img="bull_hunter" />
            <child name="Bull In Cuba" img="bull_in_cuba" />
            <child name="Bullito" img="bullito" />
        </group>
        <group letter="C">
            <child name="Cackle" img="cackle" />
            <child name="Cheapstake" img="cheapstake" />
            <child name="Checco's Bull" img="checcos_bull" />
            <child name="Cheeriness" img="cheeriness" />
            <child name="Cross Current" img="cross_current" />
            <child name="Cruel" img="cruel" />
        </group>
        <group letter="D"></group>
        <group letter="E"></group>
        <group letter="F">
            <child name="Firearm" img="firearm" />
            <child name="First Of All" img="first_of_all" />
            <child name="First Apple" img="first_apple" />
        </group>
        <group letter="G">
            <child name="Golden" img="golden" />
            <child name="Green Bull" img="green_bull" />
        </group>
        <group letter="H">
            <child name="Hollywood Passion" img="hollywood_passion" />
        </group>
        <group letter="I"></group>
        <group letter="J"></group>
        <group letter="K">
            <child name="Kind Bull" img="kind_bull" />
            <child name="Kingling" img="kingling" />
        </group>
        <group letter="L">
            <child name="Lab" img="lab" />
            <child name="Leary" img="leary" />
            <child name="Leg Pull" img="leg_pull" />
            <child name="Long Bull" img="long_bull" />
            <child name="Lucy" img="lucy" />
        </group>
        <group letter="M">
            <child name="Mayla" img="mayla" />
        </group>
        <group letter="N">
            <child name="New Red Bull Fantasy" img="new_red_bull_fantasy" />
            <child name="Nu Energy" img="nu_energy" />
        </group>
        <group letter="O"></group>
        <group letter="P">
            <child name="Passion" img="passion" />
            <child name="Power" img="power" />
        </group>
        <group letter="Q"></group>
        <group letter="R">
            <child name="Rebellion Red Peach" img="rebellion_red_peach" />
            <child name="Red Bulloska" img="red_bulloska" />
            <child name="Red 28" img="red_28" />
            <child name="Red Bull Wallbanger" img="red_bull_wallbanger" />
            <child name="Red Cell" img="red_cell" />
            <child name="Red Hothead" img="red_hothead" />
            <child name="Red Lime" img="red_lime" />
        </group>
        <group letter="S">
            <child name="Speedy" img="speedy" />
            <child name="Spritz Power" img="spritz_power" />
            <child name="Stiff-Necked" img="stiff_necked" />
        </group>
        <group letter="T"></group>
        <group letter="U"></group>
        <group letter="V">
            <child name="Vedi O' Mare Come E' Bull" img="vedi_o_mare_come_e_bull" />
        </group>
        <group letter="W"></group>
        <group letter="X"></group>
        <group letter="Y"></group>
        <group letter="Z"></group>
        <group letter="#"></group>
    </data>
    
    public class Group
    {
        private String letter;
        private ArrayList<Child> children;
    
        /**
         * @return the letter
         */
        public String getLetter()
        {
            return letter;
        }
    
        /**
         * @param letter the letter to set
         */
        public void setLetter(String letter)
        {
            this.letter = letter;
        }
    
        /**
         * @return the children
         */
        public ArrayList<Child> getChildren()
        {
            return children;
        }
    
        /**
         * @param children the children to set
         */
        public void setChildren(ArrayList<Child> children)
        {
            this.children = children;
        }
    }
    
    public class Child
    {
        private String name;
        private String image;
    
        /**
         * @return the name
         */
        public String getName()
        {
            return name;
        }
    
        /**
         * @param name the name to set
         */
        public void setName(String name)
        {
            this.name = name;
        }
    
        /**
         * @return the image
         */
        public String getImage()
        {
            return image;
        }
    
        /**
         * @param image the image to set
         */
        public void setImage(String image)
        {
            this.image = image;
        }
    }
    
    import java.util.ArrayList;
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    
    public class XmlHandler extends DefaultHandler
    {
        /**
         * The tag- and attribute names in the xml data
         */
        private static final String TAG_GROUP = "group";
        private static final String TAG_CHILD = "child";
        private static final String ATTR_LETTER = "letter";
        private static final String ATTR_NAME = "name";
        private static final String ATTR_IMG = "img";
    
        /**
         * holds the currently processing Group instance
         */
        private Group currentGroup;
        /**
         * holds the currently processing Child instance
         */
        private Child currentChild;
        /**
         * the list of Group objects parsed from the xml
         */
        private ArrayList<Group> records = null;
    
        /**
         * @return the list of Group parsed from the xml
         */
        public ArrayList<Group> getGroups()
        {
            return records;
        }
    
        @Override
        public void startDocument() throws SAXException
        {
            super.startDocument();
            this.records = new ArrayList<Group>();
        }
    
        @Override
        public void startElement(final String Uri, final String localName, final String qName, 
                final Attributes attributes) throws SAXException
        {
            if (localName == null)
                return;
            if (localName.equals(TAG_GROUP))//examining a group tag
            {
                currentGroup = new Group();
                currentGroup.setLetter(attributes.getValue(ATTR_LETTER));//the letter attribute
                currentGroup.setChildren(new ArrayList<Child>());
                records.add(currentGroup);
            }
            else if (localName.equals(TAG_CHILD))//examining a child tag
            {
                currentChild = new Child();
                currentChild.setName(attributes.getValue(ATTR_NAME));//the name attribute
                currentChild.setImage(attributes.getValue(ATTR_IMG));//the img attribute
                currentGroup.getChildren().add(currentChild);
            }
        }
    }
    
    import java.util.ArrayList;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    public class MyExpandableListAdapter extends BaseExpandableListAdapter
    {
        private final LayoutInflater inflater;
        private final ArrayList<Group> groups;
    
        public MyExpandableListAdapter(LayoutInflater inflater, final ArrayList<Group> groups)
        {
            this.inflater = inflater;
            this.groups = groups;
        }
    
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, 
                View convertView, ViewGroup parentView)
        {
            final Group parent = groups.get(groupPosition);
            if (convertView == null) //if the row is null, we create it (by inflation)
                convertView = inflater.inflate(R.layout.list_parent, parentView, false);
            // display the letter of the group:
            ((TextView) convertView.findViewById(R.id.rowText1)).
                setText(parent.getLetter());
            // display the children count of the group:
            ((TextView) convertView.findViewById(R.id.rowText2)).
                setText("(" + parent.getChildren().size() + " Ricette)");
            return convertView;
        }
    
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
                View convertView, ViewGroup parentView)
        {
            final Group group = groups.get(groupPosition);
            final Child child = group.getChildren().get(childPosition);
            if (convertView == null)//if the child row is null, we inflate it
                convertView = inflater.inflate(R.layout.list_child, parentView, false);
            // display the name of the child on the row:
            ((TextView) convertView.findViewById(R.id.rowText1)).setText(child.getName());
            // set the proper icon of the child on the child row:
            ((ImageView) convertView.findViewById(R.id.photoAlbumImg)).
                setImageResource(parentView.getResources().
                getIdentifier(child.getImage(), "drawable", "com.test.com"));
            return convertView;
        }
    
        @Override
        public Object getChild(int groupPosition, int childPosition)
        {
            return groups.get(groupPosition).getChildren().get(childPosition);
        }
    
        @Override
        public long getChildId(int groupPosition, int childPosition)
        {
            return childPosition;
        }
    
        @Override
        public int getChildrenCount(int groupPosition)
        {
            return groups.get(groupPosition).getChildren().size();
        }
    
        @Override
        public Object getGroup(int groupPosition)
        {
            return groups.get(groupPosition);
        }
    
        @Override
        public int getGroupCount()
        {
            return groups.size();
        }
    
        @Override
        public long getGroupId(int groupPosition)
        {
            return groupPosition;
        }
    
        @Override
        public void notifyDataSetChanged()
        {
            super.notifyDataSetChanged();
        }
    
        @Override
        public boolean isEmpty()
        {
            return ((groups == null) || groups.isEmpty());
        }
    
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition)
        {
            return true;
        }
    
        @Override
        public boolean hasStableIds()
        {
            return true;
        }
    
        @Override
        public boolean areAllItemsEnabled()
        {
            return true;
        }
    }
    
    import java.util.ArrayList;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import android.app.ExpandableListActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.widget.ExpandableListAdapter;
    
    /**
     * Demonstrates expandable lists using a custom {...@link ExpandableListAdapter} 
     * from {...@link BaseExpandableListAdapter}.
     */
    public class testactivity extends ExpandableListActivity
    {
        ExpandableListAdapter mAdapter;
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.list_main);
            // the next line removes the arrow from the expandable list's group rows,
            // that indicates whether the group is expanded or not.
            // I've removed, because this way it looks nicer.
            getExpandableListView().setGroupIndicator(null);
    
            // read in the list of groups from the xml data
            final ArrayList<Group> groups = readGroupsFromXml();
    
            // Set up our adapter
            mAdapter = new MyExpandableListAdapter(getLayoutInflater(), groups);
            setListAdapter(mAdapter);
            registerForContextMenu(getExpandableListView());
        }
    
        /**
         * Parses the data.xml file located in the /res/raw/ folder, 
         * and builds up the list of Group.
         * @return the list of Group instances to display in the list
         */
        public ArrayList<Group> readGroupsFromXml()
        {
            try
            {
                // initialize our handler
                final XmlHandler handler = new XmlHandler();
                // instantiate a SAXParser for parsing the xml document
                final SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
                // parse the /res/raw/data.xml file 
                // (referenced by R.raw.data) using our handler
                sp.parse(getApplicationContext().getResources().
                        openRawResource(R.raw.data), handler);
                // return the list of Group built inside the handler
                return handler.getGroups();
            }
            catch (Exception e)
            {
                Log.e("Error", "xml", e);
            }
            return null;
        }
    
        // Creo il menu
        @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            super.onCreateOptionsMenu(menu);
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.menu, menu);
            return true;
        }
    
        // Creo le azioni per i tasti del menu
        @Override
        public boolean onOptionsItemSelected(MenuItem item)
        {
            super.onOptionsItemSelected(item);
            switch (item.getItemId())
            {
                case R.id.Home:
                    return true;
                case R.id.Cerca:
                    return true;
                case R.id.Glossario:
                    // Inserire azione
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
        }
    }