简体   繁体   English

Android 将变量从 Java 活动传递到 Kotlin 适配器

[英]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.我不知道为什么我对此空白...但是我有一个显示评论的java活动...我需要将正在评论的照片的ID传递给获取所有的适配器注释。 The adapter is called CommentGrabber:该适配器称为 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."当前照片的“id”变量可以随时通过获取其意图来获得,但我已将其保存到名为“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.所以我只需要弄清楚如何从我的评论活动中获取“photo_id”到适配器中的“getComment”。

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.每当您想按 id 获取评论时。

I hope this makes sense.我希望这是有道理的。 If you need further clarification, please do not hesitate to ask.如果您需要进一步说明,请随时询问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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