0

我正在从事 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);
            }
        }

    }

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

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

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

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. 让你的第二个Activity是 aPreferenceActivity和复选框 - 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 回答