Java 在toast数组列表中显示

Java 在toast数组列表中显示,java,android,Java,Android,如何在toast中显示数组列表。。 这是我的代码,但它没有显示数组中的完整项。我的问题是什么 public static EditText txt1; String ag; public static ArrayList<String> playerList = new ArrayList<String>(); String playerlist[]; /** Called when the activity is first created. * @param

如何在toast中显示数组列表。。 这是我的代码,但它没有显示数组中的完整项。我的问题是什么

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);
            }
        }
    });
}
公共静态编辑文本txt1;
字符串ag;
public static ArrayList playerList=new ArrayList();
弦乐演奏家名单[];
/**在首次创建活动时调用。
*@param Public*/
创建时的公共void(Bundle savedInstanceState,Object public){
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
//编辑文本1或文本视图1
txt1=(EditText)findViewById(R.id.editText1);
ag=txt1.getText().toString();
//添加更多项目按钮
按钮更多=(按钮)findViewById(R.id.button1);
setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
如果(txt1.getText().length()!=0){
String ag=“当前添加的玩家:,”+txt1.getText().toString()+txt1.getText().toString();
playerList.add(ag);
Toast.makeText(getBaseContext(),ag,Toast.LENGTH_SHORT).show();
Intent myIntent=newintent(view.getContext(),Screen2.class);
putExtra(“Stringarray”,playerList);
//startActivityForResult(myIntent,0);
}
}
});
}
您可以使用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();
String[]playerArray=(String[])playerList.toArray();
字符串playerToast=playerArray[0];
对于(inti=1;i,您可以使用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();
String[]playerArray=(String[])playerList.toArray();
字符串playerToast=playerArray[0];

对于(inti=1;i您的问题是这行代码

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的内容分配到单个字符串中,尽管这会导致较大ArrayList的Toast非常大

第二种方法是使用for循环为ArrayList中的每个条目显示Toast消息

 for(int i, i < playerList.length, i++) {
      Toast.makeText(this, playerList[i], Toast.LENGTH_SHORT)
           .show();
 }
for(int i,i
您的问题是这行代码

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的内容分配到单个字符串中,尽管这会导致较大ArrayList的Toast非常大

第二种方法是使用for循环为ArrayList中的每个条目显示Toast消息

 for(int i, i < playerList.length, i++) {
      Toast.makeText(this, playerList[i], Toast.LENGTH_SHORT)
           .show();
 }
for(int i,i
如果您的ArrayList由字符串或任何包装类对象组成,那么您可以遵循以下步骤

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();

如果您的ArrayList由字符串或任何包装类对象组成,那么您可以遵循以下步骤

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();

如果有人遇到同样的问题,请回答:在按钮内部添加“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();

        }

    });

如果有人遇到同样的问题,请回答:在按钮内部添加“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();

        }

    });

为什么需要转换为数组?
String playerToast=playerList.get(0);for(int i=1;i
1。Sergey有一个很好的观点,您不需要使用
list.get(i)将列表转换为数组
虽然我会使用字符串数组以2开头。昨天清除EditText框
txt1.setText(“”)后,您的代码发生了什么变化
每次您按下按钮并添加到
播放列表时
.3.Yawus是对的,在烤面包片上循环比非常长的烤面包片在视觉上更美观,这对于(int i=0;i
你让它工作了吗?如果有帮助的话,你能选择一个答案吗?很抱歉,我现在看到了这个…我现在正在处理它,并将把它所说的代码上传到上面。谢谢你,所有的祝酒词都显示为空白,所以现在我不知道我哪里出错了。`//添加更多项目按钮按钮按钮更多=(按钮)findViewById(R.id.button1);more.setOnClickListener(new View.OnClickListener(){public void onClick(View View){if(txt1.getText().length()!=0){playerList.add(ag);for(int i=0;iString playerToast=playerList.get(0);for(int i=1;i
1.Sergey有一个很好的观点,您不需要使用
list.get(i)
将列表转换为数组,尽管我会使用字符串数组作为开头2.昨天您清除了EditText框
txt1.setText(“”)后,您的代码发生了什么变化
每次按下按钮并添加到
播放列表时
.3.Yawus