Android set position in a spinner - android

I want to set the position for a spinner. I have a string array for the adapter, i.e,
final String[] cat = { "Highest", "Lowest", "Most Recent"};
But I want my spinner to initially display a blank. So I tried this.
mSpinner.setSelection(-1);
But this doesn't solve my problem. Any ideas how to do this? Help is much needed and appreciated. Thanks.
UPDATE:
My code:
private void displayDialog() {
// TODO displayDialog
final ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, sortBy);
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.dialog_layout, null);
promptsView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
final Spinner mSpinner= (Spinner) promptsView
.findViewById(R.id.spDialog);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Sort By...");
builder.setIcon(R.drawable.launcher);
mSpinner.setAdapter(adp);
mSpinner.setSelection(-1);
mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v,
int pos, long id) {
strSpinner = mSpinner.getSelectedItem().toString();
if(strSpinner.equals("Highest Price")){
highest.setTypeface(Typeface.DEFAULT_BOLD);
lowest.setTypeface(Typeface.DEFAULT);
location.setTypeface(Typeface.DEFAULT);
price = dbHelper.sortHighestPrice();
adapter = new MyCustomAdapter(imgs, text, price);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
} else if (strSpinner.equals("Lowest Price")){
highest.setTypeface(Typeface.DEFAULT);
lowest.setTypeface(Typeface.DEFAULT_BOLD);
location.setTypeface(Typeface.DEFAULT);
price = dbHelper.sortLowestPrice();
adapter = new MyCustomAdapter(imgs, text, price);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
} else if (strSpinner.equals("Location")) {
highest.setTypeface(Typeface.DEFAULT);
lowest.setTypeface(Typeface.DEFAULT);
location.setTypeface(Typeface.DEFAULT_BOLD);
} else {
Log.d("Default", "Default");
}
}

Make your first item Blank.
final String[] cat = {"", "Highest", "Lowest", "Most Recent"};

make your first item blank and set selection of spinner by default is 0.

To set Spinner default value position to -1.
Override the Spinner Class.
that overrides the setAdapter() method.There you can set position to -1.
Please follow the link for details :
How to make an Android Spinner with initial text "Select One"

Related

How can I create a new Spinner dynamically after itemSelected?

