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 5982363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:04:35+00:00 2026-05-22T22:04:35+00:00

I’m using some reflection to set up a ListView made up of custom views.

  • 0

I’m using some reflection to set up a ListView made up of custom views. One of the HashMaps that I’ll be using with this Adapter has a button in it. I’m pretty sure I need to set the onClickListener on the button in the getView method, but I’m not sure how.

Here’s the code for my Adapter:

private class ViewAdapter extends BaseAdapter {

    private HashMap<String, View> mMap = new HashMap<String, View>();
    private String[] mKeys;


    private Context mContext;

    private ViewAdapter(Context context, HashMap<String, View> map) {
        mContext = context;
        mMap = map;
        mKeys = mMap.keySet().toArray(new String[map.size()]);          

    }


    @Override
    public int getCount() {
        return mMap.size();
    }

    @Override
    public Object getItem(int position) {
        return mKeys[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row;
        Object myObject = null;
        Class myViewClass = null;
        Constructor constructor = null;
        Method methodToExecute = null;
        String[] arguments = {mKeys[position]};
        try {

            myViewClass = mMap.get(mKeys[position]).getClass();

            //grab the constructor          
             constructor = mMap.get(mKeys[position]).getClass().getConstructor(Context.class);

            //instantiate a new object.
            myObject = (View) constructor.newInstance(getBaseContext());



            //Grab the display method
            methodToExecute = myViewClass.getMethod("display", new Class[] {String.class});
            Log.i("method", methodToExecute.toString());


            } catch (Exception e) {
                e.printStackTrace();
            }

        if(convertView == null)  {

            row = (View) myObject;

        } else {
            row =  convertView;
        }

        try {
        methodToExecute.invoke(row, new Object[]{arguments});
        } catch (Exception e) {
            e.printStackTrace(); 
    }       
        return row;
    }

}

EDIT – Yeah, this was a ridiculous design decision. I ended up scrapping the whole thing and just going for a regular ArrayList of some ADT I created which had a field of a Class that I could set to be my particular custom View class. You live and you learn 😉

  • 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-22T22:04:36+00:00Added an answer on May 22, 2026 at 10:04 pm

    I’m guessing here but in order to have a button, I would assume that the View is actually a ViewGroup. If so, try casting it to ViewGroup and iterating through the children Views using getChildAt(i):

    ViewGroup vg = (ViewGroup) myObject;
    
    for (int i=0; i< vg.getChildCount(); i++) {
      View v = vg.getChildAt(i); 
      if (v instanceOf Button)
        ((Button)v).setOnClickListener(...);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Does anyone know how can I replace this 2 symbol below from the string
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In order to apply a triggered animation to all ToolTip s in my app,
I want use html5's new tag to play a wav file (currently only supported
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.