Skip to content Skip to sidebar Skip to footer

How To Handle Button Click And Listview Click On A Single Row?

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?

Solution 1:

You can try this:

publicclassbuttonWithListextendsListActivity {
    /** Called when the activity is first created. */String[] items={"azhar","j"};
    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setListAdapter(newbsAdapter(this));


    }
    publicclassbsAdapterextendsBaseAdapter
    {
        Activity cntx;
        publicbsAdapter(Activity context)
        {
            // TODO Auto-generated constructor stubthis.cntx=context;

        }

        @Overridepublic int getCount()
        {
            // TODO Auto-generated method stubreturn items.length;
        }

        @OverridepublicObjectgetItem(int position)
        {
            // TODO Auto-generated method stubreturn items[position];
        }

        @Overridepublic long getItemId(int position)
        {
            // TODO Auto-generated method stubreturn items.length;
        }

        @OverridepublicViewgetView(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(newOnClickListener(){

                    @OverridepublicvoidonClick(View v) {
                        // TODO Auto-generated method stubonListItemClick(this, this, position, null);
                    }
                    });


            return row;
        }

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

Solution 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.

Post a Comment for "How To Handle Button Click And Listview Click On A Single Row?"