Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7646953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:20:32+00:00 2026-05-31T10:20:32+00:00

It would be more convenient for me to register a unique OnClickListener for each

  • 0

It would be more convenient for me to register a unique OnClickListener for each row in the ListView, but I want to be sure that this is an acceptable practice. My current design is a rather convoluted way to separate the concerns of the OnClickListener from each row type.

The reason for this is that I have multiple classes of rows in the ListView. Each class has a completely different responsibility and behavior. For example, consider a ListView that can contain both sub-categories and book titles. If a book title is clicked, a new activity should start that shows the cover image. If a sub-category is clicked, a new list of books and categories is displayed.

I’d like the row itself to maintain knowledge about its own identity and responsibility rather than having to leak the knowledge about each row to be maintained by the implementer of onItemClickListener.

I’d also like to know what the performance implications are of doing this vs. implementing my own logic to figure out how to handle the click.

Are there drawbacks to implementing onClickListener for each ListView ArrayAdapter row instead of onItemClickListener? I’m looking for concrete data and specific drawbacks rather than vague recommendations.

Should I expect memory use, initialization time, or steady-state speed (like scrolling through the list) to be significantly affected?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:20:34+00:00Added an answer on May 31, 2026 at 10:20 am

    You don’t explain why you would need separate click listeners for each row, but I would recommend against it. Take a look at using View.setTag(Object)/View.getTag() to pass row-specific custom data by which you can customize the response of a (shared) click listener.

    EDIT

    I see from your example why you want different OnClickListeners attached to your rows. I was under the impression that you wanted a separate OnClickListener instance for each row. (That was my main reason for recommending against this.) If you have two types of rows (categories and titles) and hundreds of rows, you need only two kinds of responses, not hundreds of them. I also understand the point about separating concerns.

    Nevertheless, I think overriding ListActivity.onListItemClick() (or calling ListView.setOnItemClickListener() if you aren’t using a ListActivity) would be cleaner and less likely to interfere with the operation of the list view. You can use a delegate pattern for this, as follows.

    Define an abstract class or interface:

    public interface MyClickHandler {
        public void onItemClick(ListView l, View v, int position, long id);
    }
    

    Then create one instance of an object that implements this interface for each type of row data (not one instance for each row). In your adapter, use setTag(Object) to initialize the tag for each row to the appropriate MyClickHander instance. In your ListActivity.onListItemClick() override, use this logic:

    protected void onListItemClick(ListView l, View v, int position, long id) {
        Object tag = v.getTag();
        if (tag instanceof MyClickHandler) {
            ((MyClickHandler) tag).onItemClick(l, v, position, id);
        } else {
            // default processing (if any)
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user script that would be much more useful if it could
I apologize in advance if this question seems remedial. Which would be considered more
I seem to be missing something obvious here, why would I want more than
I would like display something more meaningful that animated gif while users upload file
This title begs for more explanation. Basically, I'm rolling an API that wraps a
It's too late to change the question, but more precise would have been to
I edited the question so it would make more sense. I have a function
Sometimes when I'm writing XLANG/s code in an expression shape, it would be more
Would there a more elegant way of writing the following syntax? Thread t0 =
Which would be a more appropriate repository to host a small, XCode, Objective-C based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.