Set spinner selected item text - android

I have a spinner (only relevant parts of code)...
protected void onCreate(Bundle savedInstanceState) {
Spinner to_spinner = (Spinner) findViewById(R.id.to_spinner);
List<Unit> list = myDbHelper.getAllUnits();
SpinnerUnitAdapter tUnitAdapter tUnitAdapter = new SpinnerUnitAdapter(this, android.R.layout.simple_spinner_item, list);
to_spinner.setAdapter(tUnitAdapter);
to_spinner.setOnItemSelectedListener(onItemSelectedListenerTo);
}
with an onItemSelectedListener
AdapterView.OnItemSelectedListener onItemSelectedListenerTo = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
// do stuff
}
#Override
public void onNothingSelected(AdapterView<?> adapter) {
}
};
When an item is selected (i.e. where it says: "do stuff"), I would like to set/change the text of the selected spinner item. (Note that this is not the same as setting the spinner position (with setSelection()).
I was thinking of doing this with
tUnitAdapter.getView(position, ?, ?).setText("new text");
Am I on the right track? What to put as second ("convertView") and third ("parent") argument in getView. My spinner adapter looks like:
public class SpinnerUnitAdapter extends ArrayAdapter<Unit> {
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView label = new TextView(mContext);
label.setTextColor(Color.BLACK);
label.setTextSize(mContext.getResources().getDimension(R.dimen.list_row_font_size));
label.setGravity(Gravity.CENTER);
label.setText(getItem(position).getName());
return label;
}
}

Am I on the right track?
No. You should do the following steps: (in the onItemSelected method)
Update your model (the array of items you passed to the adapter) so that the item at position index takes the new name.
Issue notifyDataSetChanged on the adapter object. Alternatively, you can do this manually by ((TextView) view).setText(new_name);
Note: In onItemSelected method, adapterView points to your spinner view and view points to the row view just selected.
UPDATE #1
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
((TextView) view).setText("new name");
}
UPDATE #2
For this, you should use Java interface to implement a callback which is called once the dialog is closed.
public void onItemSelected(AdapterView<?> adapterView, final View view, int position, long id) {
Dialog dialog = new MyDialog(context, new MyDialog.OnItemSelectListener(){
#Override
public void onItemSelected(String newName){
((TextView) view).setText(newName);
}
});
dialog.show();
}
And declare interface OnItemSelectListener in your MyDialog class.

You can change the text of spinner when you click each item so you should implement onClickListener
public class SpinnerUnitAdapter extends ArrayAdapter<Unit> {
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView label = new TextView(mContext);
...
label.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// change label text here
// label.setText(...);
}
});
return label;
}
}
Hope this help

I think I found the solution. I can just do:
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
TextView tv = (TextView) adapterView.getSelectedView();
tv.setText("new text");
}

Related

view of spinner doesn't gets updated

I am using spinners which are dependent on one another. So when i choose one value in a spinner A the arraylist of other spinners B,C,D change accordingly.
when I select item at position 1 on spinner B and then choose any other value in spinner A The data on spinner B gets updated. So I again choose item at position 1 on spinner B the view doesn't changes while selecting item at any other position changes the view. So the same index doesn't get updated value in this piece of code.
ArrayAdapter<String> myAdapterEmployer = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,employerlist);
myAdapterEmployer.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
employer.setAdapter(myAdapterEmployer);
employer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
if(position > 0) {
employer1 = employer.getSelectedItem().toString();
employeridname = employdetail.get(employer1);
Sectorlist(employeridname);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
This changes data of Sectorlist everytime a item is selected in employer.
And then any item is choosen in sectorlist.
ArrayAdapter<String> myAdaptersector = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,sectorlist);
myAdaptersector.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sector.setAdapter(myAdaptersector);
sector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
sector1=sector.getSelectedItem().toString();
String selectedsectortext = (String) parent.getItemAtPosition(position);
if(position > 0){
sectoridd=sectordetail.get(selectedsectortext);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Here when i choose item at same index as before it doesn't update in the spinner view.
Inside your onItemSelected of the spinner A you can set Adapter to other spinners to be update and this will work as you want.
employer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
if(position > 0) {
employer1 = employer.getSelectedItem().toString();
employeridname = employdetail.get(employer1);
Sectorlist(employeridname);
// Update other spinners here
myAdaptersector = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,sectorlist);
myAdaptersector.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sector.setAdapter(myAdaptersector);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

spinner cannot be applied to (anonymous android.widget.AdapterView.onitemselectedlistener)

i have to select items from spinner. Actually i am making gpa calculator.I have given grades in spinner and person can select from that. I have to assign grades with like A will 4, B+ will be 3.5 and so on. That's why i have to extract value after user has selected from spinner. But i am getting that spinner cannot be applied to adapter view.
setonitemclicklistener (android.widget.AdapterView.onitemclicklistener) in spinner cannot be applied to (anonymous android.widget.AdapterView.onitemselectedlistener)
you can do :
Spinner spinner = new Spinner(getApplicationContext());
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
android.util.Log.d(TAG, "position: " + position);
}
});
hope this will help you.
You should use setOnItemSelectedListenerto do something on an item selected like below:
yourSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do Something
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

How do I know the position of selected item in spinner?

