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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:15:07+00:00 2026-06-09T20:15:07+00:00

I have created a listview that sits inside a larger activity (the list view

  • 0

I have created a listview that sits inside a larger activity (the list view fills half the screen, with inputs/buttons above it). This listview uses a custom adapter to draw the row views.

Inside the row view is a button, which when clicked/tapped i want the activity to handle, and not the adapter. However i don’t know how inside the adapter class, i can tell it to use the activity. Since it’s OOP, im assuming i have to pass some sort of reference to the activity when setting up the adapter (rather than hardcoding the parent activity into the adapter).

I had a simlier issue with sharing a dataset between the activity and adapter (i wanted the adapter to use an arraylist from the activity), however i couldn’t solve that one either so i ended up passing the arraylist through as a duplicate into the adapter. So i would hope working out the click listener, will also mean i can get rid of that duplicate data having to be created?

All the code is pretty simple, but here’s a rough outline:

Setting the list adapter & declaring dataset (this is the activity dataset, not the adapter)

numbersListAdapter = new NumberListAdapter(this);
numbersList.setAdapter(numbersListAdapter);
this.selectedContacts = new HashMap<Long, HashMap<String, String>>();

Adding entry into activity data set and adding to the adapter dataset

HashMap<String, String> tempa = new HashMap<String,String>();
tempa.put("name", name);
tempa.put("number", number);
this.selectedContacts.put(contactID, tempa);

this.numbersListAdapter.addEntry(contactID, tempa);

The adapters add entry

public void addEntry(Long id, HashMap<String, String> entry) {  

        entry.put("contactID", id.toString());      

        this.selectedNumbers.add(entry);

        this.notifyDataSetChanged();

    }

The adapters constructor

public NumberListAdapter(Context context) {

        selectedNumbers = new ArrayList<HashMap<String, String>>();

        mInflater = LayoutInflater.from(context);

    }

Please note: this is my first attempt at this stuff. I’ve never done android, java and very, very little OO programming. So i already know the code is most likely inefficient and quite terrible. But i’ve got to learn somehow 🙂

EDIT

Ok so i realised ive been a little silly and i just needed to use the context passed into the adapter to reference the parent activity. However i still ain’t getting it. Heres the code:

The constructor for the adapter

numbersListAdapter = new NumberListAdapter(this);

The variable declaration and constructor method

public class NumberListAdapter extends BaseAdapter  {

    private LayoutInflater mInflater;

    private ArrayList<HashMap<String, String>> selectedNumbers;

    private Context parentActivity;



    public NumberListAdapter(Context context) {

        parentActivity = (Context) context; 

        selectedNumbers = new ArrayList<HashMap<String, String>>();

        mInflater = LayoutInflater.from(context);

    }

The listener

Button theBtn = (Button)rowView.findViewById(R.id.actionRowBtn);
            theBtn.setOnClickListener(this.parentActivity);

I get two messages from eclipse, the first occurs when i comment out the listener and i get
The value of the field NumberListAdapter.parentActivity is not used

Once i add the listener in i get

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (Context)

Obviously im doing something wrong. Probably quite a silly mistake again too

  • 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-09T20:15:08+00:00Added an answer on June 9, 2026 at 8:15 pm

    If you need to get callback when row of the listview is clicked you can use

    numbersListAdapter.setOnitemitemClickLiistener

    But if you need a button inside each row to be clicked you will have to override the

    getView function of your adapter and then set the onclicklistener of the button individually

    Edit:
    Typecast the Context to Activity

    theBtn.setOnClickListener((YourActivity)this.parentActivity);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a list view that displays the names and dates of items
We have created custom listview by using the Base adapter. List view contains Text
I have created custom listview in that have list of textview & list of
I have created an Activity that shows a listview and on swipe action another
I have created a android list view in Eclipse that I would like to
I have created a custom ArrayAdapter that fills a ListView . The data in
I have a listview that contain several EditTexts created dynamically according to the number
I have a listview with several items that are created dynamically, each has two
I am trying to display ListView . I have created one activity , in
I have a PlaceHolder control inside of a ListView that I am using to

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.