Android 从其他活动添加时在listview中保存项目

Android 从其他活动添加时在listview中保存项目,android,listview,Android,Listview,我的ShoppingListActivity中有一个项目列表视图 项目是从另一个活动添加的,该活动被认为是一个意图。我想确保在两项活动之间进行时,所有项目都保留在列表中;但是,现在我的列表中只有上一个活动中添加的最后一项 MyShoppingListActivity.class public class ShoppingListActivity extends Activity { private ListView mainListView ; private ArrayAda

我的ShoppingListActivity中有一个项目列表视图

项目是从另一个活动添加的,该活动被认为是一个意图。我想确保在两项活动之间进行时,所有项目都保留在列表中;但是,现在我的列表中只有上一个活动中添加的最后一项

My
ShoppingListActivity.class

public class ShoppingListActivity extends Activity {

    private ListView mainListView ;
    private ArrayAdapter<String> listAdapter ;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_list);

        // Find the ListView resource.
        mainListView = (ListView) findViewById( R.id.mainListView );
        ArrayList<String> shoppingList = new ArrayList<String>();
        shoppingList.add(itemLookup());

        // Create ArrayAdapter using the shopping list.
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, shoppingList);

        // Set the ArrayAdapter as the ListView's adapter.
        mainListView.setAdapter( listAdapter );
    }

    //Lookup item by ID
    public String itemLookup() {
        String itemName = "";
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (intent != null) {
            String itemId = extras.getString("BARCODE_ID");
            try {
                itemName = ItemLookup.lookupById(itemId);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return itemName;
    }

    @Override
    public void onBackPressed() {
        startActivity(new Intent(ShoppingListActivity.this, MainActivity.class));
    }
}
公共类ShoppingListActivity扩展活动{
私有ListView主ListView;
专用阵列适配器列表适配器;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u shopping\u list);
//查找ListView资源。
mainListView=(ListView)findViewById(R.id.mainListView);
ArrayList shoppingList=新建ArrayList();
shoppingList.add(itemLookup());
//使用购物列表创建ArrayAdapter。
listAdapter=new ArrayAdapter(this,R.layout.simplerow,shoppingList);
//将ArrayAdapter设置为ListView的适配器。
mainListView.setAdapter(listAdapter);
}
//按ID查找项目
公共字符串itemLookup(){
字符串itemName=“”;
Intent=getIntent();
Bundle extras=intent.getExtras();
if(intent!=null){
String itemId=extras.getString(“条形码标识”);
试一试{
itemName=ItemLookup.lookupById(itemId);
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回itemName;
}
@凌驾
public void onBackPressed(){
startActivity(新意图(ShoppingListActivity.this、MainActivity.class));
}
}
我觉得我应该把我的
add
放在别的地方。我很确定我应该在一个
putExtra
中来回传递列表,但如果我必须这样做,那也没关系


如何确保在活动之间维护列表?

将数据结构保留在活动中会使应用程序容易丢失数据,因为活动可能会在不同时间因各种原因被破坏,包括在纵向和横向之间旋转设备

您应该使用单独的类来存储和跟踪购物列表中的项目。具有ListView的活动应仅获取存储的项目列表并显示它们。任何导致添加项的操作都应该简单地触发重新加载列表(如果活动在前台运行),否则活动在下次启动时都应该看到新项


如果您还需要在流程终止后保持数据的持久性,则应查看可能的可用数据。

将数据结构保留在活动中会使您的应用程序容易丢失数据,因为活动可能会在不同时间因各种原因被销毁,包括在纵向和横向之间旋转设备

您应该使用单独的类来存储和跟踪购物列表中的项目。具有ListView的活动应仅获取存储的项目列表并显示它们。任何导致添加项的操作都应该简单地触发重新加载列表(如果活动在前台运行),否则活动在下次启动时都应该看到新项


如果您还需要在进程终止后保持数据的持久性,您应该研究可能的可用数据。

解决问题的一种方法是

在您的情况下,您可以实现如下内容:

public class ShoppingListManager {

    private static ShoppingListManager instance = new ShoppingListManager();

    private List<String> shoppingList;

    public static ShoppingListManager getInstance() {
        return instance;
    }

    public List<String> getShoppingList() {
        return shoppingList;
    }

    // Make the constructor private so that this class cannot be instantiated
    private ShoppingListManager(){
        shoppingList = new ArrayList<String>();
    }
}

要记住的一点是永远不要在单例类中存储上下文,因为它会导致内存泄漏。

解决问题的一个方法是

在您的情况下,您可以实现如下内容:

public class ShoppingListManager {

    private static ShoppingListManager instance = new ShoppingListManager();

    private List<String> shoppingList;

    public static ShoppingListManager getInstance() {
        return instance;
    }

    public List<String> getShoppingList() {
        return shoppingList;
    }

    // Make the constructor private so that this class cannot be instantiated
    private ShoppingListManager(){
        shoppingList = new ArrayList<String>();
    }
}
要记住的一点是永远不要在单例类中存储上下文,因为它会导致内存泄漏