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();
}
}