{"id":36,"date":"2019-03-03T12:07:40","date_gmt":"2019-03-03T03:07:40","guid":{"rendered":"https:\/\/junkbulk.robots.jp\/blog\/?p=36"},"modified":"2019-03-03T12:07:40","modified_gmt":"2019-03-03T03:07:40","slug":"listview%e3%81%ae%e4%b8%ad%e3%81%ab%e7%b7%a8%e9%9b%86%e5%8f%af%e8%83%bd%e3%81%aatextviewedittext","status":"publish","type":"post","link":"https:\/\/junkbulk.com\/wp\/?p=36","title":{"rendered":"ListView\u306e\u4e2d\u306b\u7de8\u96c6\u53ef\u80fd\u306aTextView(EditText)"},"content":{"rendered":"\n<p>\n\u7de8\u96c6\u53ef\u80fd\u306aTextView\u304cListView\u306e\u4e2d\u306b\u3042\u308b\u5834\u5408\u304c\u4eca\u56de\u306e\u554f\u984c\u3067\u3059\u3002\n\n<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class GroupListAdapter(context: Context, var groups: MutableList&lt;MemoGroup>) :\n    ArrayAdapter&lt;MemoGroup> (context, 0, groups) {\ndata class ViewHolder(val textView: TextView)\n    private val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater\n\n    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {\n        var view = convertView\n        var holder: ViewHolder\n        if (view == null) {\n            view = layoutInflater.inflate(R.layout.view_group_setting, null)\n            holder = ViewHolder(\n                view.findViewById(R.id.textView)\n            )\n            view.tag = holder\n            holder.textView.addTextChangedListener(object: TextWatcher {\n                override fun afterTextChanged(p0: Editable?) {\n                    if(p0 != null) {\n                        groups[position].title = p0.toString()\n                    }\n                }\n\n                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}\n\n                override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}\n\n            })\n        } else {\n            holder = view.tag as ViewHolder\n        }\n        holder.textView.text = groups[position].title\n        return view!!\n    }\n}<\/pre>\n\n\n\n<p>\u672c\u5f53\u306f\u3082\u3046\u3061\u3087\u3063\u3068\u3054\u3061\u3083\u3054\u3061\u3083\u3057\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u591a\u5c11\u898b\u3084\u3059\u3044\u3088\u3046\u306b\u4fee\u6b63\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u3055\u3066\u3001\u3053\u308c\u3060\u3068\u4f55\u5ea6\u3082afterTextChanged\u304c\u547c\u3070\u308c\u3066\u3001\u3057\u304b\u3082position\u3082p0.toString()\u3082\u601d\u3063\u305f\u5024\u304c\u53d6\u308c\u306a\u3044\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/27741390\/listview-with-edittext-and-textwatcher-textwatcher-fires-multiple-times-more-t\">\u3053\u3053<\/a>\u3092\u53c2\u8003\u306b\u3057\u3066\u89e3\u6c7a\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class GroupListAdapter(context: Context, var groups: MutableList&lt;MemoGroup>) :\n    ArrayAdapter&lt;MemoGroup> (context, 0, groups) {\n    inner class MutableWatcher : TextWatcher {\n        var position: Int = 0\n        var active: Boolean = false\n        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}\n        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}\n        override fun afterTextChanged(s: Editable) {\n            if (active) {\n                groups[position].title =  s.toString()\n            }\n        }\n    }\n    data class ViewHolder(val textView: TextView, val watcher:MutableWatcher)\n    private val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater\n    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {\n        var view = convertView\n        var holder: ViewHolder\n        if (view == null) {\n            view = layoutInflater.inflate(R.layout.view_group_setting, null)\n            holder = ViewHolder(\n                view.findViewById(R.id.textView),\n                MutableWatcher()\n            )\n            view.tag = holder\n            holder.textView.addTextChangedListener(holder.watcher)\n        } else {\n            holder = view.tag as ViewHolder\n        }\n        holder.watcher.active = false\n        holder.textView.text = groups[position].title\n        holder.watcher.position = position\n        holder.watcher.active = true\n        return view!!\n    }\n}<\/pre>\n\n\n\n<p>textView.text\u306b\u4ee3\u5165\u3057\u3066\u3082afterTextChanged\u304c\u547c\u3070\u308c\u308b\u306e\u3067\u3001\u305d\u306e\u6642\u306f\u7121\u8996\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3001TextWatcher \u3092\u305d\u308c\u305e\u308c\u306b\u6301\u305f\u305b\u308b\u611f\u3058\u306a\u306e\u304b\uff1f\u307e\u3060\u52c9\u5f37\u304c\u8db3\u308a\u307e\u305b\u3093\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7de8\u96c6\u53ef\u80fd\u306aTextView\u304cListView\u306e\u4e2d\u306b\u3042\u308b\u5834\u5408\u304c\u4eca\u56de\u306e\u554f\u984c\u3067\u3059\u3002 \u672c\u5f53\u306f\u3082\u3046\u3061\u3087\u3063\u3068\u3054\u3061\u3083\u3054\u3061\u3083\u3057\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u591a\u5c11\u898b\u3084\u3059\u3044\u3088\u3046\u306b\u4fee\u6b63\u3057\u307e\u3057\u305f\u3002 \u3055\u3066\u3001\u3053\u308c\u3060\u3068\u4f55\u5ea6\u3082afterTextChanged\u304c\u547c\u3070\u308c &hellip; <a href=\"https:\/\/junkbulk.com\/wp\/?p=36\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;ListView\u306e\u4e2d\u306b\u7de8\u96c6\u53ef\u80fd\u306aTextView(EditText)&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/junkbulk.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/36"}],"collection":[{"href":"https:\/\/junkbulk.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/junkbulk.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/junkbulk.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/junkbulk.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=36"}],"version-history":[{"count":14,"href":"https:\/\/junkbulk.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":50,"href":"https:\/\/junkbulk.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions\/50"}],"wp:attachment":[{"href":"https:\/\/junkbulk.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/junkbulk.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/junkbulk.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}