使用ArrayList的ArrayList在Android中打印随机Toast消息

huangapple go评论96阅读模式
英文:

Using ArrayList of ArrayList to print random Toast message in Android

问题

我正在尝试处理一些Android Java的问题,但在处理Toast消息时遇到了一些问题。我的目标是能够从另一个ArrayList中调用的字符串数组中发布随机的Toast消息。尽管我已经弄清楚了如何分别打印它们,但在处理Toast语法时感觉可能漏掉了很多东西。

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. ListView myListView = findViewById(R.id.myListView);
  7. final ArrayList<String> myFamily = new ArrayList<>(asList("Giovani", "Enu", "Xochle", "Rquel", "Falsto", "Bam", "Duncan", "Goose", "Edgar", "Frank"));
  8. ArrayList<String> gio = new ArrayList<>(asList(" His other brother ", " Met in high school ", " Played on the same sports team ", " Has two kids "));
  9. ArrayList<String> karina = new ArrayList<>(asList(" His youngest sister ", " Kinda short ", " Sweet but kinda dumb "));
  10. ArrayList<String> midget = new ArrayList<>(asList(" His Second youngest sister ", " Kinda short ", " Kinda a crazy cat lady ", " Cannot handle her liquor "));
  11. ArrayList<String> ma = new ArrayList<>(asList(" His other mother ", " Works way too hard ", " Sweetest lady in the world "));
  12. ArrayList<String> pa = new ArrayList<>(asList(" His other father ", " Not feeling well ", " Good heart hopefully he can live in peace "));
  13. ArrayList<String> me = new ArrayList<>(asList(" The coolest ", " Kinda short ", " Sweet but kinda dumb "));
  14. ArrayList<String> dunks = new ArrayList<>(asList(" Brother from another mother ", " Warrior born ", " Allergic to most things that could hinder combat "));
  15. ArrayList<String> gus = new ArrayList<>(asList(" His other brother ", " Loves cars ", " Talks alot of shit ", " Loves white girls ", " Memorised the metal gear series by heart"));
  16. ArrayList<String> edd = new ArrayList<>(asList(" Oldest of the other brother ", " Mostly quiet outside of social situations ", " Cares about saving his money ", " Laughs alot"));
  17. ArrayList<String> frank = new ArrayList<>(asList(" Other brother ", " Impulsively stupid but loyal ", " Really into recreational passions "));
  18. final Random chance = new Random();
  19. final ArrayList<ArrayList> bio = new ArrayList(Arrays.asList(gio, karina, midget, ma, pa, me, dunks, gus, edd, frank));
  20. ArrayAdapter<String> arrayManager = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, myFamily);
  21. myListView.setAdapter(arrayManager);
  22. myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  23. @Override
  24. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  25. Toast.makeText(getApplicationContext(), "Sup " + bio, Toast.LENGTH_LONG).show();
  26. }
  27. });
  28. }
  29. }

希望这对你有所帮助。

英文:

I'm trying to work through some Android Java stuff and I'm having some trouble working out a Toast message. My scope is being able to post a random toast message from an array of strings that was called from another ArrayList. I figured out how to print them out separately though I feel I might be missing a lot when it comes to the toast syntax since.

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. ListView myListView = findViewById(R.id.myListView);
  7. final ArrayList&lt;String&gt; myFamily = new ArrayList&lt;&gt;(asList(&quot;Giovani&quot;, &quot;Enu&quot;, &quot;Xochle&quot;, &quot;Rquel&quot;, &quot;Falsto&quot;, &quot;Bam&quot;, &quot;Duncan&quot;, &quot;Goose&quot;, &quot;Edgar&quot;, &quot;Frank&quot;));
  8. ArrayList&lt;String&gt; gio = new ArrayList&lt;&gt;(asList(&quot; His other brother &quot;, &quot; Met in high school &quot;, &quot; Played on the same sports team &quot;, &quot; Has two kids &quot;));
  9. ArrayList&lt;String&gt; karina = new ArrayList&lt;&gt;(asList(&quot; His youngest sister &quot;, &quot; Kinda short &quot;, &quot; Sweet but kinda dumb &quot;));
  10. ArrayList&lt;String&gt; midget = new ArrayList&lt;&gt;(asList(&quot; His Second youngest sister &quot;, &quot; Kinda short &quot;, &quot; Kinda a crazy cat lady &quot;, &quot; Cannot handle her liquor &quot;));
  11. ArrayList&lt;String&gt; ma = new ArrayList&lt;&gt;(asList(&quot; His other mother &quot;, &quot; Works way too hard &quot;, &quot; Sweetest lady in the world &quot;));
  12. ArrayList&lt;String&gt; pa = new ArrayList&lt;&gt;(asList(&quot; His other father &quot;, &quot; Not feeling well &quot;, &quot; Good heart hopefully he can live in peace &quot;));
  13. ArrayList&lt;String&gt; me = new ArrayList&lt;&gt;(asList(&quot; The coolest &quot;, &quot; Kinda short &quot;, &quot; Sweet but kinda dumb &quot;));
  14. ArrayList&lt;String&gt; dunks = new ArrayList&lt;&gt;(asList(&quot; Brother from another mother &quot;, &quot; Warrior born &quot;, &quot; Allergic to most things that could hinder combat &quot;));
  15. ArrayList&lt;String&gt; gus = new ArrayList&lt;&gt;(asList(&quot; His other brother &quot;, &quot; Loves cars &quot;, &quot; Talks alot of shit &quot;, &quot; Loves white girls &quot;, &quot; Memorised the metal gear series by heart&quot;));
  16. ArrayList&lt;String&gt; edd = new ArrayList&lt;&gt;(asList(&quot; Oldest of the other brother &quot;, &quot; Mostly quiet outside of social situations &quot;, &quot; Cares about saving his money &quot;, &quot; Laughs alot&quot;));
  17. ArrayList&lt;String&gt; frank = new ArrayList&lt;&gt;(asList(&quot; Other brother &quot;, &quot; Impulsively stupid but loyal &quot;, &quot; Really into recreational passions &quot;));
  18. //ArrayList[] fList = new ArrayList[]{{myFamily.get(0),Bio.get()}, };
  19. final Random chance = new Random();
  20. final ArrayList&lt;ArrayList&gt; bio = new ArrayList (Arrays.asList(gio, karina, midget, ma, pa, me, dunks, gus, edd, frank));
  21. //final int selectChance = chance.nextInt(bio.length);
  22. /*
  23. for (String selection : myFamily)
  24. bio
  25. return ;
  26. }
  27. */
  28. ArrayAdapter&lt;String&gt; arrayManager = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_list_item_1, myFamily);
  29. myListView.setAdapter(arrayManager);
  30. myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  31. @Override
  32. public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
  33. Toast.makeText(getApplicationContext(), &quot;Sup &quot; + bio, Toast.LENGTH_LONG).show();
  34. }
  35. });
  36. }
  37. }

