2017-05-26 159 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 
相关问题