Spinner, not set value if i dont click(android) - android

I have a spinner:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int childposition, long id) {
textView.setText(spinner.getSelectedItem().toString());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
textView.setText("");
}
});
Above you'll see that textView - is my text object. I'm displaying a text item spinner in the textView when I click it. If I dont click the spinner then my textView must be textView.setText("");
But the spinner is always set text in my textView, even if I do not choose spinner.
Question
How can I accomplish this?:
If I dont choose item spinner, textView is empty: textView.setText("");
If I do choose the item spinner, textView gets: textView.setText(spinner.getSelectedItem().toString());

String item = parent.getItemAtPosition(childposition).toString(); //Get selected item
if(item.equals("spinner")){ // Check if it equals spinner
textView.setText(item); // Set text to item
}else{
textView.setText(""); // If it doesn't equal spinner set text to ""
}
If I understood the question right, putting this instead of textView.setText(spinner.getSelectedItem().toString()); and deleting content of onNothingSelected should do the trick.
UPDATE
I finally understood what you mean. To do this create your spinner like this and add "" as first choice in your string-array resource :
String[] newArray = getResources().getStringArray(R.array.yourArray);
List<String> myResArrayList = Arrays.asList(newArray);
ArrayList<String> spinnerItems = new ArrayList<String>(myResArrayList);
//Making adapter with ArrayList instead of String[] allows us to add/remove items later in the code
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, spinnerItems);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinnerItems.remove(0);
adapter.notifyDataSetChanged(); // Here we remove the first choice which is "" so the user won't be able to select empty.

Related

spinner value updation by another spinner which uses autocomplete text view not working

I have two spinners in which the first spinner uses autocomplete textview , when first spinner item is selected in normal way without using autocomplete text view the second spinner loads the corresponding value according to the first spinners selection. My issue is that when autocomplete textview is used for selecting first spinner value the second spinner is not loaded with the values according to first spinners selection. can any one please tell me a solution
public void vesselspinnerHandler(){
//Setting Class to Spinner
final ArrayAdapter<String> vesselspinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, vessellist);
vesselspinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
vesselspinner.setAdapter(vesselspinnerAdapter);
vesselspinnerAdapter.notifyDataSetChanged();
final AutoCompleteTextView searchvessel=findViewById(R.id.searchvessel);
final AutoCompleteTextView selves = findViewById(R.id.selectedvessel);
ArrayAdapter adapter1 = new ArrayAdapter(selection.this,android.R.layout.simple_spinner_dropdown_item,vessellist);
selves.setAdapter(adapter1);
selves.setDropDownVerticalOffset(50);
vesselspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selves.setText(vesselspinner.getSelectedItem().toString());
searchvessel.setText(selves.getText().toString());
getdate();//second spinner call
return;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}

finding the array index value for the selected android spinner item

during registration user can select the titleName from a android Spinner. Im loading data to the Spinner from xml class. I want to pass the titleName as well as title ID,
How can I get the value of selected titleName's array index?
eg- if user selected Mr then i want to pass the value 0.
my java code
states = getResources().getStringArray(R.array.title_array);
titleSP = (Spinner) findViewById(R.id.registerTitle);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, states);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
titleSP.setAdapter(dataAdapter);
final Button button = (Button) findViewById(R.id.btnRegister);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
String TitleName = titleSP.getSelectedItem().toString();
MY title.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title_prompt">Choose the title</string>
<string-array name="title_array">
<item>Mr</item>
<item>Mrs</item>
<item>Miss</item>
<item>Dr</item>
<item>Rev</item>
<item>Ms</item>
</string-array>
</resources>
it the position parameter in your Adapter. You can retrieve it implementing OnItemClickListener. The third parameter of onItemClick
is the value you are looking for
Use the Spinners OnItemClickListener
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String[] titles = getResources().getStringArray(R.id.title_array);
String selectedTitle = titles[position];
}
});
The position variable gives the position of the item in the spinner, which you can match directly to the position of the title in the data source.
Edit:
You can use the built in method of a spinner to get the current selected item position. So if you select the first item, you would get selected item position 0. As this is based on your data source, selected item position 0, would be the same as index position 0 in R.id.title_array which would be "Mr".
titleSP.getSelectedItemPosition();

how to open edit text on specific item is selected from spinner

