简体   繁体   中英

Starting activity from spinner and new activity wont start another - Android Development

I've followed some other questions and pieced it together to get this:

    public class FbFPS extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fbhtf);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter
                .createFromResource(this, R.array.spagesarray,
                        android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Spinner s = (Spinner) findViewById(R.id.spages);

        s.setAdapter(adapter);

        s.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
                // Display Selected option
                if (parent.getItemAtPosition(pos).toString()
                        .equals("Under 16s Reccommended Settings")) {
                    Intent i = new Intent(getApplicationContext(),
                            FbU16RS.class);
                    startActivity(i);
                    finish();
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }

        });

        s.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
                // Display Selected option
                if (parent.getItemAtPosition(pos).toString()
                        .equals("Recommended Privacy Settings")) {
                    Intent i = new Intent(getApplicationContext(), FbRS.class);
                    startActivity(i);
                    finish();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }

        });

    }
}

And there are two more activities similar but with them pointing to the remaining two activities. So basically this:

s.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {
            // Display Selected option
            if (parent.getItemAtPosition(pos).toString()
                    .equals("Recommended Privacy Settings")) {
                Intent i = new Intent(getApplicationContext(), FbRS.class);
                startActivity(i);
                finish();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }

    });

twice on every activity. They are mentioned in the manifest and xmls are correct. its really odd though it opens the last option but dosent open the other. And when in the one that starts, it wont open any...

Where am i going wrong? I'm also open to any easier ways. :) Thanks In Advance.

EDIT:

OK i've changed it around on all 3 activities and it now seems to open the same activity on whatever I select. Here's the code:

    public class FbRS extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fbhtf);

        final Intent iFbHTF = new Intent(FbRS.this, FbHTF.class);
        final Intent iFbU16RS = new Intent(FbRS.this, FbU16RS.class);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter
                .createFromResource(this, R.array.spagesarray,
                        android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Spinner s = (Spinner) findViewById(R.id.spages);

        s.setAdapter(adapter);

        s.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
                if (parent.getItemAtPosition(pos).toString()
                        .equals("Finding Privacy Settings")) {
                    startActivity(iFbHTF);
                    finish();
                } else if (parent.getItemAtPosition(pos).toString()
                        .equals("Under 16s Recommended Privacy Settings")) {
                    startActivity(iFbU16RS);
                    finish();
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }

        });

    }
}

Also here is the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.timmo.isp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.timmo.isp.Home"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.timmo.isp.FbHTF"
            android:label="@string/titleFbHTF"
            android:parentActivityName="com.timmo.isp.Home" >
        </activity>
        <activity
            android:name="com.timmo.isp.FbU16RS"
            android:label="@string/titleFbU16RS"
            android:parentActivityName="com.timmo.isp.Home" >
        </activity>
        <activity
            android:name="com.timmo.isp.FbRS"
            android:label="@string/titleFbRS"
            android:parentActivityName="com.timmo.isp.Home" >
        </activity>
        <activity
            android:name="com.timmo.isp.FYMNK"
            android:label="@string/titlefymnk"
            android:parentActivityName="com.timmo.isp.Home" >
        </activity>
    </application>

</manifest>

I think its getting there hopefully thanks for the help AndroidPenguin and kongkea.

Below is the code you want although there might be just one too many or too few brackets. Basically only one onitemselectedlistener will ever be called. So put both in the one.

public class FbFPS extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fbhtf);

ArrayAdapter<CharSequence> adapter = ArrayAdapter
        .createFromResource(this, R.array.spagesarray,
                android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Spinner s = (Spinner) findViewById(R.id.spages);

s.setAdapter(adapter);

s.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
            int pos, long id) {
        // Display Selected option
        if (parent.getItemAtPosition(pos).toString()
                .equals("Under 16s Reccommended Settings")) {
            Intent i = new Intent(getApplicationContext(),
                    FbU16RS.class);
            startActivity(i);
            finish();
        } else if (parent.getItemAtPosition(pos).toString()
                .equals("Recommended Privacy Settings")) {
            Intent i = new Intent(getApplicationContext(), FbRS.class);
            startActivity(i);
            finish();
        }

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

    }

});

}
}

You can also clean your code up using the id/position number and switch and case if you prefer this.

FBFPS Activity

Intent i = new Intent(FBFPS.this, FbU16RS.class);

On the onClickListener above. do it like FBFPS activity. set the current name of your activity Intent i = new Intent(your current activity name.this, FbRS.class);

In Manifest add all your Activity class name in the application tag

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".ToDoListActivity" // Main activity
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity 
        android:name=".your activity name">
    </activity>
    <activity 
        android:name=".your activity name>
    </activity>

</application>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM