Jack Commonw
Jack Commonw

Reputation: 395

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?

Upvotes: 5

Views: 2127

Answers (1)

Simon Dorociak
Simon Dorociak

Reputation: 33495

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.

Upvotes: 5

Related Questions