1

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?

2 Answers 2

4

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]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

can u give me a demo project wiyh xml coding and some sort of operation onitemclicklistener and on button click
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.