diff --git a/.idea/modules.xml b/.idea/modules.xml index cf763ec..8d9fd1f 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,8 +2,10 @@ - + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/xyz/mtfos/buywhat/MainActivity.kt b/app/src/main/java/xyz/mtfos/buywhat/MainActivity.kt index 83bd0ce..14497e9 100644 --- a/app/src/main/java/xyz/mtfos/buywhat/MainActivity.kt +++ b/app/src/main/java/xyz/mtfos/buywhat/MainActivity.kt @@ -11,5 +11,6 @@ class MainActivity : AppCompatActivity() { setContentView(R.layout.activity_main) startActivity(Intent(this@MainActivity, StoreList::class.java)) + finish() } } diff --git a/app/src/main/java/xyz/mtfos/buywhat/StoreList.kt b/app/src/main/java/xyz/mtfos/buywhat/StoreList.kt index 409ef84..d0360c1 100644 --- a/app/src/main/java/xyz/mtfos/buywhat/StoreList.kt +++ b/app/src/main/java/xyz/mtfos/buywhat/StoreList.kt @@ -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 = 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() } -} \ No newline at end of file + + 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(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 + } + } + +} + diff --git a/app/src/main/java/xyz/mtfos/tools/apiObject.kt b/app/src/main/java/xyz/mtfos/tools/apiObject.kt index 0463c51..2891cec 100644 --- a/app/src/main/java/xyz/mtfos/tools/apiObject.kt +++ b/app/src/main/java/xyz/mtfos/tools/apiObject.kt @@ -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) + } + }) } } \ No newline at end of file diff --git a/app/src/main/java/xyz/mtfos/tools/objectTool.kt b/app/src/main/java/xyz/mtfos/tools/objectTool.kt index 893e3b2..287a23e 100644 --- a/app/src/main/java/xyz/mtfos/tools/objectTool.kt +++ b/app/src/main/java/xyz/mtfos/tools/objectTool.kt @@ -7,14 +7,16 @@ import android.app.Activity import android.support.annotation.IdRes import android.view.View -fun Activity.bind(@IdRes idRes: Int): Lazy { - @Suppress("UNCHECKED_CAST") - return unsafeLazy { findViewById(idRes) } -} +object objectTool { + fun Activity.bind(@IdRes idRes: Int): Lazy { + @Suppress("UNCHECKED_CAST") + return unsafeLazy { findViewById(idRes) } + } -fun View.bind(@IdRes idRes: Int): Lazy { - @Suppress("UNCHECKED_CAST") - return unsafeLazy { findViewById(idRes) } -} + fun View.bind(@IdRes idRes: Int): Lazy { + @Suppress("UNCHECKED_CAST") + return unsafeLazy { findViewById(idRes) } + } -private fun unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer) \ No newline at end of file + private fun unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer) +} \ No newline at end of file diff --git a/app/src/main/res/drawable-hdpi/ic_refresh_black_24dp.png b/app/src/main/res/drawable-hdpi/ic_refresh_black_24dp.png new file mode 100644 index 0000000..9d94c39 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_refresh_black_24dp.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_refresh_black_24dp.png b/app/src/main/res/drawable-mdpi/ic_refresh_black_24dp.png new file mode 100644 index 0000000..c82efd5 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_refresh_black_24dp.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_refresh_black_24dp.png b/app/src/main/res/drawable-xhdpi/ic_refresh_black_24dp.png new file mode 100644 index 0000000..0b73128 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_refresh_black_24dp.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_refresh_black_24dp.png b/app/src/main/res/drawable-xxhdpi/ic_refresh_black_24dp.png new file mode 100644 index 0000000..91187eb Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_refresh_black_24dp.png differ diff --git a/app/src/main/res/drawable-xxxhdpi/ic_refresh_black_24dp.png b/app/src/main/res/drawable-xxxhdpi/ic_refresh_black_24dp.png new file mode 100644 index 0000000..bcaba5f Binary files /dev/null and b/app/src/main/res/drawable-xxxhdpi/ic_refresh_black_24dp.png differ diff --git a/app/src/main/res/layout/activity_store_list.xml b/app/src/main/res/layout/activity_store_list.xml index 062c427..48ce2f1 100644 --- a/app/src/main/res/layout/activity_store_list.xml +++ b/app/src/main/res/layout/activity_store_list.xml @@ -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" /> + android:gravity="center" + android:orientation="vertical"> + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4dc7b80..dc1fd15 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,4 @@ BuyWhat + 商店列表