Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我有问题,我想保存我的数组“;卡片&x201D;和下一个活动的var指数_Android_Kotlin - Fatal编程技术网

Android 我有问题,我想保存我的数组“;卡片&x201D;和下一个活动的var指数

Android 我有问题,我想保存我的数组“;卡片&x201D;和下一个活动的var指数,android,kotlin,Android,Kotlin,我的问题是,我已经创建了图像阵列,总是随机让屏幕显示其中的两幅图像,在按下next_按钮和next show 2图像后,但当我打开屏幕时,所有图像都会重置。我知道该活动将更改,必须保存在“onsaveinstancestate”中,但我不知道是否有人可以向我发送解决方案。抱歉,我的代码看起来很糟糕,因为我从kotlin和OOP开始 import android.os.Bundle import android.view.View import android.widget.ImageView i

我的问题是,我已经创建了图像阵列,总是随机让屏幕显示其中的两幅图像,在按下next_按钮和next show 2图像后,但当我打开屏幕时,所有图像都会重置。我知道该活动将更改,必须保存在“onsaveinstancestate”中,但我不知道是否有人可以向我发送解决方案。抱歉,我的代码看起来很糟糕,因为我从kotlin和OOP开始

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_roll.*
import kotlinx.android.synthetic.main.activity_roll0.*
import java.lang.NullPointerException
import kotlin.random.Random

class RollActivity0 : AppCompatActivity() {

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

        //init array
        var cards = arrayOf(R.drawable.bart_cassidy,
                                        R.drawable.black_jack,
                                        R.drawable.calamity_janet,
                                        R.drawable.el_gringo,
                                        R.drawable.jesse_jones,
                                        R.drawable.jourdonnais,
                                        R.drawable.kit_carlson,
                                        R.drawable.lucky_duke,
                                        R.drawable.paul_regret,
                                        R.drawable.pedro_ramirez,
                                        R.drawable.rose_doolan,
                                        R.drawable.sid_ketchum,
                                        R.drawable.slab_the_killer,
                                        R.drawable.suzy_lafayette,
                                        R.drawable.vulture_sam,
                                        R.drawable.willy_the_kid)

        val random_index = java.util.Random()
        var index = 0

        //shuffling array
        for (i in cards.size - 1 downTo 1)
        {
            val j = random_index.nextInt(i + 1)
            val tmp = cards[i]
            cards[i] = cards[j]
            cards[j] = tmp
        }

        imageView3.setImageResource(cards[index])
        index++ //next image
        imageView4.setImageResource(cards[index])
        index++ //next image

        next_button1.setOnClickListener {
                if ( index >= cards.size )//if i am on end array make new shuffled array
                {
                    //shuffling array
                    for (i in cards.size - 1 downTo 1)
                    {
                        val j = random_index.nextInt(i + 1)
                        val tmp = cards[i]
                        cards[i] = cards[j]
                        cards[j] = tmp
                    }

                    index = 0 //new start
                }

                imageView3.setImageResource(cards[index])
                index++

                imageView4.setImageResource(cards[index])
                index++
        }
    }
}

只需在中的清单文件中添加android:configChanges=“orientation | keyboardHidden” 你的活动

<activity android:name=".RollActivity0"
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name">

如果我没弄错的话,你不想在右转屏幕时更改这两幅图像吗?您可以在onSaveInstanceState中查看当前显示的图像,并在onCreate中读取存储的信息

设置图片时请确保索引安全 存储当前状态 恢复活动再次创建时的状态
我希望我把你的问题答对了,能帮你一点忙集合。洗牌(YouRayLeLIST);
洗牌一个arrayList,而不是在这里执行的操作。以下是一些文档的链接:。至于主要问题,当活动旋转时,您可能(尚未测试)使用savedInstanceState逻辑和ImageView中当前显示的图像的两个索引通过arrayList。这里有一个我认为对你可能有用的帖子:谢谢你的建议。谢谢你的建议。
...your code...
var indexPictureOne = -1
var indexPictureTwo = -1
imageView3.setImageResource(cards[index])
indexPictureOne = index
index++ //next image
imageView4.setImageResource(cards[index])
indexPictureTwo = index
index++ //next image
...your code...
override fun onSaveInstanceState(bundle: Bundle) {
   super.onSaveInstanceState(bundle);
   icicle.putInt("pictureOneIndexKey", indexPictureOne)
   icicle.putInt("pictureTwoIndexKey", indexPictureTwo)
}
override fun onCreate(bundle: Bundle?){
   val restoredIndexPictureOne = bundle?.getInt("pictureOneIndexKey",null)
   val restoredIndexPictureTwo = bundle?.getInt("pictureTwoIndexKey",null)

   ... //if restoredIndexPictureOne and restoredIndexPictureTwo are not null 
   skip your random logic and use these indexes, else use your random logic to 
   get two new random pictures

   ...your code..
}