Question

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
        ViewGroup parent) {
    List<String> child = getChild(groupPosition, childPosition);
    String childText = child.get(childPosition);
    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.candidate_list_item, null);
        Button addTag = (Button) convertView.findViewById(R.id.addTag);

        //TODO add button to remove used tags 
        addTag.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                //TODO handle adding tags and add removing tags
                StringBuffer tagInfo = new StringBuffer();
                //TODO magic with tagMap :D
                tagInfo.append("Name: ").append(tagMap.get(connector.get(getGroup(groupPosition))).getName());
                tagInfo.append(" GP: ").append(groupPosition);
                tagInfo.append(" CP: ").append(childPosition);
                tagInfo.append(" CONGET: ").append(connector.get(getGroup(groupPosition)));
                Log.i(this.getClass().getCanonicalName(), tagInfo.toString());
            }
        });

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

    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            StringBuffer tagInfo = new StringBuffer();
            //TODO magic with tagMap :D
            tagInfo.append("_Name: ").append(tagMap.get(connector.get(getGroup(groupPosition))).getName());
            tagInfo.append(" _GP: ").append(groupPosition);
            tagInfo.append(" _CP: ").append(childPosition);
            tagInfo.append(" _CONGET: ").append(connector.get(getGroup(groupPosition)));
            Log.i(this.getClass().getCanonicalName(), tagInfo.toString());
        }
    });
    return convertView;
}

This getChildView is from my adapter (BaseExpandableListAdapter). How can I make my button's OnClick to get the same data which is displayed in convertView's OnClick. When I click on the addTag button I get always the same data, but when I click on child's row, and get the corresponding data - exactly what I need.

My XML for candidate_list_item looks like:

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="55dip"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/lblListItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:paddingBottom="5dp"
            android:layout_alignParentLeft="true"
            android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
            android:layout_toLeftOf="@+id/addTag"
            android:paddingTop="5dp"
            android:textSize="17dip" />

        <Button
            android:id="@+id/addTag"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_alignParentRight="true"
            android:text="Dodaj tag" />
    </RelativeLayout>

I can't use view's onClick, because I will add one more button to every row, so it is very important for me, to use those buttons.

I even tried this:

Button addTag = (Button) convertView.findViewById(R.id.addTag);

//TODO add button to remove used tags 
addTag.setOnClickListener(new OnClickListener() {
    private int cp = -1;
    private int gp = -1;
    public OnClickListener init(int groupPos, int childPos){
        this.cp = childPos;
        this.gp = groupPos;
        return (this);
    }

    @Override
    public void onClick(View arg0) {
        //TODO handle adding tags and add removing tags
        StringBuffer tagInfo = new StringBuffer();
        //TODO magic with tagMap :D
        tagInfo.append("Name: ").append(tagMap.get(connector.get(getGroup(gp))).getName());
        tagInfo.append(" GP: ").append(gp);
        tagInfo.append(" CP: ").append(cp);
        tagInfo.append(" CONGET: ").append(connector.get(getGroup(gp)));
        Log.i(this.getClass().getCanonicalName(), tagInfo.toString());
    }
}.init(groupPosition, childPosition));

But with the same result like without the init(...) method.

Était-ce utile?

La solution

OK, I did it. I had to get the clickListener outside the IF(convertView == null). Now it works right.

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView,
        ViewGroup parent) {
    List<String> child = getChild(groupPosition, childPosition);
    String childText = child.get(childPosition);
    Button addTag = null;
    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.candidate_list_item, null);
    }
    if (addTag == null) {
        addTag = (Button) convertView.findViewById(R.id.addTag);
    }
    TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
    txtListChild.setText(childText);
    //TODO add button to remove used tags 
    addTag.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            //TODO handle adding tags and add removing tags
            StringBuffer tagInfo = new StringBuffer();
            //TODO magic with tagMap :D
            tagInfo.append("Name: ").append(tagMap.get(connector.get(getGroup(groupPosition))).getName());
            tagInfo.append(" GP: ").append(groupPosition);
            tagInfo.append(" CP: ").append(childPosition);
            tagInfo.append(" CONGET: ").append(connector.get(getGroup(groupPosition)));
            Log.i(this.getClass().getCanonicalName(), tagInfo.toString());
        }
    });

    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            StringBuffer tagInfo = new StringBuffer();
            //TODO magic with tagMap :D
            tagInfo.append("_Name: ").append(tagMap.get(connector.get(getGroup(groupPosition))).getName());
            tagInfo.append(" _GP: ").append(groupPosition);
            tagInfo.append(" _CP: ").append(childPosition);
            tagInfo.append(" _CONGET: ").append(connector.get(getGroup(groupPosition)));
            Log.i(this.getClass().getCanonicalName(), tagInfo.toString());
        }
    });
    return convertView;
}

I leave post for others so incompetent as I :)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top