I build a spinner in which i put element through array list and .i store the value of get selcted item from spinner in a variable,in my spinner it having 15 element,now i want to try when a specific item is selected from spinner then a dialog box is open with edit text and button then user can edit and save it in spinner.how can i do this.
my code for spinner is
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.faultid);
addItemsOnSpinner2();
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection()
{
Spinner faultSpinner = (Spinner) findViewById(R.id.spinner1);
mspinner.setOnItemSelectedListener(new
CustomOnItemSelectedListener());
}
public void addItemsOnSpinner2() {
ArrayList<String> faulttypespinner = new ArrayList<String>();
faulttypespinner.add("XL-Cross Level");
faulttypespinner.add("AL-Alignment");
faulttypespinner.add("UN-Unevenness");
faulttypespinner.add("XL-Cross Level");
faulttypespinner.add("AL-Alignment");
faulttypespinner.add("UN-Unevenness");
faulttypespinner.add("BD-Ballast Deficiency");
faulttypespinner.add("SE-Super elevation on curve");
faulttypespinner.add("LP-Loose Packing");
faulttypespinner.add("LJ-Low Joint");
faulttypespinner.add("BA-Bridge");
faulttypespinner.add("LC-Level Crossing");
faulttypespinner.add("LJ-Low Joint");
faulttypespinner.add("P and C-Point n Xing");
faulttypespinner.add("OTH-Other Defect");
faulttypespinner.add("SEJ-SEJ");
faulttypespinner.add("WEED-Weed on Cess");
ArrayAdapter<String> faultadapter = new ArrayAdapter<String>
(mConetxt,android.R.layout.simple_spinner_item, faulttypespinner);
faultadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mspinner.setAdapter(faultadapter);
}
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " +
parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
You'll need to implement Custom Adapter, where you'll have a method something like :
public void changeItemValue(int position, String newValue){
Item itm = getItem(position);
itm.setValue(newValue);
notifyDataSetChanged();
}
When you open the dialogBox onSelectedItem, you'll have to pass an instance of your spinner adapter to a dialog and then to the listener of a dialog button, where you'll call changeItemValue method on button click.
Why u r using custom adapter,when click on spinner get the position of clicked item and show the pop up and on button click set the value to the faulttypespinner arraylist using index stored previously. then add notifydatasetchanged for adapter to update the spinner thats all.

how to change data already data in spinner

I have four Spinner ,First spinner display data when user select on spinner item data then other spinner data is displayed .First time,i have loaded all data in spinner when user select first spinner then data should be changed to refresh to second Spinner
How data is changed in second spinner
First you would need to get a reference of your spinner like -
Spinner mySpinner = (Spinner) findViewById(R.id.mySpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.myArray, android.R.layout.mySpinnerItem);
adapter.setDropDownViewResource(android.R.layout.myDropdownItem);
spinner.setAdapter(adapter);
To change the values you would do -
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//change content
}
}
public void onNothingSelected(AdapterView parent) {
//do nothing
}
}
Set a listener on the setOnItemSelectedListener of your first Spinner, with the appropriate code to populate your second Spinner. This way when you change the value of the first Spinner, the second Spinner will be updated.

Sorting ListBiew by Spinner in android?

I have one ListView and one Spinner. The Spinner is situated at the right top of the layout. My ListView contains some names (eg. country names) and the spinner has some alphabets (a-z). Suppose if I choose the letter "f" from the Spinner, my ListView must show the country names that starts only with the letter "f" . I want to sort my ListView by values from the Spinner?
You need to implement a Filter for your ListView. Everytime the OnItemSelectedListener of your Spinner gets called you need to filter the items. If you're not sure how to implement a filter in your ListView's adapter have a look at this: Filtering ListView with custom (object) adapter
I guess there won't be any way around implementing an own ListAdapter (it's easy).
Try to use the getFilter() method of the ListView adapter:
String[] filterL = { "a", "b", "c" }; //etc
//...
Spinner spin = (Spinner) findViewById(R.id.spinner1);
final ArrayAdapter<String> aspin = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, filterL); //the adapter for the Spinner
aspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aspin);
final ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, countries); //the adapter for the list
setListAdapter(aa); //set the adapter for the list(if you extend LisActivity) or call setAdapter on the ListView element
//add the listener:
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
boolean status = false;
public void onItemSelected(AdapterView<?> parent, View view,
int position, long arg3) {
if (!status) {
status = true;
return;
}
aa.getFilter().filter(filterL[position]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});

Categories

Resources