【问题标题】:Using ArrayList of ArrayList to print random Toast message in AndroidAndroid中使用ArrayList的ArrayList打印随机Toast消息
【发布时间】:2020-09-11 16:06:52
【问题描述】:

我正在尝试处理一些 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 "));

        //ArrayList[] fList = new ArrayList[]{{myFamily.get(0),Bio.get()}, };

        final Random chance = new Random();

        final ArrayList<ArrayList> 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<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();
            }
        });
    }
}

【问题讨论】:

  • 所以您有一个列表视图,其中包含 GiovaniEnu 之类的名称,当您单击该项目时,您想从列表中随机发送消息,其中包含该名称的句子,例如对于Giovani 你想打印来自gio 的随机字符串吗?我说的对吗?
  • @iknow 是的,这就是我的目标
  • 所以看看我的回答,我认为它可以满足您的需求。如果不告诉我,否则您可以投票并接受:D

标签: java android string arraylist random


【解决方案1】:

如果我对您的理解正确,您可以将此添加到您的onItemClick

@Override
public void onItemClick(AdapterView<?> 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(),
                   "Sup " + 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(" 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<String>> bio = new ArrayList<>(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)
            {
                int randomIndex = chance.nextInt(bio.get(i).size());
                String randomString = bio.get(i).get(randomIndex);
                Toast.makeText(getApplicationContext(),
                               "Sup " + randomString,
                               Toast.LENGTH_LONG
                ).show();
            }
        });
    }
}

【讨论】:

  • 非常感谢你无法相信这让我多么头疼!!!
  • @BernardMesh 不客气 :D 所以我认为我的回答很有用,要接受它,请点击投票下方的“检查标志”。 See here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-16
  • 2014-05-05
  • 2021-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多