android可扩展列表中的子数据

时间:2013-12-28 07:12:31

标签: android button expandablelistview expandablelistadapter

我有一个可扩展的列表。我的项目有TextView和按钮。我想知道按下的按钮是从哪个项目!我该怎么做? 这是我的代码:

        @Override
public View getChildView(int groupPosition, 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.item_text);
    Button button = (Button) convertView.findViewById(R.id.item_button);
    button.setOnClickListener(this);
    txtListChild.setText(childText);
    return convertView;
}

4 个答案:

答案 0 :(得分:1)

而不是在适配器中实现onClickListener,而是在getChildView本身附加一个,您可以访问childPosition和groupPosition

@Override
public View getChildView(final 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.item_text);
Button button = (Button) convertView.findViewById(R.id.item_button);
button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

    //here inside you can user groupPosition, childPosition
    // TODO Auto-generated method stub
//groupPosition, childPosition
    Toast.makeText(getContext(), "groupPosition: "+groupPosition +"  childPosition: "+  childPosition,Toast.LENGTH_SHORT).show();

        }
    });
    txtListChild.setText(childText);
    return convertView;
}

答案 1 :(得分:0)

您可以获取父/组的子位置

Write this code instant of button.setOnClickListener(this);

button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                String pos=groupPosition+"-"+childPosition;
            }
        });

答案 2 :(得分:0)

vipul mittal的回答是正确的,

但改变此行

convertView = infalInflater.inflate(R.layout.list_item, null);

convertView = infalInflater.inflate(R.layout.list_item, parent, false);

这里讨论得很好: https://possiblemobile.com/2013/05/layout-inflation-as-intended/

答案 3 :(得分:-1)

您甚至可以按照以下方式获取点击按钮的行

 button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

       RelativeLayout rl = (RelativeLayout) v.getParent();// if your childview is any other 
       // layout then take that layout (like linear layout etc)
       TextView tv1 = (TextView)rl.findViewById(R.id.textview_child);//your text view from child 
                                                                       //layout
       String Name = tv1.getText().toString();
    Toast.makeText(context, Name+"  Button is clicked", Toast.LENGTH_SHORT).show();
        }
        }
    });