Question

Hi, I am trying to create a simple android app using spinners. It need not be...

Hi, I am trying to create a simple android app using spinners. It need not be very complex. The app should allow the user to use a simple spinner to select a team from the following list of teams (Kansas City Chiefs, New England Patriots, New Orleans Saints, and the Los Angeles Rams), once the user selects the team they wish to view, the app should display a small paragraph of information (hardcoded) To the screen for the user to read, as well as the record for the team (may also be hardcoded into the app)

The only real hurdle is that the app must have 2 activities. The first activity must pass the name of the team that was selected to the second activity and the second activity must pass the hardcoded information back to the first activity to display to the screen.

Again, this app need not be complex. The simpler the better, as long as it accomplishes all the tasks I mentioned.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// MainActivity

package info.pvs.test;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    TextView show_team_data;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        show_team_data = (TextView) findViewById(R.id.textView2);
        spinner.setOnItemSelectedListener(this);
        List<String> teams = new ArrayList<String>();
        teams.add("");
        teams.add("Kansas City Chiefs");
        teams.add("New England Patriots");
        teams.add("New Orleans Saints");
        teams.add("Los Angeles Rams");
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, teams);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);
        getIntent();

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String text_to_show = "";
        String item = parent.getItemAtPosition(position).toString();
        if (position == 0) {
            Toast.makeText(this, "Please select Team", Toast.LENGTH_SHORT).show();
        } else {
            Intent intent = new Intent(getBaseContext(), SecondActivity.class);
            intent.putExtra("item", item);
            startActivity(intent);
        }
    }

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

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1)
        {
            String message=data.getStringExtra("team_data");
            show_team_data.setText(message);
        }
    }
}

// SecondActivity

package info.pvs.test;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String item = getIntent().getStringExtra("item");
        System.out.println("=============================================="+item);
        String text_to_show = "";
        if (item.equals("Kansas City Chiefs")) {
            text_to_show = "The Kansas City Chiefs are a professional American football team based in Kansas City, Missouri. The Chiefs compete in the National Football League as a member club of the league's American Football Conference West division.";
        } else if (item.equals("New England Patriots")) {
            text_to_show = "The New England Patriots are a professional American football team based in the Greater Boston region. The Patriots compete in the National Football League as a member club of the league's American Football Conference East division.";
        } else if (item.equals("New Orleans Saints")) {
            text_to_show = "The New Orleans Saints are a professional American football team based in New Orleans, Louisiana. The Saints currently compete in the National Football League as a member of the league's National Football Conference South division.";
        } else if (item.equals("Los Angeles Rams")) {
            text_to_show = "The Los Angeles Rams are a professional American football team based in Los Angeles, California, which competes in the National Football League's NFC West division. The franchise has won three NFL championships, and is the only one to win championships representing three different cities.";
        }
        System.out.println("=============================================="+text_to_show);
        Intent intent = new Intent();
        intent.putExtra("team_data", text_to_show);
        setResult(RESULT_OK, intent);
        finish();

    }
}
 

//activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="216dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="62dp"
        android:layout_marginTop="67dp"
        />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="69dp"
        android:layout_marginTop="35dp"
        android:text="Select a team" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/textView"
        android:layout_marginTop="231dp"
        android:text="" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="55dp"
        android:layout_marginTop="192dp"
        android:text="Team Info" />
</RelativeLayout>
Add a comment
Know the answer?
Add Answer to:
Hi, I am trying to create a simple android app using spinners. It need not be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this assignment, your job is to create a simple game called Opoly. The objectives of...

    For this assignment, your job is to create a simple game called Opoly. The objectives of this assignment are to: Break down a problem into smaller, easier problems. Write Java methods that call upon other methods to accomplish tasks. Use a seed value to generate random a sequence of random numbers. Learn the coding practice of writing methods that perform a specific task. Opoly works this way: The board is a circular track of variable length (the user determines the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT