欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

kotlin

程序员文章站 2022-04-26 15:40:57
...

kotlin


Kotlin 是一种在 Java 虚拟机上运行的静态类型编程语言,被称之为 Android 世界的Swift,由 JetBrains 设计开发并开源。
Kotlin 可以编译成Java字节码,也可以编译成 JavaScript,方便在没有 JVM 的设备上运行。
在Google I/O 2017中,Google 宣布 Kotlin 成为 Android 官方开发语言。

kotlin中使用接近于Java的方式(内置类型)来表示数字,但是又不完全相同,
比如没有隐式转换!Kotlin中数字相关的内置类型如下:
需要注意几点:
● 1.没有自动向上转型,比如Int转Long,需要自己调toXxx方法转
● 2.Long类型结尾必须为大写的L,不能为小写,比如1024L
● 3.字符Char不是Number,用单引号来声明,比如’c’,不能像Java一样直接拿来当数字使,
如果你想把Char的值给Int,需要调toInt()方法
● 4.Boolean的值为true或false
● 5.Kotlin不支持8进制,十六进制0x开头,二进制0b开头k
● 6.位运算符,Java中的与或运算符用:|和&,kotlin中使用or和and关键字来替代
其他运算符也有分别的关键字替代:shl(有符号左移),shr(有符号右移),ushr(无符号右移)

LiseView

kotlin
布局

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <com.example.a123.dayk1.view.XListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></com.example.a123.dayk1.view.XListView>
</android.support.constraint.ConstraintLayout>

BaseAdapter

class MyBaseAdapter (var content:Context , var list:ArrayList<MyFood>): BaseAdapter() {

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    var viewHolder:ViewHolder?
    var view:View?
    if (convertView==null){
        viewHolder=ViewHolder()
        view = View.inflate(content,R.layout.name1,null)
        viewHolder.title=view.findViewById(R.id.t1)
        viewHolder.foods=view.findViewById(R.id.t2)
        viewHolder.imag=view.findViewById(R.id.iv1)
        view.tag = viewHolder
    }else{
        view = convertView
        viewHolder = view.tag as ViewHolder
    }

    val get = list.get(position)
    viewHolder.title?.text = get.title
    viewHolder.foods?.text = get.foods
    Glide.with(content).load(get.pic).apply(RequestOptions.circleCropTransform()).into(viewHolder.imag)
    return view!!

}
inner class ViewHolder{
    var title:TextView? = null
    var foods:TextView? = null
    var imag:ImageView? = null
}

override fun getItem(position: Int): Any {
    return list.get(position)
}

override fun getItemId(position: Int): Long {
   return position.toLong()
}

override fun getCount(): Int {
    return list.size
}
}

自定义类

class MyFood  {
    var title:String? =null
    var foods:String? =null
    var pic:String? = null
constructor(title: String?, foods: String?, pic: String?) {
    this.title = title
    this.foods = foods
    this.pic = pic
}

override fun toString(): String {
    return "MyFood(title=$title, foods=$foods, pic=$pic)"
}

}

Activity

//*实现接口XListView*


    class MainActivity : AppCompatActivity(),XListView.IXListViewListener{
override fun onRefresh() {
    list.clear()
    page=1
    Thread(Runnable {
        val readText = URL("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=$page").readText()
        val jsonObject = JSONObject(readText)
        val jsonArray = jsonObject.getJSONArray("data")
        for (i in 0..jsonArray.length()-1 step 1){
            val jsonObject1 = jsonArray.getJSONObject(i)
            val title = jsonObject1.getString("title")
            val food_str = jsonObject1.getString("food_str")
            val pic = jsonObject1.getString("pic")
            list.add(MyFood(title,food_str,pic))
        }
    }).start()
    myBaseAdapter = MyBaseAdapter(this, list)
    mylist.adapter = myBaseAdapter
    myBaseAdapter!!.notifyDataSetChanged()
    load()
}

override fun onLoadMore() {

    page+=1
    Thread(Runnable {
        val readText = URL("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=$page").readText()
        val jsonObject = JSONObject(readText)
        val jsonArray = jsonObject.getJSONArray("data")
        for (i in 0..jsonArray.length()-1 step 1){
            val jsonObject1 = jsonArray.getJSONObject(i)
            val title = jsonObject1.getString("title")
            val food_str = jsonObject1.getString("food_str")
            val pic = jsonObject1.getString("pic")
            list.add(MyFood(title,food_str,pic))
        }
    }).start()

    load()
}

 fun load(){
    mylist.stopLoadMore()
     mylist.stopRefresh()

}

var list:ArrayList<MyFood> = ArrayList()
var page:Int = 1
var myBaseAdapter:MyBaseAdapter? =null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

        Thread(Runnable {
            val readText = URL("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=$page").readText()
            val jsonObject = JSONObject(readText)
            val jsonArray = jsonObject.getJSONArray("data")
            for (i in 0..jsonArray.length()-1 step 1){
                val jsonObject1 = jsonArray.getJSONObject(i)
                val title = jsonObject1.getString("title")
                val food_str = jsonObject1.getString("food_str")
                val pic = jsonObject1.getString("pic")
                list.add(MyFood(title,food_str,pic))
            }
            myBaseAdapter!!.notifyDataSetChanged()
        }).start()



    myBaseAdapter = MyBaseAdapter(this, list)
    mylist.adapter = myBaseAdapter

    mylist.setPullLoadEnable(true)
    mylist.setPullRefreshEnable(true)
    mylist.setXListViewListener(this)

}
}