【发布时间】:2013-12-29 01:52:48
【问题描述】:
我有一个要发送到另一个活动的项目的 ArrayList。 ArrayList 的目的是像购物车一样保存物品,删除活动显示用户购物车中的物品并允许他们删除物品。除了从删除活动返回时,ArrayList 是空的。
onCreate 代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// instantiating layout features
priceEditText = (EditText) findViewById(R.id.priceEditText);
addItem = (Button) findViewById(R.id.addItemButton);
gps = (CheckBox) findViewById(R.id.locationCheckBox);
tax = (EditText) findViewById(R.id.taxEditText);
subTotal = (EditText) findViewById(R.id.subTotalTextEdit);
total = (Button) findViewById(R.id.totalitemsButton);
totalTextEdit = (EditText) findViewById(R.id.totalTextEdit);
context = this;
sTotal = 0.0;
name = (EditText) findViewById(R.id.itemNameTextField);
delete = (Button) findViewById(R.id.deleteItemButton);
items = new ArrayList<Item>();
// adding listeners
addItem.setOnClickListener(click);
total.setOnClickListener(done);
delete.setOnClickListener(remove);
}
开始新活动的代码:
private OnClickListener remove = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(
com.JPT3Productions.salestaxcalc.MainActivity.this,
Delete.class);
Bundle bundle = new Bundle();
bundle.putSerializable("array", items);
intent.putExtras(bundle);
startActivity(intent);
}
};
最后,接收意图:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete);
// Show the Up button in the action bar.
setupActionBar();
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
ArrayList<Item> items = (ArrayList<Item>) bundle.getSerializable("array");
final TableLayout layout = (TableLayout) findViewById(R.id.tl);
【问题讨论】:
-
我相信你在删除一些元素后不会更新你的数组列表。在检索到另一个类中感兴趣的数组列表后,您在该类中对其进行操作,以便更新内容,您需要调用 set 方法来更新内容。这就是我认为出了问题的地方。
-
您认为我应该在我的 Items 类中创建一个 ArrayList 实例变量并添加操作 ArrayList 的方法吗?
-
我相信是这样 - 这将是一种方法。但是,根据您的描述,这个数组列表应该是一个类变量,我的意思是它必须是“静态的”。
标签: java android arrays list nullpointerexception