2つの活動をしています。1 つのアクティビティには、textView とボタンがあります。textView には、ユーザーがボタンをクリックすると設定される趣味が表示されます。ユーザーがボタンをクリックすると、趣味に対応する 4 つのチェックボックスがある 2 番目のアクティビティに移動します。ユーザーがメニューから [完了] をクリックすると、最初のアクティビティに戻ります。その後、チェックした趣味がコンマ (",") で区切られてテキストビューに表示されます。私の問題は、ユーザーが再びボタンを使用して趣味を設定したときに、すでに textView に趣味が表示されている場合、textView に表示されている内容に従って、2 番目のアクティビティのチェックボックスを初期化する必要があることです。ハードコーディングも含めて配列を試しましたが、論理的なわだち掘れで立ち往生しています。これが私のコードです。すべてのビューが初期化されていると仮定します。
最初の画面 (重要なビット):
@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);
}
}
}
コードはまだ期待どおりに実行されません。ご覧のとおり、文字列分割と文字列配列を使用しましたが、極端なハードコーディングと配列インデックスの境界の問題により、配列を使用すると処理が難しくなります。