Get the String value from Second spinner position - java

I have two spinner.
First spinner value is Kathmandu and Bhaktapur and depending on first spinner value, second spinner value is filled up.
My problem is when I select Kathmandu and select first item from second spinner I get the first item of second spinner which is what i expect.
But when I select Bhaktapur or any item below Kathmandu from Spinner first, Then on Selecting first item position from second spinner I get first spinner value itself which Is Bhaktapur instead of "Bageswori"
But if I select second item position then there is no problem i.e I get the "Balkot" which is expected outcome.
So problem lies when I select any item below first item position from First spinner and first item position form second spinner.It returns first item value of First spinner instead of selected value from Second spinner.
My Code is
public class UploadBook extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
String item = "start";
String condition = "start"; // for spinner
FirebaseStorage storage;
public Books b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_book);
getSupportActionBar().setTitle("Upload Room Info");
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#8abe50")));
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP)
ActivityCompat.requestPermissions(UploadBook.this, new String[]{android.Manifest.permission.CAMERA, android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 11);
database = FirebaseDatabase.getInstance();
dbreference = database.getReference();
fauth = FirebaseAuth.getInstance();
mStorageRef = FirebaseStorage.getInstance().getReference();
b1 = (Button) findViewById(R.id.buttonPost);
spinnerCountry = (Spinner) findViewById(R.id.spinnerCountry);
//implementing OnItemSelectedListener (need to override the method)
countryArray = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
countryArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCountry.setAdapter(countryArray);
countryArray.add("Kathmandu");
countryArray.add("Bhaktapur");
countryArray.add("Lalitpur");
countryArray.setNotifyOnChange(true);
spinnerCountry.setSelection(0);
spinnerDivision = (Spinner) findViewById(R.id.spinnerDivision);
//implementing OnItemSelectedListener (need to override the method)
spinnerDivision.setOnItemSelectedListener(this);
spinnerCountry.setOnItemSelectedListener(this);
divisionArray = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
divisionArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDivision.setAdapter(divisionArray);
postButtonClick();
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
item = parent.getItemAtPosition(position).toString();
//first spinner item position
int countrySpinnerPosition = spinnerCountry.getSelectedItemPosition();
switch (countrySpinnerPosition){
case 0:
//fill data for second spinner
fillKathmanduDivision();
break;
case 1:
//fill data for second spinner
fillBhaktapurDivision();
break;
}
//first spinner item position
}
private void fillKathmanduDivision() {
divisionArray.clear();
divisionArray.add("Anamnagar");
divisionArray.add("Balaju");
divisionArray.notifyDataSetChanged();
}
private void fillBhaktapurDivision() {
divisionArray.clear();
divisionArray.add("Bageswori");
divisionArray.add("Balkot");
divisionArray.notifyDataSetChanged();
}
b1.setOnClickListener(new View.OnClickListener() {
}
void postButtonClick() {
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(this, item, Toast.LENGTH_SHORT).show();
}
}
}