答案1

得分: 1

如果我理解正确的话,您可以将以下内容添加到您的 onItemClick 方法中:

  1. @Override
  2. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
  3. {
  4. int randomIndex = chance.nextInt(bio.get(i).size()); // 生成介于 0 和数组大小之间的随机数
  5. String randomString = bio.get(i).get(randomIndex); // 从随机索引处获取值
  6. Toast.makeText(getApplicationContext(),
  7. "嘿 " + randomString,
  8. Toast.LENGTH_LONG
  9. ).show();
  10. }

完整的主活动代码(我只做了一些小的更改,所以上面的解决方案应该适用于您):

  1. import android.os.Bundle;
  2. import android.view.View;
  3. import android.widget.AdapterView;
  4. import android.widget.ArrayAdapter;
  5. import android.widget.ListView;
  6. import android.widget.Toast;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. import java.util.ArrayList;
  9. import java.util.Random;
  10. import static java.util.Arrays.asList;
  11. public class MainActivity extends AppCompatActivity
  12. {
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState)
  15. {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. ListView myListView = findViewById(R.id.myListView);
  19. final ArrayList<String> myFamily = new ArrayList<>(asList("Giovani",
  20. "Enu",
  21. "Xochle",
  22. "Rquel",
  23. "Falsto",
  24. "Bam",
  25. "Duncan",
  26. "Goose",
  27. "Edgar",
  28. "Frank"
  29. ));
  30. ArrayList<String> gio = new ArrayList<>(asList(" 他的另一个兄弟 ",
  31. " 在高中时认识 ",
  32. " 在同一支运动队上比赛 ",
  33. " 有两个孩子 "
  34. ));
  35. // 其他家庭成员的类似列表...
  36. final Random chance = new Random();
  37. final ArrayList<ArrayList<String>> bio = new ArrayList<>(asList(gio,
  38. // 其他家庭成员的类似列表...
  39. ));
  40. ArrayAdapter<String> arrayManager = new ArrayAdapter<>(this,
  41. android.R.layout.simple_list_item_1,
  42. myFamily
  43. );
  44. myListView.setAdapter(arrayManager);
  45. myListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
  46. {
  47. @Override
  48. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
  49. {
  50. int randomIndex = chance.nextInt(bio.get(i).size());
  51. String randomString = bio.get(i).get(randomIndex);
  52. Toast.makeText(getApplicationContext(),
  53. "嘿 " + randomString,
  54. Toast.LENGTH_LONG
  55. ).show();
  56. }
  57. });
  58. }
  59. }
英文:

If I understood You correctly You can Add this to Your onItemClick:

  1. @Override
  2. public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l)
  3. {
  4. int randomIndex = chance.nextInt(bio.get(i).size()); // generate random number between 0 and and array size
  5. String randomString = bio.get(i).get(randomIndex); // take value from random index
  6. Toast.makeText(getApplicationContext(),
  7. &quot;Sup &quot; + randomString,
  8. Toast.LENGTH_LONG
  9. ).show();
  10. }

