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

烂漫一生 提交于 2019-12-24 10:36:40

问题


I want to click on ListView and perform some operation in the onItemClickListener listener, also on each row I have a button to perform some other operations. How can I do this?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/5726709/how-to-handle-button-click-and-listview-click-on-a-single-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!