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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:06:11+00:00 2026-05-22T02:06:11+00:00

I am new to android and right now I am learning about ListView .

  • 0

I am new to android and right now I am learning about ListView.

I was reading tutorials from bogotobogo.com when I saw this code:

ListView lv = getListView();
            lv.setTextFilterEnabled(true);
            *** lv.setOnItemClickListener(new OnItemClickListener() {  
                public void onItemClick(AdapterView parent, View v,
                        int position, long id) {
                      // When clicked, show a toast with the TextView text
                    Toast.makeText(getApplicationContext(), ((TextView) v).getText(),
                    Toast.LENGTH_SHORT).show();
                }
            }); ***

i was not able to understand code from lv.setOnItemClickListener(new OnItemClickListener(); is it an argument?

Can some one help me to understand it?

  • 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-22T02:06:12+00:00Added an answer on May 22, 2026 at 2:06 am

    What that code is doing is implementing a new OnItemClickListener inline. The OnItemClickListener interface is basically a contract that says that an object will provide an implementation of the function onItemClick(….). Later on, when an item in your list gets clicked, the onItemClick function will be called and the AdapterView (thing that’s instantiating and managing the list rows, the view – (the render code for a particular row), the position (the position in the list) and an id property which I never use so you can look up what that’s for are being passed in.

    Inline code like this always looks strange to me. There are a couple other ways to write this that I think make more intuitive sense. Just keep in mind that what you’re doing is writing some code to get executed when a row in your list gets clicked.

    1 – You can have your Activity implement OnItemClickListener

    public class SomeActivity extends Activity implements OnItemClickListener{
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.yourview);
    
            ListView lv = (ListView)findViewById(R.id.listView);
            lv.setOnItemClickListener(this);
        } 
    
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
            //Your toast code goes in here
        }
    }
    

    In this code you’re having your main class implement the OnItemClickListener interface so setOnItemClickListener sees the main class (this) as an instance of OnItemClickListener. When a row in your list gets clicked the onItemClick function will get called.

    You can also 2 – have your click listener come from an internal class.

    public class SomeActivity extends Activity implements OnItemClickListener{
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.yourview);
    
            ListView lv = (ListView)findViewById(R.id.listView);
            lv.setOnItemClickListener(new YourInternalClass());
        } 
    
    
        class YourInternalClass implements View.OnItemClickListener{
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
                //Your toast code goes in here
            }
        }
    }
    

    And really all three methods are doing the same thing: Providing the setOnItemClickListener with an instance of a View.OnItemClickListener class that will have it’s onItemClick function called when a row in the list gets clicked.

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

Sidebar

Related Questions

I'm brand new to Android development and right now I am building a simple
I am new to android. I using emulator for my development right now ,
Right now I have to create a new physical file in eclipse android for
I'm new here, and I just learned how to code an Android application. Right
I have a very basic question about populating listviews in android. Right now, if
I'm learning Java and OpenGL ES for Android by reading tutorials and applying what
I'm new to android developing but right now I'm working on an application that
The new Android 2.1 SDK (version 7) has a new class called SignalStrength: http://developer.android.com/reference/android/telephony/SignalStrength.html
I'm trying to record audio this.recorder = new android.media.MediaRecorder(); this.recorder.setAudioSource(android.media.MediaRecorder.AudioSource.MIC); this.recorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.DEFAULT); this.recorder.setAudioEncoder(android.media.MediaRecorder.AudioEncoder.DEFAULT); this.recorder.setOutputFile(pruebaAudioRecorder.mp4); **this.recorder.prepare();**
I know that eclipse can do this, can Intellij via the new Android support

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.