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

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

Using ArrayList of ArrayList to print random Toast message in Android

问题

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView myListView = findViewById(R.id.myListView);

        final ArrayList<String> myFamily = new ArrayList<>(asList("Giovani", "Enu", "Xochle", "Rquel", "Falsto", "Bam", "Duncan", "Goose", "Edgar", "Frank"));

        ArrayList<String> gio = new ArrayList<>(asList(" His other brother ", " Met in high school ", " Played on the same sports team ", " Has two kids "));
        ArrayList<String> karina = new ArrayList<>(asList(" His youngest sister ", " Kinda short ", " Sweet but kinda dumb "));
        ArrayList<String> midget = new ArrayList<>(asList(" His Second youngest sister ", " Kinda short ", " Kinda a crazy cat lady ", " Cannot handle her liquor "));
        ArrayList<String> ma = new ArrayList<>(asList(" His other mother ", " Works way too hard ", " Sweetest lady in the world "));
        ArrayList<String> pa = new ArrayList<>(asList(" His other father ", " Not feeling well ", " Good heart hopefully he can live in peace "));
        ArrayList<String> me = new ArrayList<>(asList(" The coolest ", " Kinda short ", " Sweet but kinda dumb "));
        ArrayList<String> dunks = new ArrayList<>(asList(" Brother from another mother ", " Warrior born ", " Allergic to most things that could hinder combat "));
        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"));
        ArrayList<String> edd = new ArrayList<>(asList(" Oldest of the other brother ", " Mostly quiet outside of social situations ", " Cares about saving his money ", " Laughs alot"));
        ArrayList<String> frank = new ArrayList<>(asList(" Other brother ", " Impulsively stupid but loyal ", " Really into recreational passions "));

        final Random chance = new Random();

        final ArrayList<ArrayList> bio = new ArrayList(Arrays.asList(gio, karina, midget, ma, pa, me, dunks, gus, edd, frank));

        ArrayAdapter<String> arrayManager = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, myFamily);

        myListView.setAdapter(arrayManager);

        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(), "Sup " + bio, Toast.LENGTH_LONG).show();
            }
        });
    }
}

希望这对你有所帮助。

英文:

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.

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myListView = findViewById(R.id.myListView);
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;));
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;));
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;));
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;));
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;));
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;));
ArrayList&lt;String&gt; me = new ArrayList&lt;&gt;(asList(&quot; The coolest &quot;, &quot; Kinda short &quot;, &quot; Sweet but kinda dumb &quot;));
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;));
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;));
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;));
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;));
//ArrayList[] fList = new ArrayList[]{{myFamily.get(0),Bio.get()}, };
final Random chance = new Random();
final ArrayList&lt;ArrayList&gt; bio = new ArrayList (Arrays.asList(gio, karina, midget, ma, pa, me, dunks, gus, edd, frank));
//final int selectChance = chance.nextInt(bio.length);
/*
for (String selection : myFamily)
bio
return ;
}
*/
ArrayAdapter&lt;String&gt; arrayManager = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_list_item_1, myFamily);
myListView.setAdapter(arrayManager);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), &quot;Sup &quot; + bio, Toast.LENGTH_LONG).show();
}
});
}
}

答案1

得分: 1

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

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

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

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.Random;

import static java.util.Arrays.asList;

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView myListView = findViewById(R.id.myListView);

        final ArrayList<String> myFamily = new ArrayList<>(asList("Giovani",
                                                                  "Enu",
                                                                  "Xochle",
                                                                  "Rquel",
                                                                  "Falsto",
                                                                  "Bam",
                                                                  "Duncan",
                                                                  "Goose",
                                                                  "Edgar",
                                                                  "Frank"
        ));

        ArrayList<String> gio = new ArrayList<>(asList(" 他的另一个兄弟 ",
                                                       " 在高中时认识 ",
                                                       " 在同一支运动队上比赛 ",
                                                       " 有两个孩子 "
        ));
        // 其他家庭成员的类似列表...

        final Random chance = new Random();

        final ArrayList<ArrayList<String>> bio = new ArrayList<>(asList(gio,
                                                                        // 其他家庭成员的类似列表...
        ));

        ArrayAdapter<String> arrayManager = new ArrayAdapter<>(this,
                                                               android.R.layout.simple_list_item_1,
                                                               myFamily
        );

        myListView.setAdapter(arrayManager);

        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
            {
                int randomIndex = chance.nextInt(bio.get(i).size());
                String randomString = bio.get(i).get(randomIndex);
                Toast.makeText(getApplicationContext(),
                               "嘿 " + randomString,
                               Toast.LENGTH_LONG
                ).show();
            }
        });
    }
}
英文:

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

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

<hr>

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

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Random;
import static java.util.Arrays.asList;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myListView = findViewById(R.id.myListView);
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;
));
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;
));
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;
));
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;
));
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;
));
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;
));
ArrayList&lt;String&gt; me = new ArrayList&lt;&gt;(asList(&quot; The coolest &quot;,
&quot; Kinda short &quot;,
&quot; Sweet but kinda dumb &quot;
));
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;
));
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;
));
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;
));
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;
));
final Random chance = new Random();
final ArrayList&lt;ArrayList&lt;String&gt;&gt; bio = new ArrayList&lt;&gt;(asList(gio,
karina,
midget,
ma,
pa,
me,
dunks,
gus,
edd,
frank
));
ArrayAdapter&lt;String&gt; arrayManager = new ArrayAdapter&lt;&gt;(this,
android.R.layout.simple_list_item_1,
myFamily
);
myListView.setAdapter(arrayManager);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l)
{
int randomIndex = chance.nextInt(bio.get(i).size());
String randomString = bio.get(i).get(randomIndex);
Toast.makeText(getApplicationContext(),
&quot;Sup &quot; + randomString,
Toast.LENGTH_LONG
).show();
}
});
}
}

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:

确定