很抱歉,如果这是一个非常明显的问题。简而言之,我将数组列表从TshirtParadiseActivity
传递到ShopActivity
。在ShopActivity
中,我将数组列表传递给Adapter
类,该类创建复选框的侦听器,以便它们在选中时修改数组列表。
我可以在ShopActivity
的onDestroy()方法中看到对原始arraylist所做的更改。但是,一旦该活动消失,即使我先前已在TshirtParadiseActivity
中添加了该活动,但我又返回了一个空白列表。
如何在活动之间保留对阵列列表的更改?我在下面包含了我的代码:
TshirtParadiseActivity.java
ShopActivity
ShopActivity.java
package com.bignerdranch.android.tshirtparadise;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class TshirtParadiseActivity extends AppCompatActivity {
private Button mSexy0;
private Button mCheckOut;
private int POOR=0;
private ArrayList<Tshirt> shoppingCart = new ArrayList<Tshirt>();
private static final int requestCodeShop=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tshirt_paradise);
mSexy0 = (Button) findViewById(R.id.sexy0);
mCheckOut = (Button) findViewById(R.id.checkout);
mCheckOut.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//ARRAYLIST IS EMPTY
for (int i=0;i<shoppingCart.size();i++){
System.out.println("you bought a " + shoppingCart.get(i).getTitle() +
" for $" + shoppingCart.get(i).getPrice());
}
}
});
mSexy0.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent i = ShopActivity.newIntent(TshirtParadiseActivity.this,0,shoppingCart);
startActivity(i);
}
});
}
}
TshirtAdapter.java
public class ShopActivity extends AppCompatActivity {
private RecyclerView shirtRecyclerView;
// private ShirtAdapter shirtAdapter;
private int mQuality;
TshirtAdapter teeAdapter;
private List<Tshirt> teeShirtList;
private static ArrayList<Tshirt>shoppingCart;
private static final String QUALITY = "com.bignerdranch.android.tshirtparadise.muchquality";
private static String CART = "com.bignerdranch.android.tshirtparadise.cart";
private static final String BUY = "com.bignerdranch.android.tshirtparadise.buy";
public static Intent newIntent(Context context, int quality,ArrayList<Tshirt>shoppingCart){
//creates an instance of ShopActivity class
Intent i = new Intent(context,ShopActivity.class);
i.putExtra(QUALITY,quality);
i.putExtra(CART,(Serializable)shoppingCart);
return i;
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//Activities are presented to the user
setContentView(R.layout.activity_shop);
TShirtCabinet cabinet = new TShirtCabinet();
Bundle bundle = getIntent().getExtras();
mQuality = bundle.getInt(QUALITY);
//to transform serializable to arraylist, use getSerializable(String name)
shoppingCart = (ArrayList<Tshirt>) bundle.getSerializable(CART);
teeShirtList = cabinet.getList(mQuality);
shirtRecyclerView = (RecyclerView)findViewById(R.id.tshirt_list);
shirtRecyclerView.setLayoutManager(new LinearLayoutManager(ShopActivity.this));
teeAdapter = new TshirtAdapter(teeShirtList,shoppingCart);
shirtRecyclerView.setAdapter(teeAdapter);
}
}
}
答案 0 :(得分:2)
将对象放入Intent
并将其发送到另一个Activity
时,大多数情况下,您都会在对象中获得该对象的不同实例。新的Activity
。列表中可能包含所有相同的值,但是就程序而言,它是一个不同的列表。
如果要查看在第二项活动中所做的更改反映在第一个活动中,则应使用活动“结果”。在第一个活动中,通过调用startActivityForResult()
而不是startActivity()
来启动第二个活动。在第二个活动中,在致电setResult()
之前先致电finish()
。然后,回到第一个活动,通过实现onActivityResult()
处理该结果,并在那里更新列表。