开发者

How to handle Button click and Listview click on a single row?

开发者 https://www.devze.com 2023-02-28 01:33 出处:网络
I want to click on ListView and perform some operation in the onItemClickListener listener, also on each row I have a button to per开发者_如何学编程form some other operations.

I want to click on ListView and perform some operation in the onItemClickListener listener, also on each row I have a button to per开发者_如何学编程form some other operations. How can I do this?


You can try this:

public class buttonWithList extends ListActivity {
    /** Called when the activity is first created. */
    String[] items={"azhar","j"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setListAdapter(new bsAdapter(this));


    }
    public class bsAdapter extends BaseAdapter
    {
        Activity cntx;
        public bsAdapter(Activity context)
        {
            // TODO Auto-generated constructor stub
            this.cntx=context;

        }

        @Override
        public int getCount()
        {
            // TODO Auto-generated method stub
            return items.length;
        }

        @Override
        public Object getItem(int position)
        {
            // TODO Auto-generated method stub
            return items[position];
        }

        @Override
        public long getItemId(int position)
        {
            // TODO Auto-generated method stub
            return items.length;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            View row=null;
            // TODO Auto-generated method stub
            /*if(convertView==null)
            {

            }*/
                LayoutInflater inflater=cntx.getLayoutInflater();
                row=inflater.inflate(R.layout.row, null);
                TextView tv=(TextView)row.findViewById(R.id.txtRow);
                Button btn=(Button)row.findViewById(R.id.btnRow);
                tv.setText(items[position]);
                btn.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        onListItemClick(this, this, position, null);
                    }
                    });


            return row;
        }

        protected void onListItemClick(OnClickListener onClickListener,
                OnClickListener onClickListener2, int position, Object object) {
            // TODO Auto-generated method stub
            //Toast.makeText(this, items[position], 3000);
            System.out.println(items[position]);
        }
    }
}


You need to implement a ListAdapter. This object let's you define the elements of each row. In addiction you'll use a ViewBinder to specify the actions of your rows. So you will add a button to every row and set its onClickItemListener using the ViewBinder.

0

精彩评论

暂无评论...
验证码 换一张
取 消