我正在开发一个购物车应用程序,需要产品列表和每个产品库存。 为此,我试图做的是实现一个全局列表变量。我已经阅读了全局变量,我可以实现它,但无法弄清楚列表会如何。关于如何实施它的任何建议或帮助都会很棒,谢谢。
public class Application extends android.app.Application {
private int data;
public int getData(){
return data;
}
public void setData(int d){
this.data=d;
}
}
((Application) this.getApplication()).setData(0);
x1=((Application) this.getApplication()).getData();
我按照你的建议想通了。 首先是应用类
public class Application extends android.app.Application {
public ArrayList myGlobalArray = null;
public Application() {
myGlobalArray = new ArrayList();
}
}
然后对于我的应用程序的海豚,我将各自的值添加到它们的相关索引上
for (int i=0;i<productsList.size();i++){
if (productsList.get(i).idProduct==idProduct){
values.add(i,Integer.parseInt(txtQuantity.getText().toString()));
((Application)getApplicationContext()).myGlobalArray = (ArrayList) values;
}
}
以及当我需要获取值时
ArrayList<Integer> test = new ArrayList<>();
for (int d=0;d< ((Application) getApplicationContext()).myGlobalArray.size();d++) {
test.add((Integer) ((Application)getApplicationContext()).myGlobalArray.get(d));
}
谢谢!