I have a Spinner element in one Android Activity.
Now I want that, if I selected on Item of this Spinner, I should be able to create a new Spinner and put it into in my activity. If I select one Item of this second Spinner, I should be able to create a new Spinner ....
so this is the code:
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
v = getActivity().getLayoutInflater().inflate(R.layout.alert_insert_dialog, null);
builder.setView(v);
builder.setTitle("Insert Alerts");
builder.setCancelable(false);
listValueSet_Agent = new ArrayList<AlertValueSet>();
valueSet = new AlertValueSet("2.16.840.1.113883.1.11.20.4","106190000",
"2.16.840.1.113883.6.96","Penicillin","SNOMED CT");
listValueSet_Agent.add(valueSet);
valueSet = new AlertValueSet("2.16.840.1.113883.1.11.20.4","281647001",
"2.16.840.1.113883.6.96","Aspirin","SNOMED CT");
listValueSet_Agent.add(valueSet);
valueSet = new AlertValueSet("2.16.840.1.113883.1.11.20.4","282100009",
"2.16.840.1.113883.6.96","Codeine","SNOMED CT");
listValueSet_Agent.add(valueSet);
valueSet = new AlertValueSet("2.16.840.1.113883.1.11.20.4","282100009",
"2.16.840.1.113883.6.96","Select Agent","SNOMED CT");
listValueSet_Agent.add(valueSet);
//spinner status
Spinner sAgent = (Spinner) v.findViewById(R.id.combo_agent);
adapterAgent = new ArrayAdapter<AlertValueSet>(v.getContext(),
android.R.layout.simple_spinner_item, listValueSet_Agent);
sAgent.setAdapter(adapterAgent);
sAgent.setSelection(0);
sAgent.setOnItemSelectedListener(new SpinnerActivity());
return builder.create();
}
This class is instead, the implementation of OnItemSelectedListener class:
public class SpinnerActivity implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Spinner spinnerAgentN = new Spinner(v.getContext());
spinnerAgentN.setAdapter(adapterAgent);
spinnerAgentN.setSelection(listValueSet_Agent.size()-1);
spinnerAgentN.setOnItemSelectedListener(new SpinnerActivity());
LinearLayout linearLayout = (LinearLayout)v.findViewById(R.id.layoutText);
linearLayout.addView(spinnerAgentN,2);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
Now the problem is, if I try to run my application, this method (onItemSelected) is called for every time, and I can see in my activity N spinner, but this is not good.
If I try to delete this code:
spinnerAgentN.setOnItemSelectedListener(new SpinnerActivity());
I don't have any problem.
How can I fixed it?
while data is set to the adapter, then called onitem click listener automatically. So in on item click listener method you have to check the position value. if the position value is 0 then you don't have set the data to second spinner otherwise you have to set the data to second spinner.
Inside onItemSelected()
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
AlertValueSet set = listValueSet_Agent.get(pos);
// Fetch new data based on the selected item and set it back to the spinner
adapterAgent = new ArrayAdapter<AlertValueSet>(v.getContext(),
android.R.layout.simple_spinner_item, listValueSet_Agent);
sAgent.setAdapter(adapterAgent);
}
As per my understanding of code and requirement, there are two options for resolve your query.
But I will explain only one which is fit for your requirement
just declare your array as below.
listValueSet_Agent = new ArrayList<AlertValueSet>();
valueSet = new AlertValueSet("Select","2.16.840.1.113883.1.11.20.4","106190000",
"2.16.840.1.113883.6.96","Penicillin","SNOMED CT");
listValueSet_Agent.add(valueSet);
valueSet = new AlertValueSet("Select","2.16.840.1.113883.1.11.20.4","281647001",
"2.16.840.1.113883.6.96","Aspirin","SNOMED CT");
listValueSet_Agent.add(valueSet);
valueSet = new AlertValueSet("Select","2.16.840.1.113883.1.11.20.4","282100009",
"2.16.840.1.113883.6.96","Codeine","SNOMED CT");
listValueSet_Agent.add(valueSet);
valueSet = new AlertValueSet("Select","2.16.840.1.113883.1.11.20.4","282100009",
"2.16.840.1.113883.6.96","Select Agent","SNOMED CT");
listValueSet_Agent.add(valueSet);
Now, change your onItemSelected method as below,
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
if(pos > 0)
{
Spinner spinnerAgentN = new Spinner(v.getContext());
spinnerAgentN.setAdapter(adapterAgent);
spinnerAgentN.setSelection(listValueSet_Agent.size()-1);
spinnerAgentN.setOnItemSelectedListener(new SpinnerActivity());
LinearLayout linearLayout = (LinearLayout)v.findViewById(R.id.layoutText);
linearLayout.addView(spinnerAgentN,2);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}

Spinner data accept is mulfunctioning

I have a spinner which displays both options, but when I accept the female option, it still takes the answer as a male, any suggestions?
List<String> SpinnerArray = new ArrayList<String>();
SpinnerArray.add("Male");
SpinnerArray.add("Female");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, SpinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner Items = (Spinner) findViewById(R.id.genderSpinner);
Items.setAdapter(adapter);
// Importing all assets like buttons, text fields
inputFullName = (EditText) findViewById(R.id.registerName);
String selected = Items.getSelectedItem().toString();
if (selected.equals("Male")) {
inputGen = "Male";
}
if (selected.equals("Female")){
inputGen = "Female";
}
Following the official guide:
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...
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 can try a switch of position and then try your same conditions.

Default text shown on spinner but hide from drop down list - Android

