简体   繁体   中英

Translate Kotlin To Java in Android When extends ArrayAdapter in CustomView

I learn some code in Kotlin and want to translate it into Java. In Kotlin, when creating CustomView, I see that they directly pass ArrayList parameter to class and extends ArrayAdapter like below:

class PostClass (private val userEmail: ArrayList<String>,
                 private val userImage: ArrayList<String>,
                 private val userComment: ArrayList<String>,
                 private val context: Activity) : ArrayAdapter<String>(context, R.layout.custom_view, userEmail)
{ //Do something }

I also try to convert to Java:

class PostClass(ArrayList<String> userEmail, 
ArrayList<String> userImage, 
ArrayList<String> userComment, Activity context) 
extends ArrayAdapter<String> (context, R.layout.custom_view, userEmail) 
{ //Do something }

However, it shows a lot errors. So, can anyone help me to convert it correctly? Thank you.

In java, you should not define constructor in class declaration. Instead you should create constructor as defined below with class definition.

public class PostClass extends ArrayAdapter<String> {

        public PostClass(ArrayList<String> userEmail,
                  ArrayList<String> userImage,
                  ArrayList<String> userComment,
                  Activity context) {
            super(context, R.layout.custom_view, userEmail);
        }
}

I hope that will help you any way.

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