Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in listview.setOnItemClickListener and row.setOnClickListener

I am creating a custom array adapter, I now want to implement a function which handles clicking the view. I am having two options in mind, but I am wondering if there is a difference in performance/working speed or something?

Option 1, in the arrayAdapter itself:

row.setOnClickListener(new OnClickListener() {

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


            }
        });

Option 2, from the main Activity:

listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

            }


        });

Or are they exactly the same?

like image 315
Jack Commonw Avatar asked Apr 10 '13 20:04

Jack Commonw


1 Answers

Or are they exactly the same?

In first approach you will create for each row own listener e.q. you have 100 rows so you'll have 100 listeners that is not good at all. In second approach you will create one listener for whole ListView.

android.widget.AdapterView.OnItemClickListener
android.view.View.OnClickListener

How you can see, first is more comfortable and directly designated for dealing with adapter widgets like ListView is. Also this approach is generally recommended and used.

You have connection with each row via parameters of onItemClick() method and code is more human-readable and it's clearer.

like image 108
Simon Dorociak Avatar answered Sep 28 '22 04:09

Simon Dorociak