如何在Android中获取可扩展列表视图的子值? [英] how to get child value of expandable listview in android?

查看:76
本文介绍了如何在Android中获取可扩展列表视图的子值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用setonItemclick侦听器方法获取可扩展列表视图的子值.我的代码如下,但是click事件无法正常工作.

I want to know how to get the child value of an expandable listview using setonItemclick listener method. My code is given below but click event is not working properly.

mExpandableListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int groupPosition,
                long id) {
            // TODO Auto-generated method stub
            int itemType = ExpandableListView.getPackedPositionType(groupPosition);
            Log.i("item type",""+itemType);
            if ( itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
                  childPosition = ExpandableListView.getPackedPositionChild(id);
                  groupPosition = ExpandableListView.getPackedPositionGroup(id);
                  Log.i("child",""+ childPosition);
                  Log.i("child Group",""+ groupPosition);
              } else if(itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
                  groupPosition = ExpandableListView.getPackedPositionGroup(id);
                  Log.i("Group",""+ groupPosition);
              } 
        }
    });

推荐答案

无需尝试手动获取职位.使用android定义的方法.

There is no need of trying to get positions manually. Use android defined methods.

这是获取孩子位置和父母位置的方法

This is the method for getting child position and parent position

public class MainActivity extends Activity {

    private ExpandableListView mExpandableListView;
    private List<GroupEntity> mGroupCollection;
     private int childPosition;
        private int groupPosition;
        boolean retVal;

        ExpandableListAdapter adapter;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        prepareResource();
        initPage();


        /*mExpandableListView.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
              public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                  int itemType = ExpandableListView.getPackedPositionType(id);

            if ( itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
                      childPosition = ExpandableListView.getPackedPositionChild(id);
                      groupPosition = ExpandableListView.getPackedPositionGroup(id);
                      Log.i("child",""+ childPosition);
                      Log.i("child Group",""+ groupPosition);
                      //do your per-item callback here
                      return retVal; //true if we consumed the click, false if not
                  } else if(itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
                      groupPosition = ExpandableListView.getPackedPositionGroup(id);
                      //do your per-group callback here
                      Log.i("Group",""+ groupPosition);
                      return retVal; //true if we consumed the click, false if not
                  } else {
                      // null item; we don't consume the click
                      return false;
                  }
            }
          });*/


    }


    private void prepareResource() {

        mGroupCollection = new ArrayList<GroupEntity>();

        for (int i = 1; i < 7; i++) {
            GroupEntity ge = new GroupEntity();
            if (i == 1)
                ge.Name = "Basic Info";
            if (i == 2)
                ge.Name = "Acadamic Background";
            if (i == 3)
                ge.Name = "Experience";
            if (i == 4)
                ge.Name = "Skill";
            if (i == 5)
                ge.Name = "Objective";
            if (i == 6)
                ge.Name = "Reference";
            for (int j = 1; j < 8; j++) {
                GroupItemEntity gi = ge.new GroupItemEntity();
                if (i == 1 && j == 1)
                    gi.Name = "FirstName";
                if (i == 1 && j == 2)
                    gi.Name = "MiddleName";
                if (i == 1 && j == 3)
                    gi.Name = "LastName";
                if (i == 1 && j == 4)
                    gi.Name = "Address";
                if (i == 1 && j == 5)
                    gi.Name = "Sex";
                if (i == 1 && j == 6)
                    gi.Name = "Merital stutas";
                if (i == 1 && j == 7)
                    gi.Name = "Date Of Birthday";
                ge.GroupItemCollection.add(gi);
            }
            mGroupCollection.add(ge);
        }

    }

    private void initPage() {
        mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
         adapter = new ExpandableListAdapter(this,
                mExpandableListView, mGroupCollection);
        mExpandableListView.setAdapter(adapter);
        registerForContextMenu(mExpandableListView);
    }

    public boolean onChildClick( ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
        System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition);

        return true;
    }

}

这篇关于如何在Android中获取可扩展列表视图的子值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