Reputation: 70
When I run the app, it will directly select the first item and then call the intent to GoogleMap
even if user not yet select any option. How to prevent this? Below are parts of the codes...thank you
final ArrayList<Country> countries = new ArrayList<Country>();
countries.add(new Country("Malaysia", R.drawable.malaysia));
countries.add(new Country("Korea", R.drawable.south_korea));
countries.add(new Country("Argentina", R.drawable.argentina));
countries.add(new Country("Australia", R.drawable.australia));
countries.add(new Country("Japan", R.drawable.japan));
countries.add(new Country("United Kingdom", R.drawable.united_kingdom));
customSpinner = (Spinner)findViewById(R.id.custom_spinner);
SpinnerAdapter adapter = new SpinnerAdapter(this, countries);
customSpinner.setAdapter(adapter);
customSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Intent.ACTION_VIEW);
switch (position) {
case 0:
intent.setData(Uri.parse("geo:4.213155, 103.402914"));
break;
case 1:
intent.setData(Uri.parse("geo:36.593562, 127.040436"));
break;
case 2:
intent.setData(Uri.parse("geo:-34.883324, -65.140799"));
break;
case 3:
intent.setData(Uri.parse("geo:-24.372645, 131.823709"));
break;
case 4:
intent.setData(Uri.parse("geo:36.875761, 138.729092"));
break;
case 5:
intent.setData(Uri.parse("geo:54.887410, -2.913750"));
break;
}
if (intent.resolveActivity(getPackageManager()) != null)
startActivity(intent);
}
}
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View spinnerItem = convertView;
if(spinnerItem == null){
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(getContext().LAYOUT_INFLATER_SERVICE);
spinnerItem = inflater.inflate(R.layout.spinner_rows, parent, false);
//spinnerItem = LayoutInflater.from(getContext()).inflate(R.layout.spinner_rows, parent, false);
}
Country tempCountry = (Country) getItem(position);
ImageView image = (ImageView) spinnerItem.findViewById(R.id.imageView);
TextView text = (TextView) spinnerItem.findViewById(R.id.textView);
image.setImageResource(tempCountry.getCountryImage());
image.setVisibility(View.VISIBLE);
text.setText(tempCountry.getCountryName());
return spinnerItem;
}
@Override
public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
return getView(position, convertView, parent);
}
Upvotes: 3
Views: 8020
Reputation: 11
Make the first selection of the list empty.
String selecteditem="";
if (selecteditem.isEmpty())
{
}else{
// do work
}
Upvotes: 0
Reputation: 202
In MainActivity.java
introduce a boolean variable boolean isFirstTime = true;
And in the onItemSelected()
method add this line:
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//If the Spinner's item is selected for the first time then return and make
//isFirstTime to false, so when the user select it for the second time (as user is
//always second) the spinner will work fine.
if(isFirstTime){
isFirstTime = false;
return;
}
... }
However when the user selects the first option then android does not call onItemSelected() method So you have to make a custom spinner that will call onItemSelected() even when the item is selected second time
public class CustomSpinner extends android.support.v7.widget.AppCompatSpinner {
private int lastPosition = 0;
//overrite the required constructors
...
//
//In android Spinners, when an item is selected for the second time then android does not call onItemSelected () method
//However, when an item is selected, the setSelection method will always be triggered
// So in this method, we record and check the last selection position. If they are the same, just call the monitor manually
@Override
public void setSelection(int position, boolean animate) {
super.setSelection(position, animate);
if (position == lastPosition){
getOnItemSelectedListener().onItemSelected(this,null,position,0);
}
lastPosition = position;
}
@Override
public void setSelection(int position) {
super.setSelection(position);
if (position == lastPosition){
getOnItemSelectedListener().onItemSelected(this,null,position,0);
}
lastPosition = position;
}
}
Upvotes: 0
Reputation: 1255
Hi this may be a little late but I have a simple solution which works for me. I create a switch for any spinner.
So I set a variable
int spinnerSwitch;
.
Then in onCreate
I set the switch to spinnerSwitch = 1;
Then
@Override
public void onItemSelected(AdapterView<?> parent, View view
, int position, long id) {
String itemText = parent.getItemAtPosition(position).toString();
Log.i(TAG, "Selected: " + itemText );
if(spinnerCitySwitch == 1){
spinnerCitySwitch = 0;
return;
}
//Then you can write your code here
}
So when my activity is created, the spinner does automatically select any item in the list. Im sure there are many fancy workarounds out there but this works for me...for now.
Upvotes: 0
Reputation: 5339
add this to your code
countries.add(new Country("Select country", -1))
;
add this to your adapter
if (tempCountry.getCountryImage() != -1 ){
image.setImageResource(tempCountry.getCountryImage());
image.setVisibility(View.VISIBLE);
}else{
image.setVisibility(View.GONE);
}
and your in the case start with case 1
Upvotes: 1
Reputation: 61
please try below code...
final ArrayList<Country> countries = new ArrayList<Country>();
countries.add(new Country("Select Country", R.drawable.country_icon));
countries.add(new Country("Malaysia", R.drawable.malaysia));
countries.add(new Country("Korea", R.drawable.south_korea));
countries.add(new Country("Argentina", R.drawable.argentina));
countries.add(new Country("Australia", R.drawable.australia));
countries.add(new Country("Japan", R.drawable.japan));
countries.add(new Country("United Kingdom", R.drawable.united_kingdom));
customSpinner = (Spinner)findViewById(R.id.custom_spinner);
SpinnerAdapter adapter = new SpinnerAdapter(this, countries);
customSpinner.setAdapter(adapter);
customSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Intent.ACTION_VIEW);
switch (position) {
case 0:
break;
case 1:
intent.setData(Uri.parse("geo:4.213155, 103.402914"));
break;
case 2:
intent.setData(Uri.parse("geo:36.593562, 127.040436"));
break;
case 3:
intent.setData(Uri.parse("geo:-34.883324, -65.140799"));
break;
case 4:
intent.setData(Uri.parse("geo:-24.372645, 131.823709"));
break;
case 5:
intent.setData(Uri.parse("geo:36.875761, 138.729092"));
break;
case 6:
intent.setData(Uri.parse("geo:54.887410, -2.913750"));
break;
}
if (position!=0 && intent.resolveActivity(getPackageManager()) != null)
startActivity(intent);
}
}
Upvotes: 0
Reputation: 321
Please use this code in your MainActivity while setting adapter it will help you.
SpinnerAdapter adapter = new SpinnerAdapter(this, countries) {
@Override
public boolean isEnabled(int position) {
if (position == 0) {
return false;
}
return true;
}
};
customSpinner.setAdapter(adapter);
Upvotes: 3