I don't know how to pass the database id during the screen transition.

Asked 2 years ago, Updated 2 years ago, 81 views

"I would like to create a ""Tap each item in ListView to see the details screen corresponding to the item"" flow in Kotlin."

I don't know how to get the id of the item I tapped and give it to the detail screen

PutExtra sends appropriate text and displays toast on the detail screen

// Tap each item → transition to detail screen
MainListView.setOnItemClickListener {_,_,_,_->
    val intent:Intent=Intent(this, DetailActivity::class.java)
    int.putExtra ("TapID", "ID of the tapped item")
    startActivity (intent)
}
class DetailActivity:AppCompatActivity(){
    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_detail)

        Toast.makeText(this, "${intent.extras.get("TapID")}", Toast.LENGTH_SHORT).show()
    }
}

android database kotlin android-intent

2022-09-30 14:58

2 Answers

If you're using ListView, I think you've given the adapter to that ListView.

If you are using ArrayAdapter as an Adapter, I think you have given ArrayAdapter some sort of array.An id of the record is required in the element of the array.

By doing the following, you can retrieve the corresponding element in the array passed to ArrayAdapter. You can get an id from that element.

MainListView.setOnItemClickListener {parent,_,position,_->
    // Get the appropriate elements
    vallistView = parent as ListView
    value item=listView.getItemAtPosition(position) as Item//Get the element

    // give someone an id
    val intent:Intent=Intent(this, DetailActivity::class.java)
    int.putExtra("TapID", item.id) // Get id from affected element and set to int
    startActivity (intent)
}

(Item should be read as element type in array)

I'll take your comment and add it.

Do you mean you are handing String[] to ArrayAdapter?
If so, you cannot have an id in the String, so you will need to modify it as follows:

First, define the Item class.

private inner class Item (valid: Long, val name: String) {
    override fun to String(): String {
        return name
    }
}

You can now pair the string to be displayed in ListView with the database id.

Next, now,

string_array=emptyMutableListOf; String>()
string_array.add (first string)
string_array.add (second string)
// ...

adapter=ArrayAdapter<String>(context, resource, string_array);

I think I'm making ArrayAdapter like that, so

item_array=emptyMutableListOf;Item>()
item_array.add (item (first id, first string))
item_array.add(Item(second id, second string))
// ...

adapter=ArrayAdapter<Item>(context, resource, item_array);

Change it to pass an array of items (listed in the code above, but either should be fine).

And with ListView using this adapter,

MainListView.setOnItemClickListener {parent,_,position,_->
    // Get the appropriate elements
    vallistView = parent as ListView
    value item=listView.getItemAtPosition(position) as Item//Get the element

    // give someone an id
    val intent:Intent=Intent(this, DetailActivity::class.java)
    int.putExtra("TapID", item.id) // Get id from affected element and set to int
    startActivity (intent)
}

You can get an id from the item as shown in .

There may be other things that need to be done, but please correct them accordingly.

(Do not replace this Item with String)


2022-09-30 14:58

extraData is put for intent, so
Obtain Intent and ExtraData from intent to get when retrieving at the transition destination.

DetailActivity.kt

class DetailActivity:AppCompatActivity(){
    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_detail)

        Intent=getIntent();
        String tapid=intent.getStringExtra("TapID");
        Toast.makeText(this, tapid, Toast.LENGTH_SHORT).show()
    }
}


2022-09-30 14:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.