如何在Android中的ListView中设置OnClickListener for按钮?

时间:2014-08-05 12:16:19

标签: android listview

我想将button设置为ListView,而button显示在ListView上。 我尝试为ClickListener设置button,就像下面的代码一样。

@Override
        public View getView(int position, View view, ViewGroup parent) {
            // TODO Auto-generated method stub

            ViewHolder viewHolder;
            Log.d(TAG, "getView");
            if(view == null){
                view = mInflator.inflate(R.layout.streamid_data, null);
                viewHolder = new ViewHolder();

                viewHolder.id_text = (TextView) view.findViewById(R.id.ID_text);
                viewHolder.get = (Button) view.findViewById(R.id.get);

                //The code is error , it only can set viewHolder.get.setOnClickListener(new ItemButton());
                viewHolder.get.setOnClickListener(new ItemButton(position));

            }else {
                viewHolder = (ViewHolder) view.getTag();
            }

            return view;
        }

    }

    static class ViewHolder {
        TextView id_text;
        Button get;
    }

    class ItemButton implements OnClickListener {
        private int position;
        private DemoActivity demoActivity;

        private void ItemButton(int pos) {
            // TODO Auto-generated method stub
            this.demoActivity = context;
            position = pos;
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.d(TAG, "ItemButton = " + list.get(position).get("id"));
        }

}

但是viewHolder.get.setOnClickListener(new ItemButton());的代码是错误的,它只能找到函数ItemButton()而且它不能使函数ItemButton(int pos)变细。

所以我无法将代码设置为viewHolder.get.setOnClickListener(new ItemButton(position));

我错过了什么吗?

3 个答案:

答案 0 :(得分:0)

更改为

viewHolder.get.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             Log.d(TAG, "ItemButton = " + list.get(position).get("id"));
        }
    });

答案 1 :(得分:0)

做这样的事

@Override
    public View getView(**final** int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub

        ViewHolder viewHolder;
        Log.d(TAG, "getView");
        if(view == null){
            view = mInflator.inflate(R.layout.streamid_data, null);
            viewHolder = new ViewHolder();

            viewHolder.id_text = (TextView) view.findViewById(R.id.ID_text);
            viewHolder.get = (Button) view.findViewById(R.id.get);

            //The code is error , it only can set
          // viewHolder.get.setOnClickListener(new ItemButton());


        }else {
            viewHolder = (ViewHolder) view.getTag();
        }

   viewHolder.get.setOnClickListener(new View.OnOnClickListener(){

   @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
   //access **final** position here
        Log.d(TAG, "ItemButton = " + list.get(position).get("id"));
    }
   }));

        return view;
    }

}

答案 2 :(得分:0)

Button btnSearchCustomer = (Button) view.findViewById(R.id.btnSearchCustomer);
       btnSearchCustomer.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub



                }
            });