kotlin简单小demo,请求,解析,gson的使用,volley的单例

-----------------------------------------------------------------------------------------------------------

mainactivity

class MainActivity : BaseActivity() {
   internal val WHAT_MESSAGE = 0
    var recyclerView : RecyclerView? = null
    var adapter : MyAdapter? = null


    override fun init() {
        setContentView(R.layout.activity_main)
        recyclerView = findViewById(R.id.recycler_view) as RecyclerView
        recyclerView!!.layoutManager = LinearLayoutManager(this)
        getData()
    }
    fun getData(){
        var jsonRequest = JsonObjectRequest(Request.Method.GET
                ,"http://www.tngou.net/api/food/list?id=1",null,Response.Listener {
             response: JSONObject ->
            var gson = Gson()
               var item : Foot = gson.fromJson(response.toString(),Foot::class.java)
               var list : List = item.tngou
               async {
                  uiThread { adapter = MyAdapter(this@MainActivity,list)
                  recyclerView!!.adapter = adapter
                  }
               }

        },Response.ErrorListener { error ->
            Log.d("---->error",error.toString())
        })
      SimpleRequest.getInstance(this).addToRequestQueue(jsonRequest)
    }

}
-----------------------------------------------------------------------------
adapter
class MyAdapter(var ctx:Context,var list: List) : Adapter() {
    var inflater : LayoutInflater


    init {
        inflater = LayoutInflater.from(ctx)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
       holder.textTitle!!.text = list.get(position).description
        holder.textContent!!.text = list.get(position).keywords
        Glide.with(ctx).load("http://img1.imgtn.bdimg.com/it/u=489743113,36423855&fm=214&gp=0.jpg").into(holder.image)

    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyViewHolder {
        var view : View = inflater.inflate(R.layout.item_view,null)
         var holder : MyViewHolder = MyViewHolder(view)
        return holder

    }

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


    class MyViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
      var textContent : TextView? = null
      var textTitle : TextView? = null
       var image : ImageView? =null
        init {
            textContent = itemView.findViewById(R.id.text_content) as TextView
            image = itemView.findViewById(R.id.iv) as ImageView
            textTitle = itemView.findViewById(R.id.description) as TextView
        }


    }

}
-----------------------------------------------------------------
volley的单例
class  SimpleRequest private constructor(var ctx: Context){
    private var requestQueue : RequestQueue? = null
    init {
        requestQueue = getRequestQueue()
    }
   private fun getRequestQueue() : RequestQueue? {
       if (requestQueue==null){
           requestQueue = Volley.newRequestQueue(ctx)
       }
       return requestQueue
   }
    //同步的代码块
    companion object{
        private var simpleRequest : SimpleRequest? = null
        @Synchronized  fun getInstance(ctx : Context) : SimpleRequest {
            if (simpleRequest == null){
                simpleRequest = SimpleRequest(ctx)
            }
            return simpleRequest as SimpleRequest
        }
    }
    //添加到请求队列
    fun <T> addToRequestQueue(request : Request<T>) {
        getRequestQueue()!!.add(request)

    }


}
---------------------------------------------------------------------------
两个dataclass
data class Foot(val status: Boolean,val total:Int,val tngou : List)
data class Tngou(val count : Int,var description : String,val disease : String,
                 val fcount : Int,val food:String,val id:Int,val img : String = "",val keywords : String,
                 val name:String,val rcount:Int,val summary:String,val symptom:String
)
-----------------------------------------------------------------------------------
baseActivity
abstract class BaseActivity : Activity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        init()
    }

    abstract fun init()
}


 






 

你可能感兴趣的:(软)