Android: How to copy string value in one spinner to another spinner

is there any method to show the text on one spinner to another spinner. i.e, if i selected one item in s1 i want show same in s2 i am using check box for showing. As how we are doing it for edit text in android through this method.

             (etxt_appbilladd.setText(etxt_appresadd.getText().toString());

Is there any idea.?guide me......

Upvotes: 1

Views: 849

Answers (1)

Balaji
Balaji

Reputation: 2026

Try this Code.

I hope it will helpful to you...

    List<String> li;
    Spinner sp1,sp2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        li=new ArrayList<String>();

        li.add("Data 1");
        li.add("Data 2");

        sp1=(Spinner) findViewById(R.id.spinner1);
        sp2=(Spinner) findViewById(R.id.spinner2);
        Button b=(Button) findViewById(R.id.button1);
        final EditText et=(EditText) findViewById(R.id.editText1);
        call();

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            // TODO Auto-generated method stub
            li.add(et.getText().toString());
            et.setText(null);
            call();
            }
        });



    }

    public void call() {
        // TODO Auto-generated method stub

        ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line,li);
        adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        sp1.setAdapter(adp);
        sp2.setAdapter(adp);
        sp1.setSelection((li.size()-1));


        sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                sp2.setSelection(arg2);
            }

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

            }
        });

    }

Upvotes: 1

Related Questions