kotlin 为什么我的cardview在android的hashmap中没有显示正确的关联键值?

ef1yzkbh  于 2023-03-19  发布在  Kotlin
关注(0)|答案(1)|浏览(139)

因此,我一直在开发我的应用程序中的功能,我的cardview翻转,并显示它的背面,它显示的值和cardview的正面有一个键与它相关联。我的问题是,键-值对没有被正确显示。我写的键的值没有显示,但其他一些值得到显示。以下是我的代码。
QuestionsFragment.kt

@AndroidEntryPoint
class QuestionsFragment : Fragment() {
    lateinit var binding: FragmentQuestionsBinding
    lateinit var viewModel: BottomNavigationViewModel

    lateinit var bottomNavigationView: BottomNavigationView

    private val hashMap = HashMap<String,String>()

    private var currentPairIndex =0
    private lateinit var currentPair:Map.Entry<String,String>

    var isFront=true

    @SuppressLint("ResourceType")
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        binding = FragmentQuestionsBinding.inflate(inflater, container, false)

        bottomNavigationView = (activity as? FirstScreen)?.findViewById(R.id.bottomNavigationView)!!
        viewModel = ViewModelProvider(requireActivity())[BottomNavigationViewModel::class.java]

        bottomNavigationView.visibility = View.GONE

        // setting up listener for back Icon
        binding.backIcon?.setOnClickListener {
            activity?.onBackPressed()
        }

        binding.floatingActionButton.setOnClickListener {
            findNavController().navigate(R.id.action_questionsFragment_to_flashCards)
        }

        //changing color of progress bar progress
        binding.progressHorizontal.progressTintList = ColorStateList.valueOf(ContextCompat.getColor(requireContext()
            ,R.color.blue_card))

        //changing color of background color of progress bar
        binding.progressHorizontal.progressBackgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(requireContext(),
        R.color.silver))

        // Hashmap of strings that will shown on cardview front and back side
        hashMap["What"] = "Kas"
        hashMap["When"] = "Kai"
        hashMap["Where"] = "Kur"
        hashMap["Who"] = "Kas"
        hashMap["Whom"] = "Kam"
        hashMap["Why"] = "Kodėl"
        hashMap["How"] = "Kaip"
        hashMap["Which"] = "Kuris/kuri"
        hashMap["Whose"] = "Kieno"

       val front_animation = AnimatorInflater.loadAnimator(context, R.anim.front_animator) as AnimatorSet
        val back_animation = AnimatorInflater.loadAnimator(context,R.anim.back_animator)as AnimatorSet

        currentPair = hashMap.entries.elementAt(currentPairIndex)
        binding.textCardFront.text = currentPair.key
        binding.textCardBack.text = hashMap[currentPair.key]

        with(binding) {
            btnFlip.setOnClickListener {
                // initialize currentPairIndex to 0 if it hasn't been initialized yet
                if (currentPairIndex < 0) {
                    currentPairIndex = 0
                }
                if (isFront) {
                    front_animation.setTarget(textCardFront)
                    back_animation.setTarget(textCardBack)
                    front_animation.start()
                    back_animation.start()
                    isFront = false
                    textCardBack.visibility = View.VISIBLE
                    textCardFront.visibility = View.GONE
                    cardViewQuestions.setCardBackgroundColor(ContextCompat.getColor(requireContext(), R.color.pink))

                } else {

                    textCardFront.visibility = View.VISIBLE
                    textCardBack.visibility = View.GONE
                    cardViewQuestions.setCardBackgroundColor(ContextCompat.getColor(requireContext(), R.color.blue_card))
                    front_animation.setTarget(textCardBack)
                    back_animation.setTarget(textCardFront)
                    back_animation.start()
                    front_animation.start()
                    isFront = true
                }
                // retrieve the current pair from the hashMap
                currentPair = hashMap.entries.elementAt(currentPairIndex)
                binding.textCardFront.text = currentPair.key
                binding.textCardBack.text = hashMap[currentPair.key]

                // increment currentPairIndex for the next flip
                currentPairIndex = (currentPairIndex + 1) % hashMap.size
            }
        }

        return binding.root
    }
}

因此,如您所见,我希望显示正确的键值对,如(hashMap[“Where”] =“Kur”),其中“Where”将位于cardview的正面,“Kur”位于背面。

jjhzyzn0

jjhzyzn01#

每次点击btnFlip时,你似乎都在改变这对文本--你设置了前后文本,但每当你按下翻转按钮时,你设置的是next对中的文本,所以你最终看到的是 front 0back 1front 2 等,因为当前索引每次都在增加。
我猜你可能只想在把 * 从后面 * 翻转到前面的时候增加currentPairIndex,所以你会得到 front 0back 0front 1 等等,你已经有了条件代码,它取决于显示的是前面还是后面,你可以把它放在那里:

if (currentPairIndex < 0) {
    currentPairIndex = 0
}

if (isFront) {
    // display data for current card
} else {
    // we're looking at the back, and flip was clicked, so we need the next card
    // This needs to be updated before we use it to get current text etc!
    currentPairIndex = (currentPairIndex + 1) % hashMap.size

    // display data for current card
}

// display data for current card
currentPair = hashMap.entries.elementAt(currentPairIndex)
binding.textCardFront.text = currentPair.key
binding.textCardBack.text = hashMap[currentPair.key]

所以你基本上只会在点击按钮的时候增加索引,只会在显示背面的时候增加索引,而且你会在需要使用索引之前更新索引--所以它需要尽早发生。
将其明确化,与显示/动画/翻转代码分开可能会更清楚:

// handling the current index
if (currentPairIndex < 0) {
    currentPairIndex = 0
}
// advance to the next card if this is flipping from the back
if (!isFront) currentPairIndex = (currentPairIndex + 1) % hashMap.size

// display stuff

相关问题