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

  • Home
  • SEARCH
  • 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 8950973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:33:38+00:00 2026-06-15T13:33:38+00:00

Is it possible to parameterize an ArrayList from a method parameter? For instance, I

  • 0

Is it possible to parameterize an ArrayList from a method parameter? For instance, I have a method getAllViewsFromRoot(View root) which retrieves all Views from a layout. Now I want a method that gets all ProgressBars in a layout, so I can just use this (toolkit)method:

public static ArrayList<ProgressBar> getAllProgressBars(View root) {
  ArrayList<View> allviews = getAllViewsFromRoot(root);
  ArrayList<ProgressBar> results = new ArrayList<ProgressBar>();
  for (View view : allviews)
    if (view instanceof ProgressBar)
      results.add((ProgressBar)view);
  return results;
}

But since I need to do this for different kinds of Views, I wondered if this is possible in a more generic way, passing on the class in the method. I hoped this would work, but it doesn’t:

public static ArrayList<?> getViewsFromViewGroup(ViewGroup root, Class clazz) {
    ArrayList<View> views = getAllViewsFromRoots(root);
    ArrayList<View> result = new ArrayList<clazz>();
    for (View view : views) {
        if (clazz.isInstance(view.getClass())) {
            result.add(view);
        }
    }
    return result;
}

And somewhere else I would call ArrayList<ProgressBar> pbs = Toolkit.getViewsFromViewGroup(root, ProgressBar.class).

In other words, can I specify how I want to strong type the ArrayList with a method parameter?

Is any sort of what I want possible? I don’t necessarily need it badly, but I like to solve problems in a generic way.

EDIT

I’ve managed to get the code working with the answer below (Thanks to thkala). It’s actually way easier that I suspected.

public static <T> ArrayList<T> getViewsFromViewGroup(View root, Class<T> clazz) {
    ArrayList<T> result = new ArrayList<T>();
    for (View view : getAllViewsFromRoots(root)) 
        if (clazz.isInstance(view)) 
            result.add(clazz.cast(view));
    return result;
}

So if you want to retrieve all ProgressBars from a certain point in your layout, just call:

ArrayList<ProgressBar> myPbs = Toolkit.getViewFromViewGroup(myRootView, ProgressBar.class);
  • 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-06-15T13:33:40+00:00Added an answer on June 15, 2026 at 1:33 pm

    How about this:

    public static <T> ArrayList<T> getViewsFromViewGroup(ViewGroup root, Class<T> clazz) {
        ...
    }
    

    Class is a parametric type, so you can use its parameter to specify a generic parameter in a method. Naturally, you will have to use the T parameter in the method body – unless you plan on using reflection you could even completely ignore the clazz argument.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to generically parameterize a method accepting EITHER ClassA OR InterfaceB ?
Is it possible to have multiple plugins which are doing different jobs and triggered
Is it possible to parametrize an Aspect? Right now I have an @Integration Aspect
Possible Duplicate: How do you generate dynamic (parameterized) unit tests in Python? I have
In all the languages that I understand this is not possible but someone was
Is it possible to wire up the View and ViewModel using both a Declarative
For example, in Java, if I have a parameterized class, is it possible to
I have parametrized junit test that reads from several XML input files. In the
I'm wondering if it's possible to parameterize the sort column in sqlite. I'd like
Is it possible in Java to create a static factory method/class that uses an

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.