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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:47:14+00:00 2026-05-21T06:47:14+00:00

Why is my OnItemClickListener not working? public class UseAdp extends Activity { /** Called

  • 0

Why is my OnItemClickListener not working?

public class UseAdp extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Context context=this;

    ListView lv=(ListView)  findViewById(R.id.list_v);

    MyAdapter adp=new MyAdapter(context);

    for (int i = 0; i < 50; i++) {
      adp.addItem("item " + i);
    }
    lv.setAdapter(adp);
    // lv.setItemsCanFocus(true);

    lv.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
          long arg3) {
        // TODO Auto-generated method stub
        showToast(arg2);
      }
    });
    //registerForContextMenu(lv);      
  }

  public void showToast(int arg)
  {
    Toast.makeText(this, "Position"+arg, Toast.LENGTH_SHORT).show();
  }
}

Here is my adapter code:

public class MyAdapter extends BaseAdapter {

  private Context mcontext;
  private ArrayList<String> aList=new ArrayList<String>();  

  public MyAdapter(Context context) { 
     mcontext = context;          
  } 

  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    return aList.size();   
  }

  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    return aList.get(position);
  }

  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
  }

  public void addItem(final String item) {
    aList.add(item);
    //notifyDataSetChanged();
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

     // TODO Auto-generated method stub
     LayoutInflater inflater = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     convertView=inflater.inflate(R.layout.list_rows,parent,false);


     ((TextView)convertView.findViewById(R.id.txt1)).setText(getItem(position).toString());
     ((ImageView)convertView.findViewById(R.id.img1)).setImageResource(R.drawable.icon);
     if(position==2){
     ((CheckBox)convertView.findViewById(R.id.CheckBox01)).setChecked(true);
     }
     return convertView;
  }
}
  • 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-21T06:47:15+00:00Added an answer on May 21, 2026 at 6:47 am

    I think you did not use viewholder() method it is working fine when i checked it .
    See the complete code :

    public class FirstScreen extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            ListView lv=(ListView) findViewById(R.id.ListView01);
    
    
            MyAdapter adp=new MyAdapter(getApplicationContext());
    
            for (int i = 0; i < 50; i++) {
                adp.addItem("item " + i);
            }
            lv.setAdapter(adp);
           // lv.setItemsCanFocus(true);
    
           lv.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    // TODO Auto-generated method stub
                    showToast(arg2);
                }
            });
            //registerForContextMenu(lv);
    
        }
    
        public void showToast(int arg)
        {
            Toast.makeText(this, "Position"+arg, Toast.LENGTH_SHORT).show();
        }
    
         class MyAdapter extends BaseAdapter {
    
            private Context mcontext;
            private ArrayList<String> aList=new ArrayList<String>();    
    
             public MyAdapter(Context context) { 
                 mcontext = context;          
            } 
    
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return aList.size();
    
            }
    
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return aList.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }
             public void addItem(final String item) {
                 aList.add(item);
                 //notifyDataSetChanged();
             }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
    
                 // TODO Auto-generated method stub
                 LayoutInflater inflater = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
                 convertView=inflater.inflate(R.layout.list_row,parent,false);
    
    
                 ((TextView)convertView.findViewById(R.id.txt1)).setText(getItem(position).toString());
                 ((ImageView)convertView.findViewById(R.id.img1)).setImageResource(R.drawable.icon);
    
                 return convertView;
            }
    
            }
    
         static class ViewHolder {
             TextView   tab1Text;
             ImageView  backgrouundView;
        }
    
    }
    

    main.xml and list_row is not included here.

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

Sidebar

Related Questions

Spinner is not working. Here is my code: public class second extends Activity{ Spinner
my class extends Activity, not ListActivity. i have this code on create method but
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View item, int position, long id)
I got some invalid Token parsing problem Code : public class viewparty extends Activity
I have this in my first activity: private AdapterView.OnItemClickListener _itemClickLis = new OnItemClickListener() {
A book (Android Recipes by Smith and Friesen) shows this: public class ContactsListListActivity extends
I've created a custom gallery; however, the Set wallpaper button will not set the
I have created a custom list view and created my own adapter class I
I have an Activity (not a ListActivity, although I should probably convert my code
Hello i am making grid menu for activity , all things are working well

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.