Please use this code below :
private String country_id = "", city_id = "";
private ArrayList<String> country_array;
and to fill your array with data :
for (int i = 0; i < items.getResponse().size(); i++) {
String get_country_name = items.getResponse().get(i).getCode();
country_array.add(get_country_name);
}
Now for your first spinner adapter :
ArrayAdapter<String> country_spinnerArrayAdapter = new adapter_spinner(getActivity(), R.layout.spinner_header, country_array);
country_spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
binding.etCountry.setAdapter(country_spinnerArrayAdapter);
binding.etCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedItemText = parent.getSelectedItem().toString();
if (position > 0) {
country_id = selectedItemText;
city_id = "";
Load_City(position - 1); // -1 to remove postion of the static country text
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Now your City (2nd spinner) method :
private void Load_City(int postion) {
ArrayList<String> city_array = new ArrayList<String>();
city_array.add(getString(R.string.city));
for (int i = 0; i < items.getResponse().get(postion).getCities().size(); i++) {
String get_city_name = items.getResponse().get(postion).getCities().get(i).getName();
city_array.add(get_city_name);
}
ArrayAdapter<String> country_spinnerArrayAdapter = new adapter_spinner(getActivity(), R.layout.spinner_header, city_array);
country_spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
binding.etCity.setAdapter(country_spinnerArrayAdapter);
binding.etCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedItemText = parent.getSelectedItem().toString();
if (position > 0) {
city_id = selectedItemText;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
i hope this helped

You have taken wrong reference of first spinner.
in below line
int countrySpinnerPosition = spinnerCountry.getSelectedItemPosition();
you have taken spinnerCountry instead of spinnerFirst.
And you have initially set spinnerFirst to first postion that is A and When you change it-to select B its value not taken in onItemSelected method ,intend of takes country selected that is probably on first (0) position of spinner so you second spinner always set to Three instead of Four.

Related

After filtering ListView, I'm not able to get the old position

I am not able to get the old position of my ListView after filtering, for example I should be getting position 3,5,8 after filtering, but I'm getting the default 0,1,2 position.. Do you guys have any idea why? Here's my code..
public class Application_applicant_list extends AppCompatActivity {
private ListView applicant;
static String applId;
private ArrayAdapter adapter;
SearchView sv;
Bundle b;
Intent intent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_application_list);
sv = (SearchView) findViewById(R.id.appSv);
applicant = (ListView) findViewById(R.id.listView_allForms_from_list);
adapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1);
applicant.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView < ? > adapterView, View view, int position, long id) {
intent = new Intent(Application_applicant_list.this, Application_form_from_list.class);
b = new Bundle();
b.putInt("position", position);
intent.putExtras(b);
Toast.makeText(getApplicationContext(), "Position:" + position,
Toast.LENGTH_SHORT).show();
startActivity(intent);
}
});
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
}
This is the part where I retrieve from the database, they're in the same class
#Override
protected void onResume() {
DBHelper db = new DBHelper(this);
ArrayList < String > ArrApp = new ArrayList < String > ();
for (int i = 0; i < db.getAll_Appl().size(); i++) {
ArrApp.add(String.valueOf(db.getAll_Appl().get(i).getApplId())); // + '\t' + '\t' + '\t' + db.getAll_IOP().get(i).getId());
}
adapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, ArrApp) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView item = (TextView) super.getView(position, convertView, parent);
item.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
item.setGravity(Gravity.CENTER);
item.setPadding(0, 10, 0, 10);
return item;
}
};
applicant.setAdapter(adapter);
super.onResume();
}
}
This is my nextActivity.java where I retrieve the position
Bundle b2 = getIntent().getExtras();
if (b2 != null) {
int position = b2.getInt("position"); // position is from induction and operation page menu item click
form_from_list = db.getAll_Appl().get(position);
formAppId.setText(form_from_list.getApplId());
Because, the position that return in onItemClick is the position of ListView item not the position in list datasource (in your case it is ArrApp).
Therefore, I think you should get the value of ListView item then pass it to another Activity instead of the position.
Like
applicant.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView < ? > adapterView, View view, int position, long id) {
String clickedItemValue = (String) adapterView.getAdapter().getItem(position); // cast to String because your list datasource is a list String
// pass clickedItemValue to another activity
}
});

Spinner Value to String

Been making my first app and slow progress is being made. Just wondering how I return a Spinner value to pass it as a string.
Here is my code:
First the event class:
public void event(){
calanderBtn = (Button)findViewById(R.id.eventBtn);
calanderBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra(CalendarContract.Events.TITLE, "Home");
intent.putExtra(CalendarContract.Events.DESCRIPTION, "Cleaning : ");
intent.putExtra(CalendarContract.Events.EVENT_LOCATION, "Home");
startActivity(intent);
}
});
}
Second the Spinner Class:
public void selectcleaning() {
spCleaning = (Spinner) findViewById(spinner);
adapterCleaningType = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, cleaningType);
adapterCleaningType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCleaning.setAdapter(adapterCleaningType);
}
I'm trying to select the value from selectcleaning (Value is either Yes or No) and pass it in the descrption in the Calendar of the event class and I'm not sure what to do.
you can use this code to get the value
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
or get the value when spinner selected using OnItemSelectedListener (Android Doc)
public class SpinnerActivity extends Activity implements
OnItemSelectedListener {
#Override
protected void onStart() {
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
You should try using the Overrides for the spinner class as so:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}});
For your purposes 'int position' would equal 0 (yes) or 1 (no)
Try setting an on item selected listener. When an item is selected, you can store that value in a global variable. It gets the first item by default too.
spCleaning.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Value is current spinner item
globalStringVariable = (String) parent.getItemAtPosition(position);
}
// Because AdapterView is an abstract class, onNothingSelected must be defined
#Override
public void onNothingSelected(AdapterView<?> parent) {
// This is default, before you've selected anything.
// It gets the first value.
globalStringVariable = parent.getItemAtPositon(0);
}
});