I want to show first item as a default text on spinner but in the drop down list that item is not shown like there is arraylist
final ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("number");
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
The number is always shown as a default text on spinner but after clicking on spinner there is one, two, three is shown in drop down list. when we select any number one, two three the default text number is not changed. How can i do it?
ArrayAdapter<String> adapte = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinnerArray);
adapte.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapte);
spin.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View arg1,int pos, long arg3)
{
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Try this, it show the default as Number..
May be this help you
String[] priceString = { "Number", "one, "two"};
Spinner priceSpinner = (Spinner) findViewById(R.id.price_spinner);
ArrayAdapter priceAdapter = new ArrayAdapter( this.getActivity(), android.R.layout.simple_spinner_item,priceString); priceSpinner.setAdapter(priceAdapter);

Selection on second spinner not working

I am developing a page with 2 spinners, where the first spinner has some category populated from DB.
Based On the value selected from spinner1, the selectedCategoryName is passed in the method setReportNamesContent().
I am also getting the values populated in the second spinner, but I am unable to do the selection in the 2nd spinner.
My idea is to pass both these selected values on click of a button, but always the 1st value from the 2nd spinner is getting passed on click of the button.
protected void setReportCategory(String loginName) {
map = DBManager.getInstance().reportCategoryListByLoggedInUser(loginName);
reportCategoryList = new ArrayList<String>(map.keySet());
Collections.sort(reportCategoryList);
reportCategorySpinnner = (Spinner) findViewById(R.id.ReportCategoryList);
reportCategorySpinnner.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, reportCategoryList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
reportCategorySpinnner.setAdapter(dataAdapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(parent.getId()){
case R.id.ReportCategoryList:
selectedCategoryName = parent.getItemAtPosition(position).toString();
break;
case R.id.ReportNameList:
selectedReportName = parent.getItemAtPosition(position).toString();
break;
}
if(!isEmptyOrNull(selectedCategoryName))
setReportNamesContent(selectedCategoryName);
}
private void setReportNamesContent(String selectedCategoryName) {
reportNameList = map.get(selectedCategoryName);
Collections.sort(reportNameList);
reportNameSpinnner = (Spinner) findViewById(R.id.ReportNameList);
reportNameSpinnner.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, reportNameList);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
reportNameSpinnner.setAdapter(dataAdapter1);
}
I had already reffered various question in this site and tried all of them and are not working. Please advice for a working answer

How to add more items at run time in spinner

if we has a some items in the spinner means (like below) how shall we add extra items while run time
here is code:
String[] Items = {
"Alarm",
"Office",
"Meeting",
"Party",
"Lunch",
"Breakfast",
"Supper",
"Home",
"Private",
"Outdoor",
"Family",
"Friends",
"Others"
};
Spinner s1;
and
s1 = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item,Items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3)
{
int index = s1.getSelectedItemPosition();
Toast.makeText(getBaseContext(),
"You have selected item : " + Items[index],
Toast.LENGTH_SHORT).show();
if (index==12)
{
EditText edit = (EditText) findViewById(R.id.edittext);
Button add=(Button) findViewById(R.id.add);
edit.setVisibility(View.VISIBLE);
add.setVisibility(View.VISIBLE);
}
else
{
EditText edit = (EditText) findViewById(R.id.edittext);
Button add=(Button) findViewById(R.id.add);
edit.setVisibility(View.GONE);
add.setVisibility(View.GONE);
}
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
In it if select the "other" means we ve to add extra items in spinner list..
thank you,
I hope you can detect the case when the user has selected "others"
in that case, add the other values in your Items array and call the function adapter.notifyDataSetChanged() it should accommodate the changes and you should see the new values in the spinner.
try this out and revert.
You can create a BaseAdapter, fill it in runtime and assign the adapter to the spinner.
Following works for me:
/* Read values from resource into an array */
String[] strColorValues = getResources().getStringArray(R.array.colors);
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < strColorValues.length; i++) {
list.add(strColorValues[i]);
}
ArrayAdapter adapterColors = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, list);
adapterColors.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerColors.setAdapter(adapterPermissionLevels);
spinnerColors.setOnItemSelectedListener(this);
/* Remove first element from the adapter and notify dataset changed. */
String item = spinnerColors.getItemAtPosition(0).toString();
adapterColors.remove(item);
adapterColors.notifyDataSetChanged();

Categories

Resources