如何使OnClick从Button作为OnClick从convertView工作

时间:2014-05-14 12:44:41

标签: android onclick expandablelistview

@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;
}

getChildView来自我的适配器(BaseExpandableListAdapter)。如何使我的按钮的OnClick获得与convertView的OnClick中显示的相同的数据。 当我点击addTag按钮时,我总是得到相同的数据,但当我点击子行时,获取相应的数据 - 正是我需要的。

我的candidate_list_item XML看起来像:

 <?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>

我无法使用view的onClick,因为我会在每一行添加一个按钮,因此对我来说使用这些按钮非常重要。

我甚至试过这个:

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));

但结果与没有init(...)方法相同。

1 个答案:

答案 0 :(得分:0)

好的,我做到了。我必须在IF(convertView == null)之外获得clickListener。现在它运作正常。

@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;
}

我留下的帖子让其他人如此无能为力:)。