似乎无法使Android状态保存工作

时间:2019-02-21 11:31:02

标签: java android kotlin

我目前正在研究一个小项目,以练习我的Android开发技能。现在,我设置了2个活动,“ MainActivity”和“ EnlargeImageActivity”。

编辑:我在科特林工作

主要活动包含一堆文本视图和一个图像视图,以显示有关拳击手的信息。这些拳击手全部硬编码在一个数组中,我使用一个称为“计数器”的整数来检索它们。计数器一开始就设置为0。

private var counter: Int = 0

这里是要抓住的要点,为了在拳击手列表中移动,我添加了2个按钮:“下一个”和“上一个”按钮。单击这些按钮时,计数器将增加/减少1,并更新textviews / imageview。

最后,当单击图像时,我希望看到它在新活动中被放大。我通过从boxers-array的柜台索引处获取图像来完成这项工作。

当我从EnlargedImageActivity移回到MainActivity时,会发生问题,MainActivity完全重置为counter = 0,因此显示了数组的第一个元素,而不是对应于刚放大的图像的元素。

以下是所有代码: MainActivity:

    private lateinit var boxers: Array<Boxer>
    private lateinit var imageView: ImageView
    private lateinit var nameView: TextView
    private lateinit var boxerIdView: TextView
    private lateinit var birthDayView: TextView
    private lateinit var weightClassView: TextView
    private lateinit var winsView: TextView
    private lateinit var lossesView: TextView
    private lateinit var drawsView: TextView
    private var counter: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)

            setContentView(R.layout.activity_main)

            initialiseViews()
            addEventHandlers()
        }

        private fun initialiseViews() {
            imageView = findViewById(R.id.imageView)
            nameView = findViewById(R.id.name)
            boxerIdView = findViewById(R.id.boxerId)
            birthDayView = findViewById(R.id.birthday)
            weightClassView = findViewById(R.id.weightClass)
            winsView = findViewById(R.id.wins)
            lossesView = findViewById(R.id.losses)
            drawsView = findViewById(R.id.draws)

            boxers = getBoxers()
            imageView.setImageDrawable(getDrawable(boxers[counter].image))
            val fullName = boxers[counter].first_name + " " + boxers[counter].last_name
            nameView.setText(fullName)
            boxerIdView.setText(String.format("ID #%04d", boxers[counter].id))
            val birthday = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(boxers[counter].birth_day.time)
            birthDayView.setText(birthday)
            weightClassView.setText(boxers[counter].weightclass)
            winsView.setText(boxers[counter].wins.toString())
            lossesView.setText(boxers[counter].losses.toString())
            drawsView.setText(boxers[counter].draws.toString())
        }

        private fun addEventHandlers() {
            val buttonNext: Button = findViewById(R.id.buttonNext)
            buttonNext.setOnClickListener {
                if (counter >= 14) {
                    counter = 0
                } else {
                    counter += 1
                }
                imageView.setImageDrawable(getDrawable(boxers[counter].image))
                val fullName = boxers[counter].first_name + " " + boxers[counter].last_name
                nameView.text = fullName
                boxerIdView.setText(String.format("ID #%04d", boxers[counter].id))
                val birthday = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(boxers[counter].birth_day.time)
                birthDayView.setText(birthday)
                weightClassView.setText(boxers[counter].weightclass)
                winsView.setText(boxers[counter].wins.toString())
                lossesView.setText(boxers[counter].losses.toString())
                drawsView.setText(boxers[counter].draws.toString())
            }

            val buttonPrev: Button = findViewById(R.id.buttonPrev)
            buttonPrev.setOnClickListener {
                if (counter <= 0) {
                    counter = boxers.lastIndex
                } else {
                    counter -= 1
                }
                imageView.setImageDrawable(getDrawable(boxers[counter].image))
                val fullName = boxers[counter].first_name + " " + boxers[counter].last_name
                nameView.text = fullName
                boxerIdView.setText(String.format("ID #%04d", boxers[counter].id))
                val birthday = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(boxers[counter].birth_day.time)
                birthDayView.setText(birthday)
                weightClassView.setText(boxers[counter].weightclass)
                winsView.setText(boxers[counter].wins.toString())
                lossesView.setText(boxers[counter].losses.toString())
                drawsView.setText(boxers[counter].draws.toString())
            }
        }

        fun enlargeImage(view: View) {
            val imageToEnlarge = boxers[counter].image
            val intent = Intent(this, EnlargeImageActivity::class.java).apply {
                putExtra(EXTRA_ENLIMAGE, imageToEnlarge)
            }
            startActivity(intent)
        }

EnlargedImageActivity:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_enlarge_image)

    val image = intent.getIntExtra(EXTRA_ENLIMAGE, 0)

    val imageView = findViewById<ImageView>(R.id.enlargedImage).apply {
        setImageDrawable(getDrawable(image))
    }
}

我试图通过重写onStop(),onSaveInstance和onRestoreInstance()函数来使其工作,以使用常数保存计数器,但我无法使其正常工作。我一直只是结束数组的第一个元素。

0 个答案:

没有答案