2 lidaming and LiDaming_and 于 2012.12.12 11:38 提问

给listview的button设置OnClick Listener事件

在屏幕中有一个list view。每一个list item包含两个text view和一个按钮。
点击按钮时,我想用当前选中行的行序号去Vector里面取得一些数据。
这是List Custom Adapter

private class CustomAdapter extends ArrayAdapter<ServicesItems> {

    public CustomAdapter(Context context, int resource,
            int textViewResourceId, List<ServicesItems> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        TextView item = null;
        TextView description = null;
        Button subNowBtn;

        ServicesItems ii = getItem(position);

        if (null == convertView) {
            convertView = mInflater.inflate(
                    R.layout.list_of_servics_item_2, null);

            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        // cat_name = holder.gettitle();

        Resources resources = getResources();

        ServicesItems bean1 = (ServicesItems) servicesVector
                .elementAt(position);

        String cat_name_str = bean1.getService_name().toString();
        String descreption = bean1.getDescription().toString();


        item = holder.getItem();
        item.setText(bean1.getDescription());

        description = holder.getDescription();
        description.setText(bean1.getService_name());

         subNowBtn=holder.getSubButton();

        return convertView;
    }

    private class ViewHolder {
        private View mRow;
        private TextView description = null;
        private TextView item = null;
        private Button sub = null;

        public ViewHolder(View row) {
            mRow = row;
        }

        public TextView getDescription() {
            if (null == description) {
                description = (TextView) mRow
                        .findViewById(R.id.category_tv);
            }
            return description;
        }

        public TextView getItem() {
            if (null == item) {
                item = (TextView)       
           mRow.findViewById(R.id.descreption_tv);
            }
            return item;
        }

        public Button getSubButton(){
            if(null==sub){
                sub=(Button)findViewById(R.id.subscribe_now_btn);


            }
            return sub;
        }

    }

}

可是这段代码实现不了,请问如何获取数据呢?

3个回答

A_G_E_N
A_G_E_N   2012.12.12 12:50
已采纳

我的是这样,你改改

public View getView(final int position, View convertView, ViewGroup parent) {
    final Holder holder;
    LayoutInflater inflater = LayoutInflater.from(context);
    MsgInfo info = itemlist.get(position);
    if (convertView != null) {
        holder = (Holder) convertView.getTag();
    } else {
        holder = new Holder();
        convertView = inflater.inflate(R.layout.listitem, null);
        holder.deletebtn = (Button) convertView
                .findViewById(R.id.item_deletebtn);
        holder.hadreadbtn = (Button) convertView
                .findViewById(R.id.item_readbtn);


        holder.msgtext = (TextView) convertView.findViewById(R.id.item_text);
        holder.timetext = (TextView) convertView
                .findViewById(R.id.item_timetext);
        String msg = "";
        holder.msgtext .setText(msg);
        holder.timetext.setText(info.getCtime());
        convertView.setTag(holder);
    }
    OnClickListener listener = new OnClickListener() {
        public void onClick(View v) {
            if (v == holder.deletebtn) {
                //你的操作
            }
            if (v == holder.hadreadbtn) {
                //你的操作
            }
            if(v==holder.msgtext){
                //你的操作
            }
        }
    };
    holder.deletebtn.setOnClickListener(listener);
    holder.hadreadbtn.setOnClickListener(listener);
    holder.msgtext.setOnClickListener(listener);
    return convertView;
}

class Holder {
public Button deletebtn;
public Button hadreadbtn;
public TextView msgtext;
public TextView timetext;
}

LiDaming_and
LiDaming_and 你可以把下边的代码也编辑一下哦。
5 年多之前 回复
a1388463837
a1388463837   2012.12.17 14:53

重写一个BaseAdapter类
在这个类的getview方法中写事件就好了,唯一不同的是
初始化控件的时候,记得是要以listview的item的布局文件来初始化
否则不能获取控件
具体如下

convertView = getLayoutInflater().inflate(R.layout.XXX, null);
convertView.findViewById(R.id.XXX);
curie_87
curie_87   2012.12.13 17:42

在 Adapter 中试试这样

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    View row = convertView;
    YourWrapper wrapper = null;

    if (row == null)
    {
        row = inflater.inflate(R.layout.layout, parent, false);
        wrapper = new YourWrapper (row);
        row.setTag(wrapper);
    }
    else
        wrapper = (YourWrapper) row.getTag();

    wrapper.getButton().setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // What you want
        }
    });

    return row;
}

wrapper 类:

public class YourWrapper
{
    private View base;
    private Button button;

    public YourWrapper(View base)
    {
        this.base = base;
    }

    public Button getButton()
    {
        if (button == null)
        {
            button = (Button) base.findViewById(R.id.your_button);
        }
        return (button`);
    }
}

sub=(Button)findViewById(R.id.subscribe_now_btn);改变为sub=(Button) mRow.findViewById(R.id.subscribe_now_btn);

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!