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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:40:58+00:00 2026-06-07T13:40:58+00:00

I’m using a ListView with custom list items (including a button). I have a

  • 0

I’m using a ListView with custom list items (including a button). I have a custom adapter, in which I set the button’s OnClickListener to an inner class (defined in the adapter class).

I want to access the button in the class (defined in a different xml file from the ListAcitivty class’s) in which the listview is displayed. I want to set it’s onClickListener method inside this class. What’s the best way to go about doing this?

Edit:
This is my list_item.xml (I use for my ListView rows).

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >


<ImageView
    android:id="@+id/imageView"
    android:layout_width="22dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="4dp"
    android:contentDescription="Color"
    android:src="@drawable/ic_launcher" >
</ImageView>

<EditText
    android:id="@+id/nameEdit"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:textSize="20dp" 
    android:inputType="textPersonName"
    >
</EditText>


<Button
    android:id="@+id/deleleButton"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@drawable/delete_button" >

</Button>

</LinearLayout>

How do I access the button (id:deleteButton) inside my class that extends ListActivity? The class that extends listactivity has a separate layout (main.xml let’s say). If I do setContentView(main) inside the class that extends listactivity, findViewById(R.id.deleteButton) would return null.

Edit 2:
This is my class that extends ListActivity. If I place findViewById(R.id.deleteButton) after the setContentView it returns null.

public class PlayerSelection extends ListActivity {
    ListView list;
    ArrayList<PlayerField> textviews = null;
    PlayerFieldsAdapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player_selection_page);
        list = getListView();

        textviews = new ArrayList<PlayerField>();
        for (int i = 0; i< GlobalVariables.getPlayers(); i++) {
            textviews.add(i, new PlayerField());
        }
        adapter = new PlayerFieldsAdapter(this, R.layout.list_item, textviews); 

    ViewGroup header = (ViewGroup) getLayoutInflater().inflate(R.layout.list_header, null);
    Button backButton = null;
    for (int i = 0; i < header.getChildCount(); i++) {
        View v = header.getChildAt(i);
        if (v instanceof Button) {
            backButton = (Button) v;
            break;
        }
    }
    backButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent optionsIntent = new Intent(PlayerSelection.this, OptionsScreen.class);
                startActivity(optionsIntent);
            }
        });
    list.addHeaderView(header);
        list.setAdapter(adapter);
    }
  • 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-07T13:41:01+00:00Added an answer on June 7, 2026 at 1:41 pm

    Easy way to achieve this is to create a field inside your CustomAdapter class:

    private OnClickListener listener;
    

    Then specify a setter method for this field. Next step will be to specify this listener inside your Activity class like that:

    CustomAdapter adapter = (CustomAdapter) getListView().getAdapter();
    adapter.setListener(<your implementation of the listener>);
    

    Then inside your CustomAdapter‘s getView() method you set this listener to your button:

    Button btn = (Button) convertView.findViewById(R.id.btn);
    btn.setOnClickListener(listener);
    

    Hope this helps.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.