0

I have a simple question here yet I still have no idea how to do. So, my class have a Spinner with EditText value which will be passed to another activity. I was able to pass EditText to another activity on button click trigger. But I do not know on how to pass Spinner value that I set in the String like the codes below. Perhaps, someone can show me how can I pass both value to the next activity when I click the button. I hope for the better solution. Thank You.

public class SearchActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText editTextProduct;
    private Button btnSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_product);

        editTextProduct = (EditText) findViewById(R.id.editTextProduct);
        btnSearch = (Button) findViewById(R.id.btnSearch);

        btnSearch.setOnClickListener(this);

        initializedSpinner();
    }

    private void initializedSpinner(){
        // Spinner element
        Spinner spinner = (Spinner) findViewById(R.id.spinner_search);

        // Spinner Drop down elements
        List<String> categories = new ArrayList<String>();
        categories.add("Search By Name");
        categories.add("Search By Type");

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

        //spinner.setAdapter(dataAdapter);
        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                String searchItem = parent.getItemAtPosition(position).toString();

                Toast.makeText(parent.getContext(),searchItem + " Selected" , Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void searchData() {
        String productName = editTextProduct.getText().toString().trim();
        if (productName.equals("")) {
            Toast.makeText(this, "Please Enter Product Name", Toast.LENGTH_LONG).show();
            return;
        }
        else{
            Intent i = new Intent(SearchActivity.this, ListProductActivity.class);
            EditText tv5 = (EditText) findViewById(R.id.editTextProduct);
            i.putExtra("productName",tv5.getText().toString());
            startActivity(i);
        }
    }

    @Override
    public void onClick(View v) {

        searchData();
    }
}

3 Answers3

1

Try this on spinner click:

String Text = mySpinner.getSelectedItem().toString();

You will get the string which you select in spinner. Whatever the text value you got you just need to pass via putExtras from intent.

vilpe89
  • 4,656
  • 1
  • 29
  • 36
0

You can pass the values like this:

@Override
    public void onClick(View v) {

      Intent i = new Intent(SearchActivity.this, ListProductActivity.class);
            i.putExtra("editTextValue",editTextProduct.getText().toString());
            i.putExtra("spinnerValue",spinner.getSelectedItem().toString());
            startActivity(i);
    }
Usman Rana
  • 2,067
  • 1
  • 21
  • 32
0
String[] stdarray = { "KG", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "Other" };

       ArrayAdapter aa = new ArrayAdapter<String>(getActivity(), R.layout.layout_std, stdarray);

       final ListView lv = (ListView) rootView.findViewById(R.id.listView);

       lv.setAdapter(aa);

       lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               Tab1.sentfromStd(lv.getItemAtPosition(position).toString()); //I used static method here sentfromStd(), You can also use intents like others said for getting your job done.
           }
       });
Sumit Shetty
  • 112
  • 1
  • 9