简体   繁体   中英

Spinner Item leads to Activity (Android Studio)

I started programming a few month ago and stackoverflow always was aa good dite for solving my problems. So my codes are getting better but now I´m on a point that I Need your help again.

Programm: In my App you can choose items from a spinner, then it goes to next page and so on. You have to choose from several Spinners until you get a result...

Some Code preview (Code works, but i now have to ad some more Action and I don´t know how...

package com.sio.fmf;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Button;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.TextView;

public class Koerperform extends AppCompatActivity {
  String[] koerperform = {" ", "spindel- oder torpedoförmig", "langgestreckt", "hochrückig", "schlangenförmig", "welsartig", "grundelartig"};

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

    Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
    mySpinner.setAdapter(new MyCustomAdapter(Koerperform.this,R.layout.spinner_layout, koerperform));
  }

  public void onClick(View v){}
  public class MyCustomAdapter extends ArrayAdapter<String> {
    public MyCustomAdapter(Context context, int textViewResourceId, String[] objects) {
      super(context, textViewResourceId, objects);
    }

    private Button getSwtitchact;
    {
      final Button switchact = (Button) findViewById(R.id.button3);
      switchact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          Intent act = new Intent(view.getContext(), Maulstellung.class);
          startActivity(act);
        }
      });
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
      return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {
      LayoutInflater inflater = getLayoutInflater();
      View row = inflater.inflate(R.layout.spinner_layout, parent, false);
      TextView label = (TextView) row.findViewById(R.id.koerper);
      label.setText(koerperform[position]);

      if (position == 0) {
        label.setTextColor(0xFFF00000);
      }
      return row;
    }
  }
}

So in this state of the app you can choose the string from the spinner and then if you press Botton 3 it changes to class Maulstellung

My Problem: I want that when string "a" is choosen it goes to page xy after Button 3 is pressed and when string "b" is choosen it goes to page xyz after Button 3 is pressed,..., and so on for each string...

Hope you can help me and sorry for my bad English

maybe this layout file helps

<pre>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".Koerperform"
    android:background="#023738">




    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:id="@+id/imageView1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/header" />

    <ImageView
        android:layout_width="400dp"
        android:layout_height="100dp"
        android:id="@+id/imageView5"
        android:background="@drawable/frageeins"
        android:layout_below="@+id/space4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="120dp"
        android:id="@+id/imageView6"
        android:background="@drawable/fischeins"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/imageView5" />

    <Spinner
        android:id="@+id/spinner"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView6"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:clickable="false"
        android:contextClickable="false"
        />

    <Button
        android:layout_width="220dp"
        android:layout_height="60dp"
        android:id="@+id/button3"
        android:layout_marginTop="69dp"
        android:background="@drawable/costum_button_weiter_gehts"
        android:layout_below="@+id/imageView6"
        android:layout_centerHorizontal="true" />

    <Space
        android:layout_width="30dp"
        android:layout_height="20dp"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:id="@+id/space4" />

</RelativeLayout>

I will do in this way:

Not sure is this what you need.

Declare String selectedValue and Spinner mySpinner in global(add after line String[] koerperform ).

Remove Spinner beside mySpinner:

Spinner mySpinner = (Spinner)findViewById(R.id.spinner);

Inside getSwtitchact , change to this

  private Button getSwtitchact;
    {
      final Button switchact = (Button) findViewById(R.id.button3);
      switchact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          selectedValue=mySpinner.getSelectedItem().toString(); // here you get the selected items from spinner
          if(selectedValue.equals("a"))
          {
          Intent act = new Intent(view.getContext(), xy.class);
          startActivity(act);
          }
          else if(selectedValue.equals("B"))
          {
          Intent act = new Intent(view.getContext(), xyZ.class);
          startActivity(act);
          }
          else
         {
           ......
         }
        }
      });
    }

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