将文本字段的值添加到数组中

时间:2014-06-16 17:04:07

标签: java android arrays arraylist android-spinner

我需要将键入文本字段的值添加到数组列表中。

我按如下方式实施:

public class MainActivity extends ActionBarActivity {
 Spinner spinner1,spinner2,spinner3;
 Button add;
 EditText subject;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 // Spinner element
   spinner1 = (Spinner) findViewById(R.id.spinner1);
   spinner2 = (Spinner) findViewById(R.id.spinner2);
   spinner3 = (Spinner) findViewById(R.id.spinner3);
   add = (Button) findViewById(R.id.button1);
   subject = (EditText) findViewById(R.id.editText1);

// Spinner click listener

   //spinner2.setOnItemSelectedListener(this);
   //spinner3.setOnItemSelectedListener(this);
   spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

       @Override
       public void onItemSelected(AdapterView<?> adapter, View v,
               int position, long id) {
           // On selecting a spinner item
           String item = adapter.getItemAtPosition(position).toString();

           // Showing selected spinner item
           Toast.makeText(getApplicationContext(),
                   "Selected Subject : " + item, Toast.LENGTH_LONG).show();
       }

       @Override
       public void onNothingSelected(AdapterView<?> arg0) {
           // TODO Auto-generated method stub

       }
   });
   add.setOnClickListener(new OnClickListener() {


     Context context;

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String content;
         content = subject.getText().toString();
        List list = new ArrayList();
        list.add(content);
        ArrayAdapter dataAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item, list);

        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            spinner1.setAdapter(dataAdapter);
            ListIterator it = list.listIterator();

           it.hasNext();

    }
   });



    }
public void addListenerOnSpinnerItemSelection() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
            //do something here
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            //optionally do something here
        }
    });
  }

但是,每次在文本字段中输入新值时,前一个值都会被覆盖。如何使用数组实现此功能?每当我输入一个值时,我希望它在下拉菜单中单独保存。

2 个答案:

答案 0 :(得分:0)

之前的值会被覆盖,因为您始终会在list中添加新的adapter实例。 所以只列出一次列表实例,即。全球性的活动.i.e。添加此行

 List list = new ArrayList();

下面的

public class MainActivity extends ActionBarActivity {
 Spinner spinner1,spinner2,spinner3;
 Button add;
 EditText subject;

并在onClick函数中使用以下代码。如

 String content;
            content = subject.getText().toString();
           list.add(content);
            ArrayAdapter dataAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item, list);

            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            spinner1.setAdapter(dataAdapter);

答案 1 :(得分:0)

这种情况正在发生,因为每次有新值时,都会创建一个新的ArrayList(List list = new ArrayList();),然后添加它。使列表成为成员变量,然后尝试添加它。