Edittext Input Into Array
I have an input box (EditText1) 1. that i want to keep adding values and save them to an Array 2. then when am done, i can click the done button and it can take me to the next sc
Solution 1:
Try making your array public static in the activity you are currently in.Then you can directly access that array in next activity.
EDIT :
As per the scenario you declared in your question,Try this code: say this is your Screen1.class:
privateEditText txt1;
publicstaticArrayList<String> playerList = newArrayList<String>();
String playerlist[];
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
// edittext1 or textview1
txt1 = (EditText) findViewById(R.id.editText1);
//add more items buttonButton more = (Button) findViewById(R.id.button1);
more.setOnClickListener(newView.OnClickListener() {
publicvoidonClick(View view){
String ag=txt1.getText().toString().trim();
if(ag.length() != 0){
playerList.add(ag);
txt1.setText(""); // adds text to arraylist and make edittext blank again
}
}
});
//press Done button to redirect to next activityButton done = (Button) findViewById(R.id.done_button);
done.setOnClickListener(newView.OnClickListener() {
publicvoidonClick(View view){
// start new activityIntent myIntent = newIntent(view.getContext(),
Screen2.class);
startActivity(myIntent);
}
});
}
In your Screen2.class,directly access ArrayList like:
String s=Screen1.playerList.get(index);// this gives your the string saved in arraylist at index given.I hope,it's clear now!
Solution 2:
While onClick the done button just pass the string array to next activity and get the values of array in next screen and display it..You can pass the string array as below
Intent myIntent = newIntent(view.getContext(),
Screen2.class);
myIntent.putExtra("Stringarray",arrayvalue);
startActvity(myIntent);
finish();
In Next activity you can get value as
String[] array=getIntent().getExtras().getStringArray("Stringarray");
Edit:
for(int i=0;i<array.size;i++)
{
Toast.makeText(this,array[i], Toast.LENGTH_LONG); // here if you have value in array then it will display in toast one by one.
}
I think it may help you...
Post a Comment for "Edittext Input Into Array"