Search code examples
androidalgorithmkotlinnavigation

Images are not showing in the Image_view


I am creating a project for my college navigation. I have 13 location points in a closed loop structure as I am working on a single floor (Sixth Floor). I'm taking two inputs from the user as current location and destination and then using A* algorithm to find the shortest path and using images to navigate the user like google street view.

But the problem is that my activity_image_display is totally blank when I run the application and I cannot figure out why

My codes

Main Activity

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

Navigation Display 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)
    }
}

Solution

  • You current code only have 1 image view pathImageView, and the code for setting image to it is commented in displayPathImages

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

    Uncomment the code, better way of doing this can be:

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

    You current code only show one image, By looking at you code looks like you want to display a list of images, you can use a recycler view for that