0

I'm moving from java to kotlin and I faced with some difficulties which are connected with static method usage at kotlin. I'd like to get access from recyclerView adapter to views at my activity. At Java I did smth like that at adapter:

WriteResponseMess.deleteAttachment(position);

and static method at my activity:

public static void deleteAttachment(int adapterPosition) {
        mNames = adapter.getItems();
        mNames.remove(adapterPosition);
        adapter.updateNames(mNames);
        adapter.notifyDataSetChanged();
        mNames = adapter.getItems();
    }

right now I want to do it via kotlin. The main tack is that I have to delete item of RV and update views. I have read this and this resources and I have done smth like that:

companion object {
        @JvmStatic
        fun deleteAttachment(position: Int) {

        }
    }

but I don't have any access to activity variables, so what I have to do in that situation?

Andrew
  • 1,947
  • 2
  • 23
  • 61
  • Try `WriteResponseMess.Companion.deleteAttachment(position);`. is this what you are asking ? – ADM May 15 '19 at 07:25
  • With annotation @JvmStatic it should work without Companion – Artem Botnev May 15 '19 at 07:27
  • @ADM, I have the access to this method, but variables are still closed for me :( – Andrew May 15 '19 at 07:29
  • You're holding data within static fields. This means that you have serious correctness and architectural problems. Fix them first (`static` is only for constants), migrate them (if this makes sense). – Miha_x64 May 15 '19 at 07:56
  • @Miha_x64, I'm moving from java and don't know some parts of kotlin, I suggested that it will be right for this task, so I don't have to use static as I understand? how I can solve this task without static fields? via interface or like what? – Andrew May 15 '19 at 08:02
  • @AndrewGoroshko I can't answer without knowing why do you use statics. If you need to pass data across activities, consider implementing `Parcelable` and using Intent Extras. – Miha_x64 May 15 '19 at 08:18
  • @AndrewGoroshko, why is that method static? – r2rek May 15 '19 at 08:53
  • @r2rek, because I need to use method from adapter – Andrew May 15 '19 at 10:40
  • @AndrewGoroshko and are you using it from outside the activity? – r2rek May 15 '19 at 10:54
  • @r2rek, about what are you asking?? about method or what? I use this method inside activity, but I don't have access to activity members – Andrew May 15 '19 at 11:03
  • If you're using it inside the activity that has the adapter, then you shouldn't make it static – r2rek May 15 '19 at 11:03
  • but I need to send data from adapter to activity, and as I understand I have to use static for it – Andrew May 15 '19 at 11:06

1 Answers1

1

A static block can access only static members.

The activity member variables should also be a part of the companion object.

For instance :-

companion object {
    var mNames : MutableList<Name> = mutableListOf // is a member variable
    ......
    fun deleteAttachment(position: Int) {

    }
}
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
  • I don't understand about member variables, right now I "see" this fun, but I can't get access to activity variables anyway – Andrew May 15 '19 at 07:31
  • You have to **make your activity variables static** to gain access to them by a **static function** .. @AndrewGoroshko `deleteAttachment` is a static function. – Santanu Sur May 15 '19 at 07:34
  • hmmm... this companion object is placed at my activity, and I thought that I can get access to activity variables like in Java – Andrew May 15 '19 at 07:34
  • and how I have to change `var` to `static`? in kotlin as I understand we have smth like companion, but I didn't manage to use it for my purposes – Andrew May 15 '19 at 07:36
  • `To achieve what you want` ... you need to use an `interface or lambda functions` in kotlin ... **which is altogether a separate question** . Check this https://antonioleiva.com/lambdas-kotlin/ @AndrewGoroshko – Santanu Sur May 15 '19 at 07:40
  • so as I understood I can't do it like in Java?) – Andrew May 15 '19 at 07:41
  • Correct.. in this case.. you cant. but you can surely use `lambdas` which is also recommended in java... instead of using static functions.. @AndrewGoroshko – Santanu Sur May 15 '19 at 07:42
  • thank you for your help, I didn't understand bigger part of info which you gave, so I will go into kotlin and will try to solve this problem :) – Andrew May 15 '19 at 07:44
  • To clarify: this answer suggests converting existing bugs to a new language. – Miha_x64 May 15 '19 at 07:57
  • @Miha_x64, which bugs did you mean? – Andrew May 15 '19 at 07:59
  • @AndrewGoroshko, I mean keeping data in static fields. This is implies coupling, memory leak, and they will disappear after process restart. – Miha_x64 May 15 '19 at 08:17
  • @Miha_x64, I know that static fields cause some problems, but I managed to solve this task only via static fields :( – Andrew May 15 '19 at 08:18