首页 > 解决方案 > 如何使我的带有 queue.poll() 值的变量可供其他活动访问

问题描述

我有一个 Java 类项目,我正在使用 android studio 进行编码。

该项目是石头剪刀布。

基本上,我制作了一个包含敌人名称的队列。

q.poll()给了他们一个变量,名称为firstenemy,secondenemythirdenemy

在第一个游戏中,我想将我的文本视图设置为第一个敌人。然后,在用户获胜后,它将把用户带到下一个布局相似但敌人名称不同的活动。

在这里,我想使用secondenemy第一个游戏活动中的变量设置 textview 名称。

我怎样才能做到这一点 ?或者,有没有办法将其转换q.poll()为字符串?

很抱歉问(也许)这样一个菜鸟问题。

这是我在第一个游戏活动中的代码:

@Override
   protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gameplay);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

    Queue<String> q = new LinkedList<>();




    Intent intent = getIntent();
    String text = intent.getStringExtra(Page2Activity.EXTRA_TEXT);


    nama = findViewById(R.id.name);
    nama.setText(text);
    q.add("Jackson");
    q.add("Diamante");
    q.add("Borsalino");
    String firstenemy = q.poll();
    String secondenemy = q.poll();
    String thirdenemy = q.poll();
    TextView textView4 = findViewById(R.id.textView4);
    textView4.setText(firstenemy);
    winners = findViewById(R.id.winners);
    userresult = findViewById(R.id.userresult);
    enemyresult = findViewById(R.id.enemyresult);
    userresult.setText(text);
    enemyresult.setText(firstenemy);
    userresult2 = findViewById(R.id.userresult2);
    enemyresult2 = findViewById(R.id.enemyresult2);

    rand = new Random();
}

标签: javaandroidandroid-studioqueue

解决方案


推荐阅读