我正在从事 2 项活动。一个活动有一个 textView 和一个按钮。textView 显示爱好,如果用户单击按钮,则会设置该爱好。当用户单击按钮时,他/她将转到第二个活动,该活动有 4 个与爱好对应的复选框。当用户从菜单中单击完成时,他/她将返回到第一个活动,之后,选中的爱好将显示在文本视图中,以逗号(“,”)分隔。我的问题是,当用户再次使用按钮设置爱好,但已经在 textView 中显示爱好时,第二个 Activity 中的复选框应根据 textView 中显示的内容进行初始化。我已经尝试过数组,甚至是硬编码,但我陷入了逻辑困境。这是我的代码。假设所有视图都已初始化:
第一个屏幕(重要位):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case SET_BIRTHDAY:
TextView textBirthday = (TextView) findViewById(R.id.tvBirthdayStat);
birthdayString = data.getStringExtra(Lab2_082588birthday.MONTH)
+ "/" + data.getStringExtra(Lab2_082588birthday.DAY)
+ "/" + data.getStringExtra(Lab2_082588birthday.YEAR);
textBirthday.setText(birthdayString);
break;
case SET_HOBBIES:
TextView textHobbies = (TextView) findViewById(R.id.tvHobbiesStat);
anime = data.getStringExtra(Lab2_082588hobbies.ANIME);
games = data.getStringExtra(Lab2_082588hobbies.GAMES);
movies = data.getStringExtra(Lab2_082588hobbies.MOVIES);
books = data.getStringExtra(Lab2_082588hobbies.BOOKS);
checkNull();
textHobbies.setText(hobbiesString);
break;
}
}
}
private void checkNull() {
// TODO Auto-generated method stub
if (games == null) {
hobbiesString = books + " , " + movies + " , " + anime;
}
if (anime == null) {
hobbiesString = games + " , " + books + " , " + movies;
}
if (movies == null) {
hobbiesString = games + " , " + books + " , " + anime;
}
if (books == null) {
hobbiesString = games + " , " + movies + " , " + anime;
}
if ((games == null) && (anime == null)) {
hobbiesString = books + " , " + movies;
}
if ((games == null) && (movies == null)) {
hobbiesString = books + " , " + anime;
}
if ((games == null) && (books == null)) {
hobbiesString = movies + " , " + anime;
}
if ((anime == null) && (movies == null)) {
hobbiesString = games + " , " + books;
}
if ((anime == null) && (books == null)) {
hobbiesString = games + " , " + movies;
}
if ((movies == null) && (books == null)) {
hobbiesString = games + " , " + anime;
}
if ((movies == null) && (books == null) && (anime == null)) {
hobbiesString = games;
}
if ((movies == null) && (games == null) && (anime == null)) {
hobbiesString = books;
}
if ((games == null) && (books == null) && (anime == null)) {
hobbiesString = movies;
}
if ((movies == null) && (books == null) && (games == null)) {
hobbiesString = anime;
}
}
第二屏:
private void startUp() {
// TODO Auto-generated method stub
Intent startUp = getIntent();
String receivedString = startUp.getStringExtra(HOBBIES_STRING);
if (receivedString != null) {
String[] separated = receivedString.split(",");
if (separated.length > 0) {
if (separated[0].equals("Anime")) {
anime.setChecked(true);
}
/*----------------*/
if (separated[0].equals("Computer Games")) {
games.setChecked(true);
}
/*----------------*/
if (separated[0].equals("Books")) {
books.setChecked(true);
}
/*----------------*/
if (separated[0].equals("Movies")) {
movies.setChecked(true);
}
/*----------------*/
if (separated[0].equals("Anime")&& separated[0].equals("Computer Games")) {
anime.setChecked(true);
games.setChecked(true);
}
if (separated[0].equals("Anime")&&separated[0].equals("Books")) {
anime.setChecked(true);
books.setChecked(true);
}
if (separated[0].equals("Anime")&&separated[0].equals("Movies")) {
anime.setChecked(true);
movies.setChecked(true);
}
}
}
代码仍然没有按预期运行。如您所见,我使用了字符串拆分和字符串数组,但是由于极端的硬编码和数组索引边界问题,使用数组使其难以处理。