java - 如何使这段代码更简洁?

标签 java android string

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

第一个屏幕(重要的一点):

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

    }

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

最佳答案

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

关于java - 如何使这段代码更简洁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11361854/

相关文章:

c++ - 字符串文字之间的比较

java - 从逗号分隔的字符串中删除尾随逗号

java - 更改 JTable 中单元格的背景颜色

java - 比较器类中的错误

android - 同时在 editText 中提示和文本

android - 有什么事吗? [尝试自动化 android 构建]

perl 相等的字符串即使相等也返回 0

java - Netty 服务器停止响应

java - php 中的 AES/CBC/PKCS5Padding java 等价物是什么?

android - 来自 Google Play 的位置服务在模式 "Device Only"下不起作用