gpt4 book ai didi

java - 如何在不返回 size = 0 的情况下获取 ArrayList 的值?

转载 作者:行者123 更新时间:2023-11-30 02:01:13 24 4
gpt4 key购买 nike

当我获取 arraylist 的值时,我得到 size = 0。我在 Class Screen1 中启动 ArrayList,但我无法获取 Class Screen2 cont 的值。

public class Variables {

public Variables() {
}

private int cont = 0;
private int correct = 0;


public int getCont(){
return cont;
}
public void setCont(int cont){
this.cont = cont;
}

public int getCorrect(){
return correct;
}
public void setCorrect(int correct){
this.correct = correct;
}}

类屏幕1

public class Screen1 extends ActionBarActivity {


Variables variables = new Variables();
ArrayList<Variables> variablesArrayList = new ArrayList<Variables>();
public int cont;




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


variables.setCorrect(1);
variables.setCont(1);
variablesArrayList.add(variables);

}


public void sendTela1(View view){
cont = variables.getCont();

cont++;

variablesArrayList.get(0).setCont(cont);

}

}

类屏幕 2

这个类我正在尝试获取 ArrayList 的值

public class Screen2 extends ActionBarActivity {

Variables variables = new Variables();
ArrayList<Variables> variablesArrayList = new ArrayList<Variables>();
int cont;
ImageView progressbar;

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


progressbar = (ImageView) findViewById(R.id.progressbar);

//this line, the arraylist is returning size = 0
cont = variablesArrayList.get(1);


switch (cont){
case 1:
progressbar.setImageResource(R.drawable.progressbar1);
break;

case 2:
progressbar.setImageResource(R.drawable.progressbar2);
break;
case 3:
progressbar.setImageResource(R.drawable.progressbar3);
break;
case 4:
progressbar.setImageResource(R.drawable.progressbar4);
break;
case 5:
progressbar.setImageResource(R.drawable.progressbar5);
break;
}}

最佳答案

您得到的返回大小为 0,因为在 screen2 中您在创建新列表时执行的 Activity :ArrayList<Variables> variablesArrayList = new ArrayList<Variables>();

要解决此问题,您需要传递在 screen1 中创建的数组列表进入screen2 .您将必须实现 parcelable , 但这将允许您使用 intents 传递数据和 bundles .

关于java - 如何在不返回 size = 0 的情况下获取 ArrayList 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31443461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com