update store page

This commit is contained in:
Jay 2017-07-05 23:46:05 +08:00
parent 872062b3c5
commit 80693cff86
14 changed files with 171 additions and 19 deletions

View File

@ -2,8 +2,10 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/BuyWhat.iml" filepath="$PROJECT_DIR$/BuyWhat.iml" />
<module fileurl="file://C:\Project\BuyWhatNative\BuyWhat.iml" filepath="C:\Project\BuyWhatNative\BuyWhat.iml" />
<module fileurl="file://$PROJECT_DIR$/BuyWhatNative.iml" filepath="$PROJECT_DIR$/BuyWhatNative.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
<module fileurl="file://C:\Project\BuyWhatNative\app\app.iml" filepath="C:\Project\BuyWhatNative\app\app.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -11,5 +11,6 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)
startActivity(Intent(this@MainActivity, StoreList::class.java))
finish()
}
}

View File

@ -1,10 +1,19 @@
package xyz.mtfos.buywhat
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.ListView
import android.widget.TextView
import xyz.mtfos.tools.bind
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.
@ -12,10 +21,81 @@ import xyz.mtfos.tools.bind
class StoreList : Activity() {
private val lv: ListView by bind(R.id.storelv)
private val tvTitle: TextView by bind(R.id.tvTitle)
private val ivRefresh: ImageView by bind(R.id.ivRefresh)
private val api: apiObject = apiObject(this@StoreList)
private var storeList: ArrayList<JSONObject> = ArrayList()
private var adapter: lvAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(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
var intent = Intent(this@StoreList, null)
intent.putExtra("id", sid)
startActivity(intent)
})
ivRefresh.setOnClickListener { v ->
getList()
}
getList()
}
fun getList() {
api.getStore(object : apiObject.apicb {
override fun Callback(json: JSONObject?) {
if (json!!["status"] == 1) {
var jarr: JSONArray = json!!["record"] as JSONArray
if(jarr.length() > 0) {
storeList.clear()
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
}
}
}

View File

@ -2,7 +2,9 @@ package xyz.mtfos.tools
import android.content.Context
import android.util.Log
import okhttp3.*
import org.json.JSONObject
import java.io.IOException
/**
* Created by jay on 2017/7/5.
@ -11,6 +13,8 @@ import org.json.JSONObject
open class apiObject constructor(val ctx: Context) {
private val tag: String = "API Object"
private val client: OkHttpClient = OkHttpClient()
interface apicb {
fun Callback(json: JSONObject?)
}
@ -19,12 +23,46 @@ open class apiObject constructor(val ctx: Context) {
Log.d(tag, "API Object Initialized")
}
open fun getStore(){
open fun getStore(cb: apicb){
val url:String = "${projectSet.apiUrl}/store"
var req:Request = Request.Builder()
.url(url)
.build()
send(req, object : apicb {
override fun Callback(json: JSONObject?) {
cb.Callback(json)
}
})
}
fun send(cb: apicb) {
open fun getItems(storeId: Int, cb: apicb) {
val url:String = "${projectSet.apiUrl}/item/${storeId}"
var req:Request = Request.Builder()
.url(url)
.build()
send(req, object : apicb {
override fun Callback(json: JSONObject?) {
cb.Callback(json)
}
})
}
fun send(req: Request, cb: apicb) {
client.newCall(req).enqueue(object: Callback{
override fun onFailure(call: Call?, e: IOException?) {
e?.printStackTrace()
val json : JSONObject = JSONObject()
json.put("status", 0)
json.put("message", "System Error")
cb.Callback(json)
}
override fun onResponse(call: Call?, response: Response?) {
var str: String? = null
str = response?.body()!!.string()
val json : JSONObject = JSONObject(str)
cb.Callback(json)
}
})
}
}

View File

@ -7,14 +7,16 @@ import android.app.Activity
import android.support.annotation.IdRes
import android.view.View
fun <T : View> Activity.bind(@IdRes idRes: Int): Lazy<T> {
@Suppress("UNCHECKED_CAST")
return unsafeLazy { findViewById<T>(idRes) }
}
object objectTool {
fun <T : View> Activity.bind(@IdRes idRes: Int): Lazy<T> {
@Suppress("UNCHECKED_CAST")
return unsafeLazy { findViewById<T>(idRes) }
}
fun <T : View> View.bind(@IdRes idRes: Int): Lazy<T> {
@Suppress("UNCHECKED_CAST")
return unsafeLazy { findViewById<T>(idRes) }
}
fun <T : View> View.bind(@IdRes idRes: Int): Lazy<T> {
@Suppress("UNCHECKED_CAST")
return unsafeLazy { findViewById<T>(idRes) }
}
private fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer)
private fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer)
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 249 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 506 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 721 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 948 B

View File

@ -26,14 +26,22 @@
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商店列表"
android:text="@string/store_title"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="30dp"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/ivRefresh"
android:layout_width="25dp"
android:layout_height="25dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_refresh_black_24dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="30dp">
<TextView
android:id="@+id/tvItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20dp"
android:text="Simple"/>
</LinearLayout>

View File

@ -1,3 +1,4 @@
<resources>
<string name="app_name">BuyWhat</string>
<string name="store_title">商店列表</string>
</resources>