简体   繁体   中英

Android pass variable from Java activity to Kotlin adapter

I don't know why I'm blanking on this... but I've got a java activity which displays comments... and I need to pass the id of the photo that's being commented onto the adapter that gets all of the comments. The adapter is called CommentGrabber:

commentGrabber = new CommentGrabber(this);

...and it's executed like this:

private void requestComment() {
    commentGrabber.getComment();
}

The "id" variable of the current photo can be had at any time by getting its intent but I've saved it to a string called "photo_id."

final String photo_id = getIntent().getStringExtra("id");

This is what the adapter side looks like:

fun getComment(String photo_id) {
//this is where the function is handled
}

So I just need to figure out how to get the "photo_id" from my comment activity to "getComment" in the adapter.

I would have the adapter method expect an argument like below:

fun getComment(photo_id : String) {
    // from there then pass the photo_id to the service call
}

You would then call it like so:

adapter.getComment(photo_id);

Whenever you want to fetch comments by id.

I hope this makes sense. If you need further clarification, please do not hesitate to ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM