Step:
Your data class
- 1. write your own data class
- 2. override the toString() method of your data class
- 3. add it to mutable list
- 4. write on onItemSelected listener
- 5. get selected item, and retrieval the object of your data class
class MenuData(var menuName: String , var waterLevel: String // small, standard, long , var foamLevel: String // none, standard, thick , var grindLevel: String // fine, medium, coarse , var tasteLevel: String // mild, standard, strong , var menuId: Long // ID on web server , var menuType: String // general, customized ) { /** for displaying by spinner text, you should override toString() */ override fun toString(): String { return menuName } }Menu.kt
import android.app.Activity import android.content.DialogInterface import android.os.Bundle import android.view.View import android.widget.* import org.json.JSONArray import com.loopj.android.http.AsyncHttpClient import com.loopj.android.http.AsyncHttpResponseHandler import com.loopj.android.http.RequestParams import cz.msebera.android.httpclient.Header class Menu : Activity() { private var serverDestination: String? = null private var spnMenu: Spinner? = null private var menuAdapter: ArrayAdapter? = null private val menuList: MutableList = mutableListOf() override fun onCreate(savedInstanceState: Bundle?) { val bundle = this.intent.extras m_accessToken = bundle!!.getString("accessToken") m_refreshToken = bundle.getString("refreshToken") super.onCreate(savedInstanceState) spnMenu = findViewById(R.id.spi_CoffeeMenuCloud) invoke_InitMenu("$serverDestination/menu") spnMenu!!.onItemSelectedListener = spnMenuListener } fun invoke_InitMenu(url: String) { val temp = "Bearer " + m_accessToken!! val client = AsyncHttpClient() client.addHeader("authorization", temp) client.addHeader("Content-Type", "application/json") client.get(url, object : AsyncHttpResponseHandler() { override fun onSuccess(statusCode: Int, headers: Array , responseBody: ByteArray) { val data = String(responseBody) // Decoding downloaded menu data try { val mJMenu = JSONArray(data) val userMenuCount = mJMenu.length() menuList.clear() if (userMenuCount > 0) { for (i in 0 until userMenuCount) { val mJMenuTmp = mJMenu.getJSONObject(i) val menuId = mJMenuTmp.getLong("menu_id") val menuName = mJMenuTmp.getString("name") val menuWaterLevel = mJMenuTmp.getString("water_level") val menuFoamLevel = mJMenuTmp.getString("foam_level") val menuTasteLevel = mJMenuTmp.getString("taste_level") val menuGrindSize = mJMenuTmp.getString("grind_size") val menuType = mJMenuTmp.getString("menu_type") val menuItem = MenuData(menuName, menuWaterLevel, menuFoamLevel, menuGrindSize, menuTasteLevel, menuId, menuType) menuList.add(menuItem) } } menuAdapter = ArrayAdapter(this@Menu, android.R.layout.simple_spinner_item, menuList) menuAdapter!!.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) menuAdapter!!.notifyDataSetChanged() spnMenu!!.adapter = menuAdapter } catch (err_Mag: Exception) { Toast.makeText(applicationContext, err_Mag.message, Toast.LENGTH_LONG).show() } Toast.makeText(applicationContext, "Menu sync successful.", Toast.LENGTH_LONG).show() } override fun onFailure(statusCode: Int, headers: Array , responseBody: ByteArray, error: Throwable) { Toast.makeText(applicationContext, "Menu sync failed.", Toast.LENGTH_LONG).show() } }) } private val spnMenuListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) { val menuId = menuList[position].menuId Toast.makeText(this@Menu, "Menu ID: $menuId", Toast.LENGTH_SHORT).show() val tasteLevel = menuList[position].tasteLevel Toast.makeText(this@Menu, "Taste level: $tasteLevel", Toast.LENGTH_SHORT).show() } override fun onNothingSelected(parent: AdapterView<*>) { } } }
沒有留言:
張貼留言