I am inflating Spinner along with that I am also inflating some other views
LinearLayout child_linear = (LinearLayout) findViewById(R.id.child_linear);
LayoutInflater inflater = getLayoutInflater();
View myView = inflater.inflate(R.layout.add_custome_layout, child_linear, false);
name = (TextView) myView.findViewById(R.id.name);
price = (TextView) myView.findViewById(R.id.add_price);
names_spinner = (Spinner) myView.findViewById(R.id.product_addmore_spinner);
child_linear.addView(myView);
Now I getting data for the spinner. The question if there are 'n' no.of spinners how do I know which spinner item user selected?
names_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position).toString();
if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("Select Toppings")) {
//Show PopUp or Alerts
Toast.makeText(ProductView.this, "position" + names_spinner.getItemIdAtPosition
(position), Toast.LENGTH_SHORT).show(); }
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Even when I click on particular spinner item position is also not showing in Toast. What's my mistake?
You are showing toast only when user click on Select Toppings. Take a look to your code:
if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("Select Toppings")) {
Change this line for
if (!parent.getItemAtPosition(position).toString().equalsIgnoreCase("Select Toppings")) {
And alerts should be shown.
Additionally to know which item is clicked, the listener provided you the clicked position, you can search in you items array this position and you can know which item is.
EDIT:
Now, lets suppose you have more than one spinner. spinner1 and spinner2, for example. You could register an OnItemSelectedListener for each one.
Then you can create a global variable called Spinner clickedSpinner and inside the OnItemSelectedListener of the spinner1 you set the clickedSpinner to spinner1 and inside the OnItemSelectedListener of the spinner2 set the clickedSpinner to spinner2:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
clickedSpinner = spinner1;
... // Do your stuff here
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
And:
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
clickedSpinner = spinner2;
... // Do your stuff here
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
So, after clicking any item of any Spinner you can now know which Spinner was clicked because is stored in clickedSpinner.
Please note that this variable is not mandatory, you could achieve the same behavior without this global variable, because in each OnItemSelectedListener you know which spinner is the clicked. So, you could do the following:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int spinner1Position = spinner1.getItemIdAtPosition(position);
... // Do your stuff here
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
And:
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int spinner2Position = spinner2.getItemIdAtPosition(position);
... // Do your stuff here
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Without using a global variable.
Hope this helps.
Try this
final Spinner names_spinner = (Spinner) myView.findViewById(R.id.product_addmore_spinner);
ArrayAdapter<String> madapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.yourarray));
madapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
names_spinner.setAdapter(madapter);
names_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String mySpinner = (String) adapterView.getItemAtPosition(i);
Log.i("names_spinner",mySpinner);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
Toast.makeText(getApplicationContext(),"Your Message",Toast.LENGTH_LONG).show();
}
});
}

Cannot get current spinner position with setOnItemSelectedListener

I am trying to retrieve the spinner's selected position. The values inside the spinner are of type String and I am using setOnItemSelectedListener to change a class String variable to the one that has been selected and use it later on:
spinner = (MaterialBetterSpinner) myToolbar.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,socialMediaOptions);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
socialMedia = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
private void sendPost(){
Log.v(TAG, socialMedia);
}
The problem is that the variable does not change when an item is selected, it seems like onItemSelected is not being called when there is a change in the spinner selection
See this thread:
https://github.com/Lesilva/BetterSpinner/issues/42
There said that BetterSpinner extends EditText, that's why onItemSelectedListener is not being called. You should use TextChangedListener instead.
Hope your socialMediaOptions are string array like this
socialMediaOptions={"FB","TWIT","IN"}
then
// Specify the layout to use when the list of choices appears
add this line
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
socialMedia = socialMediaOptions[position];
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

How do I change a list item text for a ListFragment/ArrayAdapter?

I have a fragment which extends ListFragment.
In it I use an ArrayAdapter
public class MyFragment extends ListFragment {
public void onStart() {
super.onStart();
List<String> list = new ArrayList<String>();
list.add("Superman");
list.add("Batman");
arrayAdapter = new ArrayAdapter<OptionToStringAdapter>(
getActivity(),
android.R.layout.simple_list_item_1,
list);
getListView().setAdapter(arrayAdapter);
getListView().setOnItemClickListener(new ModifyOption(this));
setListShown(true);
}
When the user taps an item I want to modify the text.
How do I modify the text?
public class ModifyOption implements OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
???What goes here????
}
}
Assuming you want to change the content of the list item you clicked.
First, change the value using a dialog or widget of your choice.
Second, change the value of the item at the position in the list by using 'position' argument.
Third, call notifyDataSetChanged() method of the adapter object.
public class ModifyOption implements OnItemClickListener {
public void onItemClick(AdapterView parent, View view, int position, long id) {
// create a dialog or other widget
String newValue = "your_new_value";
list.set(position,newValue);
arrayAdapter.notifyDataSetChanged(); //informs views that data has been changed
}
}
try with below code:
public class ModifyOption implements OnItemClickListener { public void onItemClick(AdapterView parent, View view, int position, long id) {
arrayAdapter.remove(arrayAdapter.getItem(position));
arrayAdapter.insert("new text",position);
} }
This seems to do the trick. I don't know if this is an appropriate way though.
public void onItemClick(AdapterView parent, View view, int position, long id) {
TextView textview = (TextView) view.findViewById(android.R.id.text1);
textview.setText("Spiderman");
}

Categories

Resources