103 lines
3.1 KiB
Kotlin
103 lines
3.1 KiB
Kotlin
package xyz.mtfos.buywhat
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.os.Bundle
|
|
import android.os.Handler
|
|
import android.os.Message
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import android.widget.*
|
|
import org.json.JSONArray
|
|
import org.json.JSONObject
|
|
import xyz.mtfos.tools.apiObject
|
|
import xyz.mtfos.tools.objectTool.bind
|
|
|
|
/**
|
|
* Created by jay on 2017/7/5.
|
|
*/
|
|
class StoreList : SampleActivity() {
|
|
private val lv: ListView by bind(R.id.storelv)
|
|
private val ivRefresh: ImageView by bind(R.id.ivRefresh)
|
|
private var storeList: ArrayList<JSONObject> = ArrayList()
|
|
private var adapter: lvAdapter? = null
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setViewSource(R.layout.activity_store_list)
|
|
|
|
adapter = lvAdapter(this@StoreList, 0)
|
|
|
|
lv.adapter = adapter
|
|
|
|
lv.setOnItemClickListener({ parent, view, position, id ->
|
|
val data: JSONObject = storeList.get(position) as JSONObject
|
|
val sid: Int = data!!["id"] as Int
|
|
val name: String = data!!["name"] as String
|
|
|
|
val intent = Intent(this@StoreList, ItemRandom::class.java)
|
|
intent.putExtra("id", sid)
|
|
intent.putExtra("name", name)
|
|
startActivity(intent)
|
|
})
|
|
|
|
ivRefresh.setOnClickListener { v ->
|
|
getList()
|
|
}
|
|
|
|
getList()
|
|
}
|
|
|
|
fun getList() {
|
|
toggleLoading(true)
|
|
api.getStore(object : apiObject.apicb {
|
|
override fun Callback(json: JSONObject?) {
|
|
uiHandler.post({ toggleLoading(false) })
|
|
if (json!!["status"] == 1) {
|
|
var jarr: JSONArray = json!!["record"] as JSONArray
|
|
storeList.clear()
|
|
if (jarr.length() > 0) {
|
|
for (i in 0..(jarr.length() - 1)) {
|
|
val tmp = jarr.getJSONObject(i)
|
|
storeList.add(tmp)
|
|
}
|
|
uiHandler.sendEmptyMessage(1)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
@SuppressLint("HandlerLeak")
|
|
private val uiHandler = object : Handler() {
|
|
override fun handleMessage(msg: Message?) {
|
|
super.handleMessage(msg)
|
|
when (msg!!.what) {
|
|
1 -> {
|
|
// update listview data
|
|
adapter!!.clear()
|
|
adapter!!.addAll(storeList)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
class lvAdapter(context: Context, resource: Int) : ArrayAdapter<JSONObject>(context, resource) {
|
|
|
|
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
|
|
val data = getItem(position) as JSONObject
|
|
var v: View = if (convertView == null) {
|
|
View.inflate(context, R.layout.listview_store_item, null)
|
|
} else {
|
|
convertView
|
|
}
|
|
val tv: TextView = v.findViewById(R.id.tvItem)
|
|
tv.text = data!!["name"] as String
|
|
return v
|
|
}
|
|
}
|
|
|
|
}
|
|
|