Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So i am very new at this android and Java in generel. However, i do know c++, php among others, but this s*** is just not working for me, so i hope you can help me.

I guess it is a pretty simple and easy answer, however i cannot seem to get it to work. I have tried using spinners in the way the official "android" help says, but then i do not seem to be able to save the data and transfer it to another class.

So my code is this:
Java
package com.example.handballstats;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class NewGameScreen extends Activity {

    ArrayAdapter<String> HomePlayer1Adapter;
    ArrayAdapter<String> AwayPlayer1Adapter;

int [] valueHomePlayer1Array = {0,25};
int [] valueAwayPlayer1Array = {0,25};

Spinner HomePlayer1Spinner;
Spinner AwayPlayer1Spinner;


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

        HomePlayer1Spinner = (Spinner) findViewById(R.id.spinner1);
        AwayPlayer1Spinner= (Spinner) findViewById(R.id.spinner2);

        loadHomePlayer1Range();
        loadAwayPlayer1Range();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.new_game_screen, menu);
        return true;
    }

    public void loadHomePlayer1Range() {
        HomePlayer1Spinner.setAdapter(HomePlayer1Adapter);
        HomePlayer1Spinner.setSelection(HomePlayer1Adapter.getPosition("400"));

    }

    public void loadAwayPlayer1Range() {
        AwayPlayer1Spinner.setAdapter(AwayPlayer1Adapter);
        AwayPlayer1Spinner.setSelection(AwayPlayer1Adapter.getPosition("77"));
    }

    public void game_start (View view){
        Intent game_start_intent = new Intent(NewGameScreen.this, GameStartScreen.class);
        game_start_intent.putExtra("Data1", valueHomePlayer1Array);
        game_start_intent.putExtra("Data2", valueAwayPlayer1Array);
        NewGameScreen.this.startActivity(game_start_intent);
    }

}



I want the two spinners to have values from 0-25. Then i want to be able to save these values and import them in a new class using something like:
Java
Private String data1;
Private String data2;
Private int data3;

Bundle extras = getIntent().getExtras();
if (extras != null) {
    data1 = extras.getString("Data1");
    data2 = extras.getString("Data2");
    data3 = extras.getInt("Data3");
}
// other code
Intent myIntent = new Intent(Activity2.this, Activity3.class);
myIntent.putExtra("Data1", data1);
myIntent.putExtra("Data2", data2);
myIntent.putExtra("Data3", data3);
Activity2.this.startActivity(myIntent);


Can someone help me, with what i am doing wrong in the first activity?
Posted

1 solution

I think the problem lies in the way you were trying to pass array object between activities as intent extras, unfortunately that would not work. You have to implement serializable or make use of parcelable class, find out more here: android-parcel-data-to-pass-between-activities-using-parcelable-classes[^]
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900