<hr>

Full code of main activity (I made only small changes so above solution should work for You):

  1. import android.os.Bundle;
  2. import android.view.View;
  3. import android.widget.AdapterView;
  4. import android.widget.ArrayAdapter;
  5. import android.widget.ListView;
  6. import android.widget.Toast;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. import java.util.ArrayList;
  9. import java.util.Random;
  10. import static java.util.Arrays.asList;
  11. public class MainActivity extends AppCompatActivity
  12. {
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState)
  15. {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. ListView myListView = findViewById(R.id.myListView);
  19. final ArrayList&lt;String&gt; myFamily = new ArrayList&lt;&gt;(asList(&quot;Giovani&quot;,
  20. &quot;Enu&quot;,
  21. &quot;Xochle&quot;,
  22. &quot;Rquel&quot;,
  23. &quot;Falsto&quot;,
  24. &quot;Bam&quot;,
  25. &quot;Duncan&quot;,
  26. &quot;Goose&quot;,
  27. &quot;Edgar&quot;,
  28. &quot;Frank&quot;
  29. ));
  30. ArrayList&lt;String&gt; gio = new ArrayList&lt;&gt;(asList(&quot; His other brother &quot;,
  31. &quot; Met in high school &quot;,
  32. &quot; Played on the same sports team &quot;,
  33. &quot; Has two kids &quot;
  34. ));
  35. ArrayList&lt;String&gt; karina = new ArrayList&lt;&gt;(asList(&quot; His youngest sister &quot;,
  36. &quot; Kinda short &quot;,
  37. &quot; Sweet but kinda dumb &quot;
  38. ));
  39. ArrayList&lt;String&gt; midget = new ArrayList&lt;&gt;(asList(&quot; His Second youngest sister &quot;,
  40. &quot; Kinda short &quot;,
  41. &quot; Kinda a crazy cat lady &quot;,
  42. &quot; Cannot handle her liquor &quot;
  43. ));
  44. ArrayList&lt;String&gt; ma = new ArrayList&lt;&gt;(asList(&quot; His other mother &quot;,
  45. &quot; Works way too hard &quot;,
  46. &quot; Sweetest lady in the world &quot;
  47. ));
  48. ArrayList&lt;String&gt; pa = new ArrayList&lt;&gt;(asList(&quot; His other father &quot;,
  49. &quot; Not feeling well &quot;,
  50. &quot; Good heart hopefully he can live in peace &quot;
  51. ));
  52. ArrayList&lt;String&gt; me = new ArrayList&lt;&gt;(asList(&quot; The coolest &quot;,
  53. &quot; Kinda short &quot;,
  54. &quot; Sweet but kinda dumb &quot;
  55. ));
  56. ArrayList&lt;String&gt; dunks = new ArrayList&lt;&gt;(asList(&quot; Brother from another mother &quot;,
  57. &quot; Warrior born &quot;,
  58. &quot; Allergic to most things that could hinder combat &quot;
  59. ));
  60. ArrayList&lt;String&gt; gus = new ArrayList&lt;&gt;(asList(&quot; His other brother &quot;,
  61. &quot; Loves cars &quot;,
  62. &quot; Talks alot of shit &quot;,
  63. &quot; Loves white girls &quot;,
  64. &quot; Memorised the metal gear series by heart&quot;
  65. ));
  66. ArrayList&lt;String&gt; edd = new ArrayList&lt;&gt;(asList(&quot; Oldest of the other brother &quot;,
  67. &quot; Mostly quiet outside of social situations &quot;,
  68. &quot; Cares about saving his money &quot;,
  69. &quot; Laughs alot&quot;
  70. ));
  71. ArrayList&lt;String&gt; frank = new ArrayList&lt;&gt;(asList(&quot; Other brother &quot;,
  72. &quot; Impulsively stupid but loyal &quot;,
  73. &quot; Really into recreational passions &quot;
  74. ));
  75. final Random chance = new Random();
  76. final ArrayList&lt;ArrayList&lt;String&gt;&gt; bio = new ArrayList&lt;&gt;(asList(gio,
  77. karina,
  78. midget,
  79. ma,
  80. pa,
  81. me,
  82. dunks,
  83. gus,
  84. edd,
  85. frank
  86. ));
  87. ArrayAdapter&lt;String&gt; arrayManager = new ArrayAdapter&lt;&gt;(this,
  88. android.R.layout.simple_list_item_1,
  89. myFamily
  90. );
  91. myListView.setAdapter(arrayManager);
  92. myListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
  93. {
  94. @Override
  95. public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l)
  96. {
  97. int randomIndex = chance.nextInt(bio.get(i).size());
  98. String randomString = bio.get(i).get(randomIndex);
  99. Toast.makeText(getApplicationContext(),
  100. &quot;Sup &quot; + randomString,
  101. Toast.LENGTH_LONG
  102. ).show();
  103. }
  104. });
  105. }
  106. }

huangapple
  • 本文由 发表于 2020年9月12日 00:06:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63850597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定