eclipse EditText 输入到数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9476050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EditText input into array
提问by user1179083
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 screen and I can display the values or call the values of the array.. this is what i have so far 1. Done Button Works 2. Add more button and storing to array doesn't quite work....HELP PLZ,
我有一个输入框 (EditText1) 1. 我想继续添加值并将它们保存到数组 2. 然后完成后,我可以单击完成按钮,它可以带我到下一个屏幕,我可以显示值或调用数组的值.. 这是我到目前为止所拥有的 1. 完成按钮工作 2. 添加更多按钮并存储到数组不太工作......帮助PLZ,
Thanks
谢谢
EDIT :Here is the edited code:
编辑:这是编辑后的代码:
private EditText txt1;
public static ArrayList<String> playerList = new ArrayList<String>();
String playerlist[];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
// edittext1 or textview1
txt1 = (EditText) findViewById(R.id.editText1);
final String 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){
playerList.add(ag);
DisplayToast("Current Players" + playerList);
Intent myIntent = new Intent(view.getContext(),
Screen2.class);
myIntent.putExtra("playerList",playerList);
startActivity(myIntent);
}
}});
}
//display message
private <Strg> String DisplayToast(String ag) {
Toast.makeText(getBaseContext(), ag, Toast.LENGTH_LONG)
.show();
return ag;
}}
采纳答案by Hiral Vadodaria
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:
根据你在问题中声明的场景,试试这个代码:说这是你的 Screen1.class:
private EditText txt1;
public static ArrayList<String> playerList = new ArrayList<String>();
String playerlist[];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
// edittext1 or textview1
txt1 = (EditText) findViewById(R.id.editText1);
//add more items button
Button more = (Button) findViewById(R.id.button1);
more.setOnClickListener(new View.OnClickListener() {
public void onClick(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 activity
Button done = (Button) findViewById(R.id.done_button);
done.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
// start new activity
Intent myIntent = new Intent(view.getContext(),
Screen2.class);
startActivity(myIntent);
}
});
}
In your Screen2.class,directly access ArrayList like:
在 Screen2.class 中,直接访问 ArrayList,如:
String s=Screen1.playerList.get(index);// this gives your the string saved in arraylist at index given.
I hope,it's clear now!
我希望,现在清楚了!
回答by deepa
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
当 onClick 完成按钮只是将字符串数组传递给下一个活动并在下一个屏幕中获取数组的值并显示它..您可以传递字符串数组如下
Intent myIntent = new Intent(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...
我想它可以帮助你...