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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:39:02+00:00 2026-06-10T22:39:02+00:00

I have a listView, it has about 100 items. Currently I have this code:

  • 0

I have a listView, it has about 100 items.

Currently I have this code:

public class VanillaList extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vanilla_block_list);

        ArrayList<ItemDetails> image_details = GetSearchResults();

        final ListView lv1 = (ListView) findViewById(R.id.listV_main);
        lv1.setAdapter(new ItemListBaseAdapter(this, image_details));

        lv1.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
                Object o = lv1.getItemAtPosition(position);
                ItemDetails obj_itemDetails = (ItemDetails)o;
                Toast.makeText(VanillaList.this, "You have clicked on : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
            }  
        });
    }

This shows a toast notification saying the name of the list item with “You have clicked on:” in front of it.

This is the case for all 100 items. How would I make a different onClick method for each item. I have tried:

public class VanillaBlockList extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vanilla_block_list);

        ArrayList<ItemDetails> image_details = GetSearchResults();

        final ListView lv1 = (ListView) findViewById(R.id.listV_main);
        lv1.setAdapter(new ItemListBaseAdapter(this, image_details));

        lv1.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
                Object o = lv1.getItemAtPosition(position);
                ItemDetails obj_itemDetails = (ItemDetails)o;
                Toast.makeText(VanillaBlockList.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
            }  
lv2.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
                Object o = lv2.getItemAtPosition(position);
                ItemDetails obj_itemDetails = (ItemDetails)o;
                Toast.makeText(VanillaBlockList.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
            } 
        });
    }

But this hasn’t worked for me. Can anyone help me?
I would prefer an onClick method, like in the layout XML file there is android:onClick="something"

Then in the java file:

public void something{
//method here
}

Thanks.

  • 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-10T22:39:03+00:00Added an answer on June 10, 2026 at 10:39 pm

    I hope in the Custom Adapter class, you are using an item layout to show each item in the list. In that Item list, I think you are having a TextView to show the item description. For for getting on click for each item, need to modify the adapter code as below :

    Create an interface in custom adapter :

    public interface OnItemClickListener{
        void onClick(View v,String description);
    }
    

    Create a field in Custom Adapter :

    private OnItemClickListener listener;
    

    From the adapter Constructor, initialize the Listener :

    public ItemListBaseAdapter(....,OnItemClickListener listener){
        ...your code.
        this.listener=listener;
    }
    

    Change/Add the code as below in getView(…) of Adapter

    TextView tv=convertView.findViewById(yourID);
    tv.setOnClickListener(new TextView.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.onClick(v, ((TextView)v).getText().toString());
                }
            }
        });
    

    Then in your activity class,

    create a new field :

    private OnItemClickListener myItemClickListener=new OnItemClickListener() {
    
        @Override
        public void onClick(View v, String description) {
            // Do what you like when individual item gets clicked.
    
        }
    };
    

    create the adapter instance as :

    new ItemListBaseAdapter(this, image_details, myItemClickListener);
    

    Now when ever you click on the textview in listitem, your myItemClickListener ‘s onClick will getcalled and you can handle as you need.

    if you need the click event for the whole listitem rather than for TextView, then set the onClick of convertView instead of TextView in getView(..) method of Adapter

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

Sidebar

Related Questions

I have a ListView that has 3 GridViewColumns and about 10 items. The third
I have a listview with several items that are created dynamically, each has two
I have a listview in my main activity which shows the detail info about
I have ListView that has the following EditItemTemplate: <EditItemTemplate> <tr style=> <td> <asp:LinkButton ID=UpdateButton
I have a ListView that has a custom ArrayAdapter with a custom XML row.
I have a listview which has its datasource changed after update of a search
I have a Custom ListView which has an ImageView and a TextView. and i
I have ListView in my project which has ListItems as in the form of
I have a ListView and each row has an ImageView. These Images are downloaded
I have a listview and each item has a title, some info, and a

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.