android Unable to get all editext values using in listview inside custom dialog

I have one listview inside the Custom Dialog. In that list i have one Text field and one Edittext for each row. I am using editTextWatcher for handling all edittext values on exact position. Up to this working fine. I have one save button on that dialog under the listView. After click on save i want to post the all editext data to server. But i am able to post only that values of edittext those are in front view.
Dialog Layout :
private void showDialog(){
dialog1 = new Dialog(this);
final Dialog tradDialog = new Dialog(this, android.R.style.Theme_Light_NoTitleBar);
//tradDialog.setContentView(R.layout.trad_dialog_layout);
View view = getLayoutInflater().inflate(R.layout.trad_dialog_layout_individual, null);
//tradDialog.setContentView(R.layout.trad_dialog_layout);
tradDialog.setCanceledOnTouchOutside(false);
// holder.mWatcher = new MutableWatcher();
lv = (ListView) view.findViewById(R.id.productsListView);
RelativeLayout saveBtnLayout = (RelativeLayout) view.findViewById(R.id.saveBtnLayout);
// Change MyActivity.this and myListOfItems to your own values
clad = new CustomListAdapterDialog(SolutionActivity.this, individual_productChoosedAr);
lv.setAdapter(clad);
clad.notifyDataSetChanged();
//save button for posting all edittext values to server
saveBtnLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < lv.getChildCount(); i++) {
v = lv.getChildAt(i);
etPrice = (EditText) v.findViewById(R.id.etPrice);
ProductPrice = etPrice.getText().toString();
if(ProductPrice.equals("")){
ProductPrice = "NULL";
}
productPriceAr.add(ProductPrice);
}
Toast toast = Toast.makeText(getApplicationContext(),"Please wait...",Toast.LENGTH_LONG);
toast.show();
SendIndividualDatatoServer sendIndividualData = new SendIndividualDatatoServer();
sendIndividualData.execute();
}
});
//lv.setOnItemClickListener(........);
dialog1.setContentView(view);
dialog1.show();
}
Custom Adatper :
public View getView(final int position, View convertView, ViewGroup parent) {
final int pos = position ;
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.custom_layout_individual, null);
holder = new ViewHolder();
holder.product = (TextView) convertView.findViewById(R.id.tvProductName);
holder.price = (EditText) convertView.findViewById((R.id.etPrice));
holder.mWatcher = new MutableWatcher();
holder.price.addTextChangedListener(holder.mWatcher);
etPrice = (EditText) convertView.findViewById(R.id.etPrice);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mWatcher.setActive(false);
holder.price.setText(myList.get(position), TextView.BufferType.EDITABLE);
holder.mWatcher.setPosition(position);
holder.mWatcher.setActive(true);
holder.product.setText(listData.get(position).P_Name);
productIds = listData.get(position).Category_Id;
return convertView;
}
TextWatcher class:
#Override
public void afterTextChanged(final Editable s) {
// SolutionActivity.this.runOnUiThread(new Runnable() {
// #Override
// public void run() {
if (mActive) {
myList.put(mPosition, s.toString());
System.out.print(s.toString());
}
What are you passing to your CustomAdapter ?
You should use a custom object having the property which will contain the editText's text.
So, you will pass an ArrayList myList of type custom object to the adapter.
Whenever user types something, you get the position of the editText in the listview, then get the custom object at that position and write the editText value in the object's property.
Like this :
MyCustomClass object = myList.get(position);
object.setValue(edtText.getText());
Like this you will have all the data in the arrayList.
On save button click, you can loop through the arrayList and check individual object property, if it has data add it to your productPriceList.
Hope this helps!
Finally i got answer myself:
I counted list items and put only positions in Hashmap.
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
int mCount = lv.getCount();
if(mIndCount==1){
for(int i = 0; i<mCount;i++){
myList.put(i,"");
}
mIndCount=2;
}
}
And for posting all values i used following :
for (int i = 0; i < myList.size(); i++) {
if(myList.get(i).toString().equals("")){
ProductPrice = "NULL";
}else {
ProductPrice = myList.get(i).toString();
}
productPriceAr.add(ProductPrice);
}

