2017-03-15 1 views
0

Ich lese Strings aus einer XML-Datei (über einen PullParser) in einen AlertDialog. Ich muss mehrzeiligen Text im AlertDialog anzeigen können. Die normale Empfehlung (here zum Beispiel) besteht darin, \ n für Newline zu verwenden. Another thread sagt, dass das "hätte funktionieren sollen". Nun, es tat es nicht. n erzeugt keine neue Zeile in AlertDialog

Hier ist der Code für den Alertdialog, wo completionString die Zeichenfolge aus dem XML genommen sind:

public void gameComplete() 
{ 
    AlertDialog alert = new AlertDialog.Builder(this).create(); 

    myTimer.cancel(); 
    myTimer = null; 

    alert.setMessage(completionString); 
    alert.setCancelable(true); 
    alert.setCanceledOnTouchOutside(false); 
    alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) 
     { 
      finish(); 
     } 
    }); 
    alert.show(); 
} 

Aber hier ist, was die eigentliche Screenshot wie folgt aussieht:

enter image description here

Ob ich eine verwenden oder zwei Schrägstriche in der Zeichenkette (\ n oder \\ n) in der Zeichenkette, der AlertDialog nimmt es wörtlich und druckt genau diese Zeichen, nicht eine neue Zeile. Wie kann ich eine tatsächliche neue Zeile anzeigen?

Falls das Problem mit dem Pull-Parser ist, hier ist der Code, der diese Zeichenfolge aus der XML-Datei zieht und in die completionString Variable:

private void parseXml() 
{ 
    // Post1: tuningVars contains the values in the XML tuning file corresponding to the difficulty settings for this particular game. 
    // Post2: completion, win, bonus, fail strings are filled out from the endgamestrings.xml file, 
    // matching the digit specified by the QR/input code. 
    XmlPullParser theParser = Xml.newPullParser(); 

    try { 
     // Post1 

     // Specify the XML file to be parsed. 
     InputStream theStream = getApplicationContext().getAssets().open("tuning.xml"); 
     theParser.setInput(theStream, null); 

     // Parse! 
     int eventType = theParser.getEventType(); 
     String gameTitle = ""; 
     String tagName; 
     int[] difficultyNums = new int[5]; 

     while (eventType != XmlPullParser.END_DOCUMENT) { 
      if (eventType == XmlPullParser.START_TAG) { 
       tagName = theParser.getName(); 
       Log.i("XML Parser", "Got a tag called " + tagName); 

       if (tagName.equals("game")) { 
        gameTitle = theParser.getAttributeValue(null, "title"); 
        Log.i("XML Parser", "Found a game title tag " + gameTitle + " and this game is " + extras.getString(GAME_NAME)); 
       } 

       if (gameTitle.equals(extras.getString(GAME_NAME))) { 
        Log.i("XML Parser", "Adding variables for " + gameTitle); 

        while (eventType != XmlPullParser.END_TAG) { 
         String nextTagName = ""; 

         if (theParser.getName() != null) { 
          nextTagName = theParser.getName(); 
         } 
         Log.i("XML Parser", "Next tag name is" + nextTagName); 
         if (nextTagName.equals("gamevar")) { 
          String varName = theParser.getAttributeValue(null, "varname"); 
          difficultyNums[0] = Integer.parseInt(theParser.getAttributeValue(null, "easyval")); 
          difficultyNums[1] = Integer.parseInt(theParser.getAttributeValue(null, "mediumval")); 
          difficultyNums[2] = Integer.parseInt(theParser.getAttributeValue(null, "hardval")); 
          difficultyNums[3] = Integer.parseInt(theParser.getAttributeValue(null, "hackerinc")); 
          difficultyNums[4] = Integer.parseInt(theParser.getAttributeValue(null, "languageinc")); 

          // then construct the Tuning variable and add it to the array 
          TuningVariable varToAdd = new TuningVariable(varName, difficultyNums); 
          tuningVars.add(varToAdd); 
          Log.i("XML Parser", "Added tuning variable " + varName); 
         } 

         eventType = theParser.next(); 
        } 
       } else { 
        Log.i("XML Parser", "I compared " + extras.getString(GAME_NAME) + " with " + gameTitle + " and decided they were not the same."); 
       } 
      } 
      eventType = theParser.next(); 
     } 

     // Post2 

     // Specify the XML file to be parsed. 
     theStream = getApplicationContext().getAssets().open("endgamestrings.xml"); 
     theParser.setInput(theStream, null); 

     int chosenDigit = extras.getInt(STRINGS_NUM); 

     Log.i("XML Parser", "Running PullParser on endgamestrings."); 

     // Parse! 
     eventType = theParser.getEventType(); 
     int stringNum = -1; 
     String parsedGameType; 
     String selectedGameType; 

     // Define which games are accelerators and which puzzles. 
     // It would probably make more sense to do this via the XML as well, but let's start here for testing. 
     switch (extras.getString((GAME_NAME))) { 
      case "Simon": 
       selectedGameType = "accelerator"; 
       break; 
      case "Square": 
       selectedGameType = "accelerator"; 
       break; 
      case "Pegs": 
       selectedGameType = "puzzle"; 
       break; 
      case "Pipes": 
       selectedGameType = "puzzle"; 
       break; 
      case "Lights": 
       selectedGameType = "puzzle"; 
     } 


     while (eventType != XmlPullParser.END_DOCUMENT) 
     { 
      // Log.i("XML Parser", "Running PullParser while loop on endgamestrings."); 

      if (eventType == XmlPullParser.START_TAG) 
      { 
       tagName = theParser.getName(); 
       // Log.i("XML Parser", "Got a tag called " + tagName); 

       if (tagName.equals("setofstrings")) 
       { 
        stringNum = Integer.parseInt(theParser.getAttributeValue(null, "id")); 
        parsedGameType = theParser.getAttributeValue(null, "type"); 
       } 

       if (stringNum == chosenDigit) 
       { 
        /* 
        // Java is whining like a bitch about these variables not being initialized. 
        // Won't bother with this particular error-catching for now. 
        if (parsedGameType.equals("puzzle") && selectedGameType.equals("accelerator")) 
        { 
         String warning = "Warning: Accelerator strings passed to puzzle. Setting strings to default."; 
         Toast toast = Toast.makeText(GameActivity.this, warning, 10); 
         toast.show(); 
        } 
        */ 

        while (eventType != XmlPullParser.END_TAG) 
        { 
         String nextTagName = ""; 
         String stringType = ""; 

         if (theParser.getName() != null) 
         { 
          nextTagName = theParser.getName(); 
         } 

         if (nextTagName.equals("string")) 
         { 
          stringType = theParser.getAttributeValue(null, "type"); 
          Log.i("XML Parser", "Found a string type tag " + stringType); 
         } 

         switch (stringType) 
         { 
          case ("time"): 
           completionString = theParser.nextText(); 
           break; 
          case ("bonus"): 
           bonusString = theParser.nextText(); 
           break; 
          case ("win"): 
           winString = theParser.nextText(); 
           break; 
          case ("fail"): 
           failString = theParser.nextText(); 
           break; 
         } 

         eventType = theParser.next(); 
        } 
       } 
      } 
      eventType = theParser.next(); 
     } 
    } 
    catch (XmlPullParserException e) 
    { 
     Log.e("XML Parser", "Dude! XmlPullParserException!"); 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     Log.e("XML Parser", "Dude! IOException!"); 
     e.printStackTrace(); 
    } 

    return; 

} 

Antwort

2

starten:

alert.setMessage(completionString.replace("\\n", "\n")); 
Verwandte Themen