如何在android应用中保留不同对象实例的数量?

问题描述 投票:0回答:1

我正在制作一个包含MainActivity的应用程序,其中的Recycler视图包含一个鸟类列表。当您单击该鸟类时,它会转到包含该鸟类信息的详细信息活动。 (效果很好)

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

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


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);
    }
}

``

java android class
1个回答
0
投票

[您要将原始的捆绑鸟数据传递给details活动,然后将其绑定到UI。这是拥有这种类型的只读组件的最简单方法。

如果要使这些数据“实时”,可操作且有状态,并保留Android最佳体系结构做法,则应解决以下一种方法:

  • [使用数据库支持的ORM(RoomRealm文件支持/内存中)以便一致地查询和编写您的数据在应用程序的不同部分(最好合并与ViewModelArchitecture ComponentsPagingNavigation
  • 使用共享的首选项/本地存储方法基本上可以相同的东西,但是带有定制的序列化/反序列化管道(学习曲线较简单,但要低得多灵活性/可扩展性/性能/功能集)
  • [使用具有共享ViewModel层的单页应用程序方法,负责数据CRUD,并使用以前的选项之一或完全在内存中的存储库。
© www.soinside.com 2019 - 2024. All rights reserved.