如何使这个代码更清洁?

时间:2012-07-06 12:04:03

标签: java android string

我正在进行2项活动。一个活动有一个textView和一个按钮。 textView显示了爱好,如果用户点击按钮,将会设置爱好。当用户点击该按钮时,他/她将转到第二个活动,该活动具有与爱好相对应的4个复选框。当用户从菜单中单击“完成”时,他/她将返回到第一个活动,之后,检查的爱好将显示在文本视图中,以逗号(“,”)分隔。我的问题是,当用户再次使用按钮设置爱好但已经在textView中显示爱好时,应根据textView中显示的内容初始化第二个Activity中的复选框。我已经尝试过数组,甚至是硬编码,但我陷入了逻辑上。这是我的代码。假设所有视图都已初始化:

第一屏(重要位):

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

    }

代码仍未按预期运行。正如您所看到的,我使用了字符串拆分和字符串数组,但由于极端硬编码和数组索引边界问题,使用数组使其难以处理。

3 个答案:

答案 0 :(得分: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;
}

对于第二部分,我将使用以下代码

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

答案 1 :(得分:0)

您可能希望使用Preferences实现此功能。让您的第二个Activity成为PreferenceActivity,并选中复选框CheckBoxPreferences。当用户选中/取消选中它们时,值会自动保存在SharedPreferences中。然后,当您返回到第一个Activity时,您只需使用保存在SharedPreferences中的布尔值来显示值。您可以在Preferenceshere's 上获取有关SharedPreferences的精彩帖子的google,这也很有用。希望这会有所帮助。

答案 2 :(得分: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 ...