图像未显示在 Image_view 中

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

我正在为我的大学导航创建一个项目。由于我在单层(六楼)工作,所以我在闭环结构中有 13 个位置点。我从用户那里获取两个输入作为当前位置和目的地,然后使用 A* 算法找到最短路径并使用图像来导航用户,如谷歌街景。

但问题是,当我运行应用程序时,我的 Activity_image_display 完全是空白的,我不明白为什么

我的代码

主要活动

package com.example.collegeroamer

import android.content.Intent
import android.os.Bundle
import android.widget.ImageButton
import android.widget.Spinner
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var navigateButton: ImageButton
    private lateinit var currentLocationSpinner: Spinner
    private lateinit var destinationSpinner: Spinner
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        navigateButton = findViewById(R.id.navigateButton)
        currentLocationSpinner = findViewById(R.id.currentLocationSpinner)
        destinationSpinner = findViewById(R.id.destinationSpinner)

        navigateButton.setOnClickListener {
            val startingLocation = currentLocationSpinner.selectedItem.toString()
            val destination = destinationSpinner.selectedItem.toString()

            val intent = Intent(this, NavigationDisplayActivity::class.java)
            intent.putExtra("starting_location", startingLocation)
            intent.putExtra("destination", destination)
            startActivity(intent)
        }
    }
}

导航显示Activity.kt

package com.example.collegeroamer

import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.*
import java.util.*
import kotlin.collections.HashMap
import kotlin.coroutines.CoroutineContext

class NavigationDisplayActivity : AppCompatActivity(), CoroutineScope {
    private lateinit var job: Job
    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + job

    private lateinit var pathImageView: ImageView

    // Map of location points to their corresponding images
    private val locationImages = mapOf(
        "Class 601" to R.drawable.sixthfloor_class601,
        "Class 602" to R.drawable.sixthfloor_class602,
        "Class 603" to R.drawable.sixthfloor_class603,
        "Class 604" to R.drawable.sixthfloor_class604,
        "Class 605" to R.drawable.sixthfloor_class605,
        "Class 606" to R.drawable.sixthfloor_class606,
        "Class 607" to R.drawable.sixthfloor_class607,
        "Class 608" to R.drawable.sixthfloor_class608,
        "Class 609" to R.drawable.sixthfloor_class609,
        "Class 610" to R.drawable.sixthfloor_class610,
        "Class 611" to R.drawable.sixthfloor_class611,
        "Class 612" to R.drawable.sixthfloor_class612,
        "NSS Unit and DLLE Room" to R.drawable.sixthfloor_nssroom
    )

    // Map of location points to their connected points
    private val adjacencyList = mapOf(
        "Class 601" to listOf("Class 602"),
        "Class 602" to listOf("Class 601", "Class 603"),
        "Class 603" to listOf("Class 602", "Class 604"),
        "Class 604" to listOf("Class 603", "Class 605"),
        "Class 605" to listOf("Class 604", "Class 606"),
        "Class 606" to listOf("Class 605", "NSS Unit and DLLE Room"),
        "NSS Unit and DLLE Room" to listOf("Class 606", "Class 607"),
        "Class 607" to listOf("NSS Unit and DLLE Room", "Class 608"),
        "Class 608" to listOf("Class 607", "Class 609"),
        "Class 609" to listOf("Class 608", "Class 610"),
        "Class 610" to listOf("Class 609", "Class 611"),
        "Class 611" to listOf("Class 610", "Class 612"),
        "Class 612" to listOf("Class 611", "Class 601")
    )

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

        job = Job()

        pathImageView = findViewById(R.id.pathImageView)

        // Example usage
        val startingLocation = intent.getStringExtra("starting_location") ?: ""
        val destination = intent.getStringExtra("destination") ?: ""

        launch {
            val shortestPath = findShortestPath(startingLocation, destination)
            displayPathImages(shortestPath)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        job.cancel()
    }

    private suspend fun findShortestPath(
        startingPoint: String,
        destinationPoint: String
    ): List<String> = withContext(Dispatchers.Default) {
        val queue = PriorityQueue<Node>()
        val visited = mutableSetOf<String>()
        val parentMap = HashMap<String, String>()

        queue.add(Node(startingPoint, 0))

        while (queue.isNotEmpty()) {
            val current = queue.poll()

            if (current.location == destinationPoint) {
                // Found the destination, reconstruct the path
                val path = mutableListOf<String>()
                var node = current.location

                while (node != startingPoint) {
                    path.add(node)
                    node = parentMap[node] ?: break
                }

                path.add(startingPoint)
                return@withContext path.reversed()
            }

            if (current.location !in visited) {
                visited.add(current.location)

                for (neighbor in adjacencyList[current.location] ?: emptyList()) {
                    if (neighbor !in visited) {
                        val cost = current.cost + 1 // Assuming each step has a cost of 1

                        queue.add(Node(neighbor, cost))
                        parentMap[neighbor] = current.location
                    }
                }
            }
        }

        return@withContext emptyList()
    }

    private fun displayPathImages(path: List<String>) {
        // Display the images corresponding to the locations in the path
        val imageIds = path.map { locationImages[it] ?: 0 }
        // Set up your ImageView or other UI elements to display the images
        // Example: pathImageView.setImageResource(imageIds.firstOrNull() ?: 0)
    }

    private data class Node(val location: String, val cost: Int) : Comparable<Node> {
        override fun compareTo(other: Node): Int = cost.compareTo(other.cost)
    }
}
android algorithm kotlin navigation
1个回答
0
投票

您当前的代码只有1个图像视图

pathImageView
,设置图像的代码已在
displayPathImages

中注释
// Example: pathImageView.setImageResource(imageIds.firstOrNull() ?: 0)

取消注释代码,更好的方法可以是:

imageIds.firstOrNull()?.let {
   pathImageView.setImageResource(it)
}

您当前的代码仅显示一张图像,通过查看您的代码看起来您想要显示图像列表,您可以使用回收器视图

© www.soinside.com 2019 - 2024. All rights reserved.