Adding a button to each row of a list view in android?

I want to add two buttons to each row of a list view. And I want to add them in the custom adapter. The two buttons are for edit and delete. The following is my adapter code.
public class RoleList extends ArrayAdapter<String>
{
private String[] name;
private String[] username;
private String[] password;
private String[] role;
private Activity context;
public RoleList(Activity context, String[] name, String[] username, String[] password, String[] role)
{
super(context, R.layout.role_list,name);
this.context = context;
this.name = name;
this.username =username;
this.password = password;
this.role = role;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.role_list, null, true);
TextView textViewName = (TextView) listViewItem.findViewById(R.id.tv_empname);
TextView textViewusername = (TextView) listViewItem.findViewById(R.id.tv_empusername);
TextView textViewPass = (TextView) listViewItem.findViewById(R.id.tv_emppassword);
TextView textViewRole = (TextView) listViewItem.findViewById(R.id.tv_emprole);
textViewName.setText(name[position]);
textViewusername.setText(username[position]);
textViewPass.setText(password[position]);
textViewRole.setText(role[position]);
return listViewItem;
}
}
Handler for the list:-
public void userRolehandler()
{
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run()
{
RoleList roleList = new RoleList(UserRegistration.this,employee_name,emp_username,emp_password,employee_role);
userList.setAdapter(roleList);
roleList.notifyDataSetChanged();
}
});
}
//User Role array
public void userRoleArray() throws JSONException {
name = editTextName.getText().toString();
username = editTextUsername.getText().toString();
password = editTextPassword.getText().toString();
emp_role = textRole.getText().toString();
JSONObject jsonobj = new JSONObject();
jsonobj.put("name",name);
jsonobj.put("username",username);
jsonobj.put("password",password);
jsonobj.put("emp_role",emp_role);
rolejson.put(counter,jsonobj);
counter++;
}
//travRoleArray
public void travRoleArray() throws JSONException {
response = "{\"result\":" + rolejson.toString() + "}";
Log.d("RESPONSE",response);
JSONObject jsonOb = new JSONObject(response);
JSONArray result = jsonOb.getJSONArray(JSON_ARRAY);
try
{
for (int i=0; i< result.length();i++)
{
JSONObject jObj = result.getJSONObject(i);
employee_name[i] = jObj.getString(KEY_NAME);
emp_username[i] = jObj.getString(KEY_UNAME);
emp_password[i] = jObj.getString(KEY_PASS);
employee_role[i] = jObj.getString(KEY_ROLE);
}
}catch (ArrayIndexOutOfBoundsException e)
{
Toast.makeText(UserRegistration.this, "Contact customer care for assigning more roles", Toast.LENGTH_LONG).show();
}
}
Scenario:
User clicks on add user button a dialog box opens
user enters the required fields and clicks on register
a row gets added to the list view.
Screen shots:
Dialog box:
Final screen:
Now I want to add two buttons edit and delete. To allow the
user to edit and delete the row added to the list view.
The simplest solution is you should change your String[] name to ArrayList<String> name
public class RoleList extends ArrayAdapter<String>{
//private String[] name;
private ArrayList<String> name;
// it show how many items are in the data set represented by this Adapter.
#Override
public int getCount(){
return name.size();
}
}
Current you use String[], and the size of it is fix
String[] toppings = new String[10];
System.out.println("Hello World"+toppings.length); // will print 10
therefore your ListView always display 10 items (even some is empty)
But if you use ArrayList<String>, the size of list auto change when you add or remove item then your ListView will have total row = list size
OR if you still want to use String[]
You can create a new int variable like realTotalFriends (it start from 0) and every time you add new user (just increase it by 1)
Then inside getCount() you return realTotalFriends
int realTotalFriends;
#Override
public int getCount(){
return realTotalFriends;
}
Hope this help
First you need to re-edit your R.layout.role_list to add your button. I assume you know how to add button to layout. After that on your getView() method you need to define your buttons and set click listeners for them.
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.role_list, null, true);
TextView textViewName = (TextView) listViewItem.findViewById(R.id.tv_empname);
TextView textViewusername = (TextView) listViewItem.findViewById(R.id.tv_empusername);
TextView textViewPass = (TextView) listViewItem.findViewById(R.id.tv_emppassword);
TextView textViewRole = (TextView) listViewItem.findViewById(R.id.tv_emprole);
Button edit = (Button ) listViewItem.findViewById(R.id.edit_button);
Button delete = (Button ) listViewItem.findViewById(R.id.delete_button);
textViewName.setText(name[position]);
textViewusername.setText(username[position]);
textViewPass.setText(password[position]);
textViewRole.setText(role[position]);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
yourList.remove(position);
notifyDataSetChanged();
}
});
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// May be you can show a popup here
}
});
return listViewItem;
}
Hope it works for your needs
sorry for my english.
simply you should add buttons in your "role_list.xml" as you added TextView. then you can add onClickListner on button in your adapter class.
I hope it will help you.

