{"id":6964,"date":"2014-05-16T01:14:25","date_gmt":"2014-05-16T01:14:25","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/16\/get-text-of-dynamic-generated-textview-when-clicking-on-it-collection-of-common-programming-errors\/"},"modified":"2014-05-16T01:14:25","modified_gmt":"2014-05-16T01:14:25","slug":"get-text-of-dynamic-generated-textview-when-clicking-on-it-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/16\/get-text-of-dynamic-generated-textview-when-clicking-on-it-collection-of-common-programming-errors\/","title":{"rendered":"Get text of dynamic generated TextView when clicking on it-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m running through a for-loop and am generating TextViews that should be clickable because I want to start then an intent and pass the url as parameter as well as the source.<\/p>\n<p>So, I&#8217;ve tried this<\/p>\n<pre><code>articleURL[i].setPaintFlags(articleURL[i].getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);\narticleURL[i].setOnClickListener(new View.OnClickListener() {\n    @Override\n    public void onClick(View v) {\n        \/\/System.out.println(articleURL[v.getId()].getText().toString());\n        System.out.println(v.getId());\n    }\n});\n<\/code><\/pre>\n<p>The problem i encounter is that the v.getId() is always 0. And when i use the commented code<\/p>\n<pre><code>System.out.println(articleURL[v.getId()].getText().toString()); \n<\/code><\/pre>\n<p>I get an exception that says<\/p>\n<pre><code>java.lang.ArrayIndexOutOfBoundsException: length=10; index=-1\n<\/code><\/pre>\n<p>I just need the content of the TextView i clicked on. How exactly do i get it? articleURL[i] doesn&#8217;t work because he doesn&#8217;t know i then. How can v.getId() always be -1? No matter which one I click?<\/p>\n<p>This here is the complete for-loop<\/p>\n<pre><code>TextView articleURL = new TextView[hashMapSize];\nfor (int i = 0; i &lt; hashMapSize; i++) {\n    articleURL[i] = new TextView(getActivity());\n    articleURL[i].setPaintFlags(articleURL[i].getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);\n    articleURL[i].setOnClickListener(new View.OnClickListener() {\n        @Override\n        public void onClick(View v) {\n            System.out.println(articleURL[v.getId()].getText().toString());\n            \/\/System.out.println(v.getId());\n        }\n    });\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m running through a for-loop and am generating TextViews that should be clickable because I want to start then an intent and pass the url as parameter as well as the source. So, I&#8217;ve tried this articleURL[i].setPaintFlags(articleURL[i].getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); articleURL[i].setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { \/\/System.out.println(articleURL[v.getId()].getText().toString()); System.out.println(v.getId()); } }); The problem i [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6964","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6964"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6964\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}