【发布时间】:2017-03-06 04:34:11
【问题描述】:
您好,我想知道是否有一种方法可以获取我的数组中的一个元素并根据用户的回答将其删除。例如,在我的代码中,我有一个国家数组和一个显示国家/地区的随机生成器到用户的文本视图。如果用户按下是按钮,则生成器运行。如果用户按下否按钮,到目前为止什么都没有发生,但想法是删除正在显示的数组元素,生成器将再次运行,但是这样元素已删除。`
@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);
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String[] country = new String[10];
country[0] = "greece";
country[1] = "germany";
country[2] = "america";
country[3] = "serbia";
country[4] = "france";
country[5] = "england";
country[6] = "cyprus";
country[7] = "japan";
country[8] = "russia";
country[9] = "china";
Random rand = new Random();
int randome = rand.nextInt(10);
tvMessage.setText(country[randome]);
}
});
}
【问题讨论】: