简体   繁体   English

用上一个活动中的项目填充ListView

[英]Fill ListView With Items From Previous Activity

I'm new to Android programming, and still teaching myself to code. 我是Android编程的新手,仍然在自学代码。

Currently I'm teaching myself about GridViews and still coding that project with tutorials so I have nothing to show right now, but the basic idea is the following... 目前,我正在自学有关GridViews的知识,并且仍在使用教程对该项目进行编码,因此我现在什么都没显示,但是基本思想如下:

If I have images of groceries in GridView in the first activity and when you click an image you will be able to open a new activity with a larger image and you could input the number how many you things you need, like 5 apples or whatever. 如果我在第一个活动中的GridView中有杂货的图像,并且单击图像时,您将可以打开一个具有较大图像的新活动,并且可以输入所需数量的数字,例如5个苹果或其他。

All of that is more or less clear to me how to do. 所有这些对我来说或多或少都是清楚的。

But how would I send the number and image to a new (third) activity with a ListView that would list all the items you need to buy at the grocery store? 但是,我该如何使用ListView将编号和图像发送到新的(第三个)活动,该View列出您需要在杂货店购买的所有物品? How would I be able to fill the list only with items after you enter the number on the previous activity with the large picture and click an "OK" or "Add" button or whatever and not list absolutely everything? 在上一个活动中输入带有大图的编号并单击“确定”或“添加”按钮或其他任何内容之后,又又不能列出所有内容,我如何才能仅用项目填充列表?

Thanks! 谢谢!

It's difficult at first, but you can use an SQLiteDatabase to store the data. 首先很难,但是您可以使用SQLiteDatabase来存储数据。 It's not a quick solution for you, but definitely worth learning about if you're serious to learn android. 这不是您的快速解决方案,但是如果您是认真学习android的人,绝对值得学习。 Here's a link to the official stuff: https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html 这是官方资料的链接: https : //developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

I personally used this tutorial: http://www.androidwarriors.com/2016/02/android-sqlite-database-tutorial-sqlite.html?m=1 我个人使用了本教程: http : //www.androidwarriors.com/2016/02/android-sqlite-database-tutorial-sqlite.html? m =1

Sharing some data between multiple activities or fragments is a very common situation. 在多个活动或片段之间共享一些数据是很常见的情况。 One way around it is implementing a Singleton Pattern . 解决它的一种方法是实现Singleton Pattern

In your case you can design some kind of data structure for your purpose and manage it inside shared singleton class. 您可以根据自己的目的设计某种数据结构,并在共享单例类中对其进行管理。 For example something like this: 例如这样的事情:

public class ShoppingListManager {

    private static ShoppingListManager instance = new ShoppingListManager();

    private List<ShoppingItem> shoppingList;

    public static ShoppingListManager getInstance() {
        return instance;
    }

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

    public void addShoppingItem(ShoppingItem item) {
        this.shoppingList.add(item);
    }

    ...

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

Then you can access your data anywhere in your code and manage shared data in any way you'd like. 然后,您可以在代码中的任何位置访问数据,并以任意方式管理共享数据。

ShoppingListManager.getInstance().getShoppingList();

// You can add items in any activity
ShoppingListManager.getInstance().addShoppingItem(item);

One point to remember never store context in singleton classes as it will lead to memory leaks. 切记不要将上下文存储在单例类中,因为这会导致内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM