0

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);
            }
        }

    }

コードはまだ期待どおりに実行されません。ご覧のとおり、文字列分割と文字列配列を使用しましたが、極端なハードコーディングと配列インデックスの境界の問題により、配列を使用すると処理が難しくなります。

4

3 に答える 3

2

CheckNull メソッドの場合、次を使用します

private void checkNull() {  
hobbiesstring = "";
if (games != null) 
  hobbiesstring += games;
if (anime != null)
  hobbiesstring += anime;
if (books != null) 
  hobbiesstring += books;
if (movies != null)
  hobbiesstring += movies;
}

2番目の部分では、次のコードを使用します

private void startUp() {  
Intent startUp = getIntent();  
String receivedString = startUp.getStringExtra(HOBBIES_STRING);  

if (receivedString != null) {  
    String[] separated = receivedString.split(",");  
    foreach(string sep in seperated){
      switch(sep){
        case "Anime":
          anime.setChecked(true);
          break;
        case "Movies":
          movies.setChecked(true);
          break;
        case "Books":
          books.setChecked(true);
          break;
        case "Games":
          games.setChecked(true);
          break;
      }
    }
  }
}
于 2012-07-06T12:12:21.663 に答える
0

を使用してこの機能を実装することをお勧めしますPreferences。2 番目Activityを a PreferenceActivity、チェックボックスを - としますCheckBoxPreferences。ユーザーがチェック/チェックを外すと、値は自動的に に保存されSharedPreferencesます。次に、最初に戻ったときに、にActivity保存されているブール値を使用して値を表示するだけですSharedPreferences。チュートリアルについてはグーグルで検索できますPreferencesこれSharedPreferencesも役立つ素晴らしい投稿です。お役に立てれば。

于 2012-07-06T12:13:22.177 に答える
0

,' ' を StringTokenizerに渡そうとしましたか?

それほど重要ではありませんが、このように最適化できます

   if (games == null) {
            hobbiesString = books + " , " + movies + " , " + anime;

            if (books == null) {
                hobbiesString = movies + " , " + anime;

                 if (anime == null) {
                       hobbiesString = movies;
                 }

            }
    } else if(books == null) {
           ...
    } else ...
于 2012-07-06T12:14:18.410 に答える