LiDaming_and
2012-12-12 03:38
采纳率: 88.9%
浏览 8.3k

给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 2012-12-12 04: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;
    }

    已采纳该答案
    打赏 评论
  • Curie-87 2012-12-13 09: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);

    打赏 评论
  • 琴弦欲奏 2012-12-17 06:53

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

    convertView = getLayoutInflater().inflate(R.layout.XXX, null);
    convertView.findViewById(R.id.XXX);
    
    打赏 评论

相关推荐 更多相似问题