Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting a spinner onClickListener() in Android
    text
    copied!<p>I'm trying to get an onClickListener to fire on a Spinner, but I get the following error:</p> <blockquote> <p>Java.lang.RuntimeException is "Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead,"</p> </blockquote> <p>I'm sure I want to call onClickListener and NOT onItemClickListener. I found a question asked by someone else on Stack Overflow, <em><a href="https://stackoverflow.com/questions/3523078">Is there a way to use setOnClickListener with an Android Spinner?</a></em></p> <p>The answer stated there is:</p> <blockquote> <p>You will have to set the Click listener on the underlying view (normally a TextView with id: android.R.id.text1) of the spinner. To do so:</p> <p>Create a custom Spinner In the constructor (with attributes) create the spinner by supplying the layout android.R.layout.simple_spinner_item Do a findViewById(android.R.id.text1) to get the TextView Now set the onClickListener to the TextView</p> </blockquote> <p>I have tried the answer noted there, but it doesn't seem to work. I get a null pointer to the TextView after I do the findViewById().</p> <p>This is what I'm doing:</p> <pre><code>Spinner spinner = (Spinner) findViewById(R.id.spinner); ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,R.layout.layoutspinner,dataArray); spinner.setAdapter(adapter); TextView SpinnerText = (TextView)findViewById(R.id.spinnerText); if (SpinnerText == null) { System.out.println("Not found"); } else { SpinnerText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { //Do something } }); } </code></pre> <h3>File layoutspinner.xml</h3> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/spinnerText" android:singleLine ="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="6pt" android:gravity="right"/&gt; </code></pre> <p>What am I doing wrong?</p> <p>I'm new to Stack&nbsp;Overflow, I didn't find any way to post an aditional question to the other thread (or comment since I have to little rep) so I started a new question.</p> <p>Per recomendation I tried this:</p> <pre><code>int a = spinnerMes.getCount(); int b = spinnerMes.getChildCount(); System.out.println("Count = " + a); System.out.println("ChildCount = " + b); for (int i = 0; i &lt; b; i++) { View v = spinnerMes.getChildAt(i); if (v == null) { System.out.println("View not found"); } else { v.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Click code } }); } } </code></pre> <p>But <a href="https://sites.google.com/site/androidhowto/how-to-1/save-logcat-to-a-text-file" rel="noreferrer">LogCat</a> isn't showing promising results.</p> <pre><code>10-14 16:09:08.127: INFO/System.out(3116): Count = 7 10-14 16:09:08.127: INFO/System.out(3116): ChildCount = 0 </code></pre> <p>I have tested this on API levels 7 and 8 with the same results.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload