Frage

i have a expandableList. my items have TextView and button. i want to know the pressed button is from which item! how can i do it? here is my code :

        @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;
}
War es hilfreich?

Lösung

Instead of implementing onClickListener in adapter attach one in getChildView itself where you will have access to childPosition and 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;
}

Andere Tipps

You able to get Child position of the parent/group

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

button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

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

vipul mittal's answer is correct,

but change this line

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

to

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

as it is well discussed here: https://possiblemobile.com/2013/05/layout-inflation-as-intended/

you can even do as below to get from which row your button is clicked

 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();
        }
        }
    });
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top