EditText input into array

2019-09-14 18:24发布

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,

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;
    }}

2条回答
时光不老,我们不散
2楼-- · 2019-09-14 18:26

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 = 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...

查看更多
闹够了就滚
3楼-- · 2019-09-14 18:46

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:

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:

String s=Screen1.playerList.get(index);// this gives your the string saved in arraylist at index given.

I hope,it's clear now!

查看更多
登录 后发表回答