Saturday, December 29, 2018

android kotlin ArrayAdapter withen JSONObject itemdata

private inner class StableArrayAdapter(context: Context, viewResourceId: Int)
    : ArrayAdapter<JSONObject>(context, viewResourceId) {

    fun addItems(items: JSONArray) {
        for (i in 0 until items.length()) {
            val item = items.getJSONObject(i)
            this.add(item)
        }
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var retView: View? = null        if (convertView == null) {
            retView = LayoutInflater.from(getContext()).inflate(R.layout.main_row_item, parent, false)
        } else {
            retView = convertView
        }
        val item = getItem(position) as JSONObject
        retView!!.tag = item

        if (item.optBoolean("is_denied"))
            retView!!.icon_state.setBackgroundResource(R.drawable.bg_oval_odo_maroon)
        else {
            if (item.optBoolean("is_received"))
                retView!!.icon_state.setBackgroundResource(R.drawable.bg_oval_disable_blue)
            else {
                if (item.optBoolean("is_approved"))
                    retView!!.icon_state.setBackgroundResource(R.drawable.bg_oval_odo_orange)
                else {
                    if (item.optBoolean("is_sent"))
                        retView!!.icon_state.setBackgroundResource(R.drawable.bg_oval_active_blue)
                    else                        retView!!.icon_state.visibility = View.GONE                }
            }
        }

        val dateStr = item.getString("created_at")
        retView.tv_datetime.setText(AppHelper.formatJsonDate(dateStr))

        val toc = item.getJSONObject("toc")
        val tob = item.getJSONObject("tob")
        retView.tv_name.setText(toc.getString("name") + " / " + tob.getString("name"))
        retView.tv_descr.setText("№ " + item.getString("order_number") + " - " + item.getString("order_name"))

        retView.setOnClickListener { view ->            itemClick(view)
        }        return retView!!
    }

    fun itemClick(view: View) {
        val item = view.tag as JSONObject

        val args = Bundle()
        args.putString(ArgumentPassKeys.ARG_JSON_OBJECT, item.toString())

        val intent = Intent(_context, EventsActivity::class.java)
        intent.putExtras(args)
        _activity!!.startActivityForResult(intent, ActivityPassCodes.ITEM_EDIT)
    }
}

No comments: