Reputation: 79
How do i display my array list in a toast.. This is the code i have, but it's not displaying the full items in the array..what is my problem?
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);
}
}
});
}
Upvotes: 0
Views: 7170
Reputation: 79
Answer incase someone runs into the same problem: add the the "player= txt1....." inside the button click itself not outside
//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();
}
});
Upvotes: 0
Reputation: 19502
If your ArrayList is of Strings or of any wrapper class objects, then you can follow this
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();
No need to convert to array or iterate through the list, because when the list is of String or wrapper class objects, then their toString() methods will return the value instead of hexadecimal logical address.
So in this case you will get a toast message showing
[Hello,World,...other elements what is stored in the arraylist]
Upvotes: 2
Reputation: 1822
Your problem is this line of code
String ag = "Current Added Players:," +txt1.getText().toString() + txt1.getText().toString() ;
Every time you push whatever button you've set that listener to, the variable ag
is assigned the above value. The Toast you're using has ag
set as its message so the Toast will always display
"Current Added Players:," +txt1.getText().toString() + txt1.getText().toString()
There are several ways to use Toast to display the contents of your ArrayList:
One way is to use a for loop and assign the contents of your ArrayList into a single String, although this will result in an enormously large Toast for larger ArrayLists.
The second way is to use a for loop to display a Toast message for each entry in your ArrayList. Something along the lines of
for(int i, i < playerList.length, i++) {
Toast.makeText(this, playerList[i], Toast.LENGTH_SHORT)
.show();
}
Upvotes: 0
Reputation: 20198
you can use List.toArray(playerList), and then loop over the list to make a long string and toast that
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();
Upvotes: 1