Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get Spinner OnListItemSelected to work
    primarykey
    data
    text
    <p>I am currently trying to implement a Spinner within my android application. I am having trouble with getting the OnItemSelected method, to open a new class based on what item was selected. </p> <p>I have the code shown bellow, which does not seem to work, Also since adding this is, it now from the menu when I click the button to open Film and TV it opens the wrong layout, but nothing changed other than adding the bellow code.</p> <p>What should happen: Activity Starts --> Click on Film and TV --> Select item from Spinner --> New class opens based on what Item was selected.</p> <p>What Happens now: Activity Starts --> Click on Film and TV --> Wrong layout opens --> Press back on phone --> Right layout opens --> Select item from Spinner --> Nothing Happens</p> <p>Code: </p> <pre><code>String classes[] = {"SeanConnery", "BillyConnoly", "JamesMcAvoy", "KarenGillan", "KellyMacdonald", "AshleyJensen"}; @Override public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int pos, long id) { String classSpot = classes[pos]; try{ Class nextClass = Class.forName("com.example.famouspeople." + classSpot); Intent ourIntent = new Intent(Film.this, nextClass); startActivity(ourIntent); } catch(ClassNotFoundException e){ e.printStackTrace(); } } </code></pre> <p>Manifest:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.famouspeople" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="com.example.famouspeople.MainMenu" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name="com.example.famouspeople.Film" android:label="@string/app_name" &gt; &lt;/activity&gt; &lt;activity android:name="com.example.famouspeople.SeanConnery" android:label="@string/app_name" &gt; &lt;/activity&gt; &lt;activity android:name="com.example.famouspeople.BillyConnoly" android:label="@string/app_name" &gt; &lt;/activity&gt; &lt;activity android:name="com.example.famouspeople.JamesMcAvoy" android:label="@string/app_name" &gt; &lt;/activity&gt; &lt;activity android:name="com.example.famouspeople.KarenGillan" android:label="@string/app_name" &gt; &lt;/activity&gt; &lt;activity android:name="com.example.famouspeople.AshleyJensen" android:label="@string/app_name" &gt; &lt;/activity&gt; &lt;activity android:name="com.example.famouspeople.KellyMacdonald" android:label="@string/app_name" &gt; &lt;/activity&gt; &lt;/application&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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