Java Studio更改数组值第2部分
我问如果用户选择一个动作而不是另一个动作,如何删除数组中的一个元素。Java Studio更改数组值第2部分,java,android,arrays,arraylist,random,Java,Android,Arrays,Arraylist,Random,我问如果用户选择一个动作而不是另一个动作,如何删除数组中的一个元素。 我得到了一个关于将字符串数组设置为ArrayList的答案 现在,我想知道如何为数据指定id值,以便显示该值上的下一个子项。 例如,如果用户按yes to Greece,我想显示希腊的城市 int randome=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
我得到了一个关于将字符串数组设置为ArrayList的答案 现在,我想知道如何为数据指定id值,以便显示该值上的下一个子项。
例如,如果用户按yes to Greece,我想显示希腊的城市
int randome=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button no = (Button) findViewById(R.id.No);
final Button yes=(Button) findViewById(R.id.Yes);
final TextView tvMessage=(TextView) findViewById(R.id.tvMessage);
final ArrayList<String> country = new ArrayList<>();
country.add("Do you wanna see an Action Movie???");
country.add("Do you wanna see a Comedy Movie???");
country.add("Do you wanna see a Drama Movie???");
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (randome>=0 && randome<country.size()){
country.remove(randome);
Random rand = new Random();
randome = rand.nextInt(country.size());
tvMessage.setText(country.get(randome));
}
}
});
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Random rand = new Random();
randome = rand.nextInt(country.size());
tvMessage.setText(country.get(randome));
}
});
}
如果用户按yes键“你想看喜剧电影吗”,我想以某种方式显示存储在其他阵列或类似阵列上的电影
您可以在单击按钮时检查textview文本等于什么,然后从一些列表/数组中获取相应的数据
final Random rand = new Random();
final TextView tvMessage=(TextView) findViewById(R.id.tvMessage);
// Some prompts to compare against later
final String actionMovie = "Do you wanna see an Action Movie???";
final ArrayList<String> prompts = new ArrayList<>();
prompts.add(actionMovie);
// Your lists
final ArrayList<String> actionMovies = new ArrayList<>();
actionMovies.add("Terminator 2");
// Setup the click event
findViewById(R.id.Yes).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Check which prompt is shown
if (tvMessage.getText().toString().equals(actionMovie)) {
// Get a random movie
String movieTitle = actionMovies.get(rand.nextInt(actionMovies.size()));
} else { } // equals something else
}
final Random rand=new Random();
最终文本视图tvMessage=(文本视图)findViewById(R.id.tvMessage);
//一些提示,以供以后比较
最后一个字符串actionMovie=“你想看动作片吗?”;
最终ArrayList提示=新建ArrayList();
添加(动作电影);
//你的名单
最终ArrayList actionMovies=新ArrayList();
动作片。添加(“终结者2”);
//设置单击事件
findviewbyd(R.id.Yes).setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
//检查显示的提示
if(tvMessage.getText().toString().equals(actionMovie)){
//随机拍一部电影
String movieTitle=actionMovies.get(rand.nextInt(actionMovies.size());
}else{}//等于别的东西
}
不清楚你在问什么。但是,如果你想要有ID的国家/城市,不要将自己限制在字符串中。创建一个Country
类,它包含一个列表城市
,或者一些东西。如果你在android上运行代码,你会得到一个文本视图,它会说欢迎(默认布局是欢迎)有两个按钮,是和否。如果你在欢迎文本视图上按是,兰德将回答来自国家的问题(国家。添加(“你想看动作电影吗?”);国家。添加(“你想看喜剧电影吗?”);国家。添加(“你想看戏剧电影吗?”);)我想要的是:如果用户按yes“你想看一部Commedy电影吗?”我想以某种方式显示一部存储在其他阵列或类似阵列上的电影。这就是我想要理解的,谢谢你宝贵的回答。
final Random rand = new Random();
final TextView tvMessage=(TextView) findViewById(R.id.tvMessage);
// Some prompts to compare against later
final String actionMovie = "Do you wanna see an Action Movie???";
final ArrayList<String> prompts = new ArrayList<>();
prompts.add(actionMovie);
// Your lists
final ArrayList<String> actionMovies = new ArrayList<>();
actionMovies.add("Terminator 2");
// Setup the click event
findViewById(R.id.Yes).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Check which prompt is shown
if (tvMessage.getText().toString().equals(actionMovie)) {
// Get a random movie
String movieTitle = actionMovies.get(rand.nextInt(actionMovies.size()));
} else { } // equals something else
}