Marroquino Marroquino - 21 days ago 6
Android Question

How to use save 2 editText to 2 spinners in same Activity

I want to make the second "save" button save the second editText to second spinner like the first does this is the code I'm trying to use and thanks to cricket_007 for helping me with the call() function

List<String> pn, txt;
Spinner sp1, sp2;
Button b, b1, b2, b3;
EditText et, et1;

ArrayAdapter<String> adp1, adp2; // Added

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

setupSpinners();

b = (Button) findViewById(R.id.button);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
et = (EditText) findViewById(R.id.editText);
et1 = (EditText) findViewById(R.id.editText1);

b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adp1.add(et.getText().toString());
et.setText(null);
}

});
}

public void setupSpinners() {

sp1 = (Spinner) findViewById(R.id.spinner);
sp2 = (Spinner) findViewById(R.id.spinner1);
pn = new ArrayList<String>();
txt = new ArrayList<String>();
txt.add("Sorry Boss I'cant go today i'm sick");
pn.add("123456789");

adp1 = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, pn);
adp2 = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, txt);

sp1.setAdapter(adp1);
sp1.setSelection((pn.size() - 1));
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
sp1.setSelection(arg2);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adp2.add(et1.getText().toString());
et1.setText(null);

sp2.setAdapter(adp2);
sp2.setSelection((txt.size() - 1));
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
sp2.setSelection(arg2);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
});
}


Layout Snap

Answer

You don't need the call method, really. You only need to setup the adapter and spinner's once.

Whatever you are trying to do between the click events, just gets added into onItemSelected, but in order to set the selection of the other spinner, it needs some data, so add an adapter to it.

And, for your latest edit of the question, you've not indented the code correctly, but you've set some stuff within the onClick of the button that shouldn't be there.

public class MainActivity extends AppCompatActivity {

    List<String> pn, txt;
    Spinner sp1, sp2;
    Button b, b1, b2, b3;
    EditText et, et1;

    ArrayAdapter<String> adp1, adp2; // Added

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

        setupSpinners();

        b = (Button) findViewById(R.id.button);
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        et = (EditText) findViewById(R.id.editText);
        et1 = (EditText) findViewById(R.id.editText1);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adp1.add(et.getText().toString());  // Changed
                et.setText("");
            }
        });

       b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adp2.add(et1.getText().toString());
                et1.setText("");
            }
        });
    }

    public void setupSpinners() {

        sp1 = (Spinner) findViewById(R.id.spinner);
        sp2 = (Spinner) findViewById(R.id.spinner1);
        pn = new ArrayList<String>();
        txt = new ArrayList<String>();
        txt.add("Sorry Boss I'cant go today i'm sick");
        pn.add("123456789");

        adp1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, pn);
        adp2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, txt);

        sp1.setAdapter(adp1);
        sp1.setSelection((pn.size() - 1));
        sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                sp2.setSelection(arg2);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });

        sp2.setAdapter(adp2);

    }
}