首页 > 解决方案 > 如何在android应用程序中保持不同对象实例的计数?

问题描述

我正在制作一个包含 MainActivity 的应用程序,其中包含一个包含鸟类列表的回收器视图。当您单击鸟时,它会转到包含鸟信息的详细信息活动。(工作正常)

详细信息活动还有一个增加计数的按钮。我遇到的问题是我不知道如何从详细信息活动中访问鸟的实例以跟踪该计数。

这是鸟类;这是我应该保留计数变量的地方?


public class Bird {

    // Variables
    private String scientificName;
    private String commonName;
    private String description;
    private String numberImage;
    private int count;

    // Defaults Constructor
    public Bird() {

    }

    // Constructor with parameters
    public Bird(String scientificName, String commonName, String description, String numberImage) {
        this.scientificName = scientificName;
        this.commonName = commonName;
        this.description = description;
        this.numberImage = numberImage;
        this.count = 0;
    }

    // Getters
    public String getScientificName() {
        return scientificName;
    }

    public String getCommonName() {
        return commonName;
    }

    public String getDescription() {
        return description;
    }

    public String getNumberImage() {
        return numberImage;
    }

    public int getCount() {
        return this.count;
    }

    // Setters
    public void setScientificName(String scientificName) {
        this.scientificName = scientificName;
    }

    public void setCommonName(String commonName) {
        this.commonName = commonName;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setNumberImage(String numberImage) {
        this.numberImage = numberImage;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

这是详细活动

public class BookActivity extends AppCompatActivity {

    private ImageView birdImage;
    private TextView birdCommonName;
    private TextView birdSciName;
    private TextView birdDesc;
    private TextView birdCount;
    private Button countButton;
    int count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book);

        // Views
        birdCommonName = findViewById(R.id.com_name);
        birdSciName = findViewById(R.id.sci_name);
        birdDesc = findViewById(R.id.description);
        birdImage = findViewById(R.id.bird_image);
        birdCount = findViewById(R.id.bird_count);
        countButton = findViewById(R.id.count_button);

        // Receive data
        Intent intent = getIntent();
        String commonName = intent.getExtras().getString("CommonName");
        String sciName = intent.getExtras().getString("SciName");
        String desc = intent.getExtras().getString("Description");
        int img = intent.getExtras().getInt("Image");

        // Set values
        birdCommonName.setText(commonName);
        birdSciName.setText(sciName);
        birdDesc.setText(desc);
        birdImage.setImageResource(img);
    }

    public void plusCount() {
        birdCount.setText(count);
    }
}

``

标签: javaandroidclass

解决方案


您将原始捆绑的鸟类数据传递给详细信息活动,然后简单地将其绑定到 UI。这是拥有此类只读组件的最简单方法。

如果您想让这些数据“实时”、可操作和有状态,并保留 Android 最佳架构实践,您应该解决以下方法之一:

  • 使用由数据库(RoomRealm 文件支持/内存中)支持的 ORM,以便在应用程序的不同部分持续查询和写入数据(最好将其与ViewModelArchitecture ComponentsPagingNavigation结合使用)
  • 使用共享首选项/本地存储方法做基本相同的事情,但使用自定义序列化/反序列化管道(学习曲线更简单,但灵活性/可扩展性/性能/功能集低得多)
  • 使用具有共享 ViewModel 层的单页应用程序方法,负责数据 CRUD 并利用以前的选项之一或完全在内存中的存储库。

并且请不要尝试通过任何类型的引用在您的应用程序组件之间传递对象,尤其是 UI 组件。Android 状态处理很复杂,这种方法肯定会失败。


推荐阅读