{"id":6323,"date":"2014-04-15T19:46:24","date_gmt":"2014-04-15T19:46:24","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/15\/how-to-select-from-resources-randomly-r-drawable-xxxx-collection-of-common-programming-errors\/"},"modified":"2014-04-15T19:46:24","modified_gmt":"2014-04-15T19:46:24","slug":"how-to-select-from-resources-randomly-r-drawable-xxxx-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/15\/how-to-select-from-resources-randomly-r-drawable-xxxx-collection-of-common-programming-errors\/","title":{"rendered":"how to select from resources randomly (R.drawable.xxxx)-Collection of common programming errors"},"content":{"rendered":"<p>I did this in my Applez application as follows;<\/p>\n<p>1) create an ArrayList to hold images and fill it 2) take random number and return corresponding image.<\/p>\n<p>in code :<\/p>\n<pre><code>public static void fruitInventory() {\nfruitResources.clear();\nField[] fields = R.drawable.class.getFields();\n\nfor (Field field : fields) {\n    \/\/ Take only those with name starting with \"fr\"\n    if (field.getName().startsWith(\"fr_\")) {\n        try {\n            String FType = field.getName().substring(3);\n\n            fruitResources.add(field.getInt(null));\n            alfruitTypes.add(FType);\n            Log.d(\"FruitManager\", \"Type \" + FType);\n\n        } catch (IllegalArgumentException e) {\n\n            e.printStackTrace();\n        } catch (IllegalAccessException e) {\n\n            e.printStackTrace();\n        }\n    }\n\n\n}\n<\/code><\/pre>\n<p>and to add the fruit :<\/p>\n<pre><code>public static void addFruit(Context context){\n\nfruitInventory();\n\nfor (int i = 0; i &lt; 10; i++) {\nRandom Rnd = new Random();\nint FruitType = Rnd.nextInt(fruitResources.size());\nGameObject nextApple = new GameObject(BitmapFactory.decodeResource(context.getResources(),\n                  fruitResources.get(FruitType)), FruitType);\n\nMainGamePanel.AppleList.add(nextApple);\n}\n<\/code><\/pre>\n<p>}<\/p>\n<p>Idea was (and works) that I could add extra fruits to the game, simply by adding for instance &#8220;fr_eggplant.bmp&#8221; to the images folder.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I did this in my Applez application as follows; 1) create an ArrayList to hold images and fill it 2) take random number and return corresponding image. in code : public static void fruitInventory() { fruitResources.clear(); Field[] fields = R.drawable.class.getFields(); for (Field field : fields) { \/\/ Take only those with name starting with &#8220;fr&#8221; [&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-6323","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6323","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=6323"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6323\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}