EditText输入到数组中

时间:2012-02-28 03:38:59

标签: android arrays eclipse button android-edittext

我有一个输入框(EditText1) 1.我想继续添加值并将它们保存到数组 2.然后当完成时,我可以点击完成按钮,它可以带我到下一个屏幕,我可以显示值或调用数组的值..这是我到目前为止 1.完成按钮工作 2.添加更多按钮并存储到数组不太有用.... HELP PLZ,

由于

编辑: 这是编辑过的代码:

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 个答案:

答案 0 :(得分:0)

onClick时,完成按钮只是将字符串数组传递给下一个活动,并在下一个屏幕中获取数组的值并显示它。您可以传递字符串数组,如下所示

Intent myIntent = new Intent(view.getContext(),
                Screen2.class);

    myIntent.putExtra("Stringarray",arrayvalue);
    startActvity(myIntent);
   finish();

在下一个活动中,您可以获得价值

        String[] array=getIntent().getExtras().getStringArray("Stringarray");

编辑:

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

我认为它可以帮到你......

答案 1 :(得分:0)

尝试在当前的活动中将您的数组设为公共静态。然后,您可以在下一个活动中直接访问该数组。

编辑:

根据您在问题中声明的方案,请尝试以下代码: 说这是你的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);

        }
    });
}   

在Screen2.class中,直接访问ArrayList,如:

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

我希望,现在很清楚了!