Android Spinner Selection - 如何保存数据 [英] Android Spinner Selection - how to save data

查看:106
本文介绍了Android Spinner Selection - 如何保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对这个android和Java inrelrel非常新。但是,我确实知道c ++,php等等,但这个s ***对我不起作用,所以我希望你能帮助我。



我猜这是一个非常简单和容易的答案,但我似乎无法让它工作。我尝试使用官方android帮助说的方式使用微调器,但是我似乎无法保存数据并将其传输到另一个类。



所以我的代码是这样的:

  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 活动{

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

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

Spinner HomePlayer1Spinner;
Spinner AwayPlayer1Spinner;


@覆盖
受保护 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(菜单菜单){
// 膨胀菜单;如果项目存在,则会将项目添加到操作栏。
getMenuInflater()。inflate(R.menu.new_game_screen,menu);
返回 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(查看视图){
意图game_start_intent = new 意图(NewGameScreen。,GameStartScreen。);
game_start_intent.putExtra( Data1,valueHomePlayer1Array);
game_start_intent.putExtra( Data2,valueAwayPlayer1Array);
NewGameScreen。 this .startActivity(game_start_intent);
}

}







我想要两个微调器从0到25的值。然后我希望能够保存这些值并使用以下内容将它们导入到新类中:

 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);
}
// 其他代码
意图myIntent = new Intent(Activity2。 this ,Activity3。 class );
myIntent.putExtra( Data1,data1);
myIntent.putExtra( Data2,data2);
myIntent.putExtra( Data3,data3);
Activity2。 this .startActivity(myIntent);





有人可以帮助我,我在第一个活动中做错了吗?

解决方案

我认为问题在于你试图在活动之间传递数组对象的方式作为意图附加物,不幸的是,这是行不通的。你必须实现serializable或使用parcelable类,在这里找到更多: android-parcel-data-to-between-activities-using-parcelable-classes [ ^

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:

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:

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?

解决方案

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[^]


这篇关于Android Spinner Selection - 如何保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