2017-05-26 71 views
-4

我有兩個項目,每個有3個單詞的任何數組列表:用戶名,積分和高分(例如:瑞安120中)。我希望能夠從數組列表項中分離出這些單詞,並將每個單詞顯示在android studio中的文本視圖中。我將如何去做這件事。我已經寫了一些代碼,但我不知道該去哪裏?我知道它與for循環有關。我正在使用計數來表示從數組列表項中分離出單獨的單詞。如何分隔數組列表項中的單詞?

public void setHighScore() { 
    List<String> lines = new ArrayList<>(); 
    lines.add("ryan 150 medium"); 
    lines.add("andrew 200 medium") 
    int count = 0; 
    int count2 = 0; 
    while(count < lines.size()) { 
     for() { 
      if(count2 == 0) { 
       count2++; 
      } 
      else if(count2 == 1) { 
       count2++; 
      } 
      else if(count2 == 2) { 
       count2++; 
      } 
     } 
    } 
} 
+0

你能提供的輸出應該是什麼樣子的樣本? – bradimus

+0

https://stackoverflow.com/help/how-to-ask – mmaceachran

+0

而你被低估了4次,因爲它在谷歌上花了不到10秒鐘才找到答案。 SO不是谷歌。 – mmaceachran

回答

4

use Split();

例如:

lines.add("ryan 150 medium"); 
lines.add("andrew 200 medium") 

String[] words = lines.get(0).split(" "); 
words[0] // ryan 
words[1] // 150 
words[2] // medium 

String[] words2 = lines.get(1).split(" "); 
words2[0] // andrew 
words2[1] // 200 
words2[2] // medium 



TextView viewsName = (TextView)findViewById(R.id.name); 
viewsName.setText(words[0]); // ryan 

TextView viewsScore = (TextView)findViewById(R.id.score); 
viewsScore.setText(words[1]); // 150 

TextView viewsLevel = (TextView)findViewById(R.id.level); 
viewsLevel.setText(words[2]); // medium 
+0

如果我想盡快將它們加載到文本視圖而不創建新的數組列表,那麼會有什麼辦法呢? –

+0

你想動態創建textview或只是將其設置爲現有的textview? – ZeroOne

+0

將其設置爲我已創建的現有文本視圖。我也有每個textview的id。 –

0

您可以使用foreach循環列表,然後你需要給它的任何行動。

public void setHighScore(){ 

    List<String> lines = new ArrayList<>(); 
    lines.add("ryan 150 medium"); 
    lines.add("andrew 200 medium"); 
    lines.forEach(line -> { 
     String[] temp = line.split(" "); 
     // Whatever you want to do for the line. 
    }); 
} 
0
public void setHighScore() { 
    List<String[]> listStringArray = new ArrayList<>(); 
    List<String> lines = new ArrayList<>(); 
    lines.add("ryan 150 medium"); 
    lines.add("andrew 200 medium") 
    for(int i = 0; i < lines.size(); i++){ 
     String[] words = lines.get(i).split(" "); 
     listStringArray.add(words); 
    } 
} 

您將得到String數組列表中的數據。

現在,您可以將數據集這樣的觀點:

TextView textView = (TextView) findViewById(R.id.text); 
textView.setText(listStringArray.get(i)[0]); 
0

標準 JAVA的方法來解決這個問題是定義一個class其中包含不同的屬性。然後使用它。

Person.class

public class Person { 

    String name, highscore; 
    int points; 

    public Person(String name, int points, String highscore) { 
     this.name = name; 
     this.points = points; 
     this.highscore = highscore; 
    } 
    public String getName() { 
     return name; 
    } 
    public int getPoints() { 
     return points; 
    }   
    public String getHighscore() { 
     return highscore; 
    }  
} 

現在使用這個類在你的名單如下:

List<Person> lines = new ArrayList<>(); 
lines.add(new Person("ryan", 150, "medium")); 
lines.add(new Person("andrew", 200,"medium")); 
//Accress them as follows: 
lines.get(0).getName(); //this will return ryan 
lines.get(1).getPoints(); //this will return 200 
相關問題