Kotlin初学者

在学习Kotlin之前要知道的事情

为什么Kotlin被创建?

Kotlin由JetBrains创建,JetBrains是一家以专业人士创造开发工具而闻名的公司。回到2011年8月,JetBrains发布了一篇博客文章,为什么JetBrains需要Kotlin,解释创建Kotlin背后的原因。要点如下:

提高JetBrains生产力

当时,JetBrains团队几乎全部使用Java语言编写了所有基于IntelliJ的IDE。花了很多时间用javac编译。他们希望通过转向更具表现力的语言来提高生产力。
推动IntelliJ IDEA的销售
他们希望Kotlin的企业开发框架和工具成为IntelliJ IDEA Ultimate(商业版)的一部分,从而提高其销售额。
通过保持信心推动公司业务
JetBrains受到许多专业开发人员的信任。他们希望推动公司的业务,并通过提高社区对JetBrains的认识和维护信任来吸引更多人开发工具。

Kotlin的特点

Kotlin是一种静态类型的语言。以下是Kotlin的一些重要功能:

开源

Kotlin是在Apache许可证2.0版下发布的。Kompiler(Kotlin编译器),IntelliJ IDEA插件,对基本Java库和构建工具的增强都是开源的。
与Java和Android互操作
它可与Java和Android 100%互操作。这意味着所有当前的Java / Android代码都可以与Kotlin无缝协作。 在一个项目中混合使用Java和Kotlin。实际上,Android团队宣布Kotlin是开发Android应用程序的一流语言。除此之外,它还允许开发人员将相当多的Kotlin项目编译为JavaScript模块。
简明和富有表现力
粗略估计表明,使用Kotlin可以将代码行中断约40%(与Java相比)。表达的意思是,编写代码很容易,编译人员可以轻松理解。

易于学习

它受Java,Scala,Groovy,C#,JavaScript和Gosu的影响。如果你了解这些编程语言,那学习Kotlin会很容易。

工具友好

Kotlin由JetBrains开发,JetBrains是一家以专业人员创造开发工具而闻名的公司。可以选择任何Java IDE来运行Kotlin或从命令行构建。

安全

它旨在消除代码中null引用的危险(Java出现的问题)。此外,它是静态类型的,可以在运行时捕获更多错误(类型安全)。

以前我们实例化一个类需要一个一个拿出来现在

现在代码只需要一句话

 var bean=Bean()
以前使用Thread线程获取网络数据很麻烦,现在使用Kotlin就很简便
 Thread{
            val readText = URL("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1").readText()
            val jsonObject = JSONObject(readText)
            val jsonArray = jsonObject.getJSONArray("data")
            for(i in 0..jsonArray.length()-1){
                val jsonObject1 = jsonArray.getJSONObject(i)
                val pic = jsonObject1.getString("pic")
                val string = jsonObject1.getString("title")
                var bean=Bean()
                bean.pic=pic
                bean.name=string
                arr.add(bean)
                Log.e("##",pic)
            }


        }.start()

现在就来实现用一个Kotlin一个简单的ListView

MainActivity.Kotlin
package com.example.kotlin

import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import com.squareup.picasso.Picasso
import org.json.JSONObject
import java.net.URL


class MainActivity : AppCompatActivity() {
   
    var arr=ArrayList<Bean>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

       //网络获取数据在里面操作
        Thread{
        //读出这个Json字符串,然后开始解析
            val readText = URL("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1").readText()
            val jsonObject = JSONObject(readText)
            val jsonArray = jsonObject.getJSONArray("data")
            for(i in 0..jsonArray.length()-1){
                val jsonObject1 = jsonArray.getJSONObject(i)
                val pic = jsonObject1.getString("pic")
                val string = jsonObject1.getString("title")
                var bean=Bean()
                bean.pic=pic
                bean.name=string
                //添加到集合里面
                arr.add(bean)
                Log.e("##",pic)
            }


        }.start()


        val myList=findViewById(R.id.list)as ListView
        myList.adapter=MyAdapter(this,arr)



    }
//匿名内部类操作BaseAdapter
   class MyAdapter(val context:Context, val arr: ArrayList<Bean>) : BaseAdapter() {

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
             val vie=View.inflate(context,R.layout.item,null)
            val text= vie.findViewById(R.id.list_text) as TextView
            text.text= arr[position].name
            val imag=vie.findViewById(R.id.image)as ImageView
            Picasso.with(context).load(arr[position].pic).into(imag)

            return vie

        }

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

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

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


    }
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <pl.droidsonroids.gif.GifImageView
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:src="@drawable/gg"
        />

    <pl.droidsonroids.gif.GifImageView
        android:id="@+id/g2"
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:src="@drawable/g2"
        />
    <pl.droidsonroids.gif.GifImageView
        android:id="@+id/g3"
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:src="@drawable/g3"
        />
<ListView
    android:id="@+id/list"
    android:layout_weight="4"
    android:layout_width="match_parent"
    android:layout_height="0dp"></ListView>
</LinearLayout>
实现效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值