如何在祝酒词中显示我的数组列表.. 这是我的代码,但它没有显示数组中的完整项目。我的问题是什么?
public static EditText txt1;
String ag;
public static ArrayList<String> playerList = new ArrayList<String>();
String playerlist[];
/** Called when the activity is first created.
* @param Public */
public void onCreate(Bundle savedInstanceState, Object Public) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
// edittext1 or textview1
txt1 = (EditText) findViewById(R.id.editText1);
ag = txt1.getText().toString();
//add more items button
Button more = (Button) findViewById(R.id.button1);
more.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (txt1.getText().length() != 0) {
String ag = "Current Added Players:," + txt1.getText().toString() + txt1.getText().toString();
playerList.add(ag);
Toast.makeText(getBaseContext(), ag, Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(view.getContext(), Screen2.class);
myIntent.putExtra("Stringarray", playerList);
//startActivityForResult(myIntent, 0);
}
}
});
}
答案 0 :(得分:2)
如果您的ArrayList是Strings或任何包装类对象,那么您可以按照
进行操作ArrayList<String> a = new ArrayList<String>();
a.add("Hello");
a.add("World"); //similarly any other add statements
Toast.makeText(getBaseContext(), a+"", Toast.LENGTH_SHORT).show();
无需转换为数组或遍历列表,因为当列表是String或包装类对象时,它们的toString()方法将返回值而不是十六进制逻辑地址。
因此,在这种情况下,您将收到一条显示
的Toast消息[Hello,World,...other elements what is stored in the arraylist]
答案 1 :(得分:1)
你可以使用List.toArray(playerList),然后循环遍历列表以制作一个长字符串并祝酒
String[] playerArray = (String[]) playerList.toArray();
String playerToast = playerArray[0];
for(int i=1; i<playerArray.length(); i++){
playerToast += " ";
playerToast += playerArray[i];
}
Toast.makeText(getBaseContext(), playerToast, Toast.LENGTH_SHORT).show();
答案 2 :(得分:0)
您的问题是这行代码
String ag = "Current Added Players:," +txt1.getText().toString() + txt1.getText().toString() ;
每当你按下你设置该监听器的任何按钮时,变量ag
被赋予上述值。您正在使用的Toast已将ag
设置为其消息,因此Toast将始终显示
"Current Added Players:," +txt1.getText().toString() + txt1.getText().toString()
有几种方法可以使用Toast来显示ArrayList的内容:
一种方法是使用for循环并将ArrayList的内容分配到单个String中,尽管这会导致更大的Toast用于更大的ArrayLists。
第二种方法是使用for循环为ArrayList中的每个条目显示Toast消息。
的内容 for(int i, i < playerList.length, i++) {
Toast.makeText(this, playerList[i], Toast.LENGTH_SHORT)
.show();
}
答案 3 :(得分:0)
回答有人遇到同样的问题:在按钮内添加“player = txt1 .....”点击本身不在外面
//add more items button
Button more = (Button) findViewById(R.id.button1);
more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
player = txt1.getText().toString();
if (txt1.getText().toString().length() !=0){
playerList.add(player);
txt1.setText("");
}
Toast.makeText(getBaseContext(), playerList + " ", Toast.LENGTH_LONG).show();
}
});