英文:
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<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();
}
});
}
}
答案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<?> 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();
}
<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<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();
}
});
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论