android - 制作一个全局列表变量以从任何活动访问
问题描述
我正在开发一个购物车应用程序,需要有产品列表和每个产品库存。为此,我要做的是实现一个全局列表变量。我已经阅读了有关全局变量的信息,并且可以实现它,但无法弄清楚列表的形式。任何关于如何实施它的建议或帮助都会很棒,谢谢。
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();
}
}
然后对于我的应用程序的 porpouse,我在它们各自的索引上添加了各自的值
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));
}
谢谢!
推荐阅读
- sql - 基于另一个表从一个表中删除一系列行作为 SQL 中的输入
- c++ - 继承抽象类Stream的SLCD类中printf的实现
- r - 使用连续预测变量的多个值计算 emmeans
- python - Python将网站表数据读入Dataframe
- python - 从 Firebird 数据库中的表中获取数据
- javascript - 如何独立使用 Bootstrap dropdown.js?
- twitter-bootstrap - 折叠的引导砌体面板在第一次展开时会四处移动
- html - 如何根据浏览器大小更改 tabindex 和 aria-hidden 的值?
- c++ - 了解 TCMalloc 的“释放到操作系统的字节数(又名未映射)”统计信息
- jquery - 将 Vimeo 缩略图 URL 放入每个相同的类 div - jquery