Check item selected on two spinners?

I have two spinners on my layout as below:
public class DemoActivity extends Activity {
private static final String STATE_SCALE = "state-scale";
private static final String STATE_CENTER_X = "state-center-x";
private static final String STATE_CENTER_Y = "state-center-y";
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
String text1 = spinner1.getSelectedItem().toString();
Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
String text3 = spinner3.getSelectedItem().toString();
if (text1.equals("Harris Academy"))
Harris(spinner1);
if (text1.equals("Harris Academy") && text3.equals("Ground Floor")) {
Toast.makeText(getBaseContext(), text3, Toast.LENGTH_LONG).show();
try {
SubsamplingScaleImageView imageView =
(SubsamplingScaleImageView) findViewById(id.imageView);
imageView.setImageAsset("DSC00277.png");
if (savedInstanceState != null &&
savedInstanceState.containsKey(STATE_SCALE) &&
savedInstanceState.containsKey(STATE_CENTER_X) &&
savedInstanceState.containsKey(STATE_CENTER_Y)) {
imageView.setScaleAndCenter(savedInstanceState.getFloat(STATE_SCALE),
new PointF(savedInstanceState.getFloat(STATE_CENTER_X),
savedInstanceState.getFloat(STATE_CENTER_Y)));
}
} catch (IOException e) {
Log.e(DemoActivity.class.getSimpleName(), "Could not load asset", e);
}
}
}
}
This works fine when the app starts it gets the value of spinner 1 and spinner 3 and then does something depending on the values.
How do I extend it so that when I select an item on either spinner it does something based on the new values?
To get updated value as user select item from spinner,
You have to implement spinner's onItemSelectedListner
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// Do your stuff here for spinner1
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// Do your stuff here for spinner3
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
If you want to manipulate views or data on the selection of item in Spinner you need to implement
ItemSelectedListener to the particular Spinner.
Sample Code:
// in onCreate() or where you want to bind this Views with XML views
{
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner3 = (Spinner) findViewById(R.id.spinner3);
// mItemSelectedListener Object of OnItemSelectedListener to handle Item Selection to Spinners.
spinner1.setOnItemSelectedListener(mItemSelectedListener);
spinner3.setOnItemSelectedListener(mItemSelectedListener);
}
// Creating an Object of Anonymous Class so no need to create another instance for same class
// OutSide of onCreate() method.
OnItemSelectedListener mItemSelectedListener = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (view.getId()) {
case R.id.spinner1:
// Manipulates Views at selection of item in Spinner1
break;
case R.id.spinner3:
// Manipulates Views at selection of item in Spinner3
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
};

Categories