mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// my code here, but I don't know how..
}
}
I think it will be something like this:
get the selected item position of mySpinner
switch(pos)
Update spinner entries? I don't know...
I'm not sure what to do for the last step. Help?
try in this way
car.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
itemSelected = arg2;
// add items to the cardetails spinner's adapter using the itemselected and refresh the adapter using nofifyDataSetChanged()
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
cardetails.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
here car and cardetails are spinners
you need to refresh the adapter that is related to the car spinner so that the spinner also gets refreshed
Related
I have two autocompletetextview
the goal for example if any one select the autocompletetextview "HUB ID " position#3 then the another autocompletetextview " HUBName" must be forced to be on position 3
i.e want the both of them at the same position
// HUB Name listener
HubName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
HubName.setSelection(pos);
Log.d("ID Array follow pos","HUB ID in selected");
HubID.setSelection(pos);
HubName.setSelection(pos);
ShltrArray.clear();
ShltrArray.add(Shltr1Array.get(pos).toString());
if (Shltr2Array.get(pos).toString() != "Not")
{
ShltrArray.add(Shltr2Array.get(pos).toString());
}
ShltrAdapt.notifyDataSetChanged();
}
});
HubName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
HubName.setSelection(position);
HubID.setSelection(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
HubName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
HubName.showDropDown();
Log.d("ID Array follow ID ", Integer.toString(HubID.getListSelection()));
Log.d("ID Array follow Name ", Integer.toString(HubName.getListSelection()));
}
});
the solution to index the array list to the same position of the first array then inside autocompleteTextView listener then set text to this position as follow
HubName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
HubID.setText(IDArray.get(pos).toString());}
Is there a way that I can get the previously selected item from a ItemSelected event on a spinner? My first thought was to respond to the OnItemClickedEvent, but that method is not supported for spinners.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
MyItem current = (MyItem) parent.getItemAtPosition(position);
//MyItem previous = (MyItem) parent.getPrevious ... how
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
//do nothing
}
});
I'm aware I can just cache the previous value, but it seems like a common operation that there should be a method for.
Use a variable to track the previous item. It would likely be at member of the Activity.
MyItem previous, current;
...
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
previous = current
current = (MyItem) parent.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
//do nothing
}
});
spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
int prev = comboValues.getSelectedItemPosition();
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
prev=position;
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
}
);
Note that nothing happens if you select an item you have already selected
I'm trying retrieve an object from my spinner array and set it to a certain constant, in this case "EFFECT_AQUA"
My array
String[] spinnerValues = {"Aqua", "Mono", "Blackbird", "Negative"};
when the user clicks on the "Aqua" in the spinner I want the screen to change to Aqua.
My spinner is set and called
Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
mySpinner.setAdapter(new MyAdapter(this,R.layout.custom_spinner,spinnerValues));
But not sure how should I approach. Seen many different answers but haven't found anything working.
I know my switch will come in this part
class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
}
Any help is appricated!!
If you want to retrieve an object from your spinner array on item click, you can easily do it with the position parameter in the method onItemSelected.
For example :-
class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String itemSelected = spinnerValues[position];
//Set this to a constant
}
}
Now if you select Aqua, then code will set the variable itemSelected to Aqua.
I hope this is what you need
final Spinner cardStatusSpinner1 = (Spinner)findViewById(R.id.text_interested);
String cardStatusString;
cardStatusSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
cardStatusString = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
put the code below to get the item first then you can manipulate by taking the values like aqua or any color.
spinner = (Spinner)findViewById(R.id.spinner1);
spinner.setAdapter(new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1,spinnerValues));
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView)view;
Toast.makeText(getApplicationContext(), tv.getText().toString(), 5000).show();
switch(tv.getText().toString()){
case "Aqua":{
//change the color to aqua
break;
}
case " ...":{
//
break;
}
//.... for all the option
}
}
I'm having a ListFragment with a list, that does not get single clicks. But long clicks are recognised.
ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i(null, "single click does NOT work.");
}
});
// contextual action bar (CAB).
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i(null, "does work.");
}
});
I had a similar problem and was resolved by invalidating the listview's views when created and after scroll
listView.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
if ( scrollState == OnScrollListener.SCROLL_STATE_IDLE )
{
listView.invalidateViews();
}
}
#Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {}
});
I hope this helps
Make your activity extends from Activity and not ListActivity or something like that ;)
I am using spinner that shows error when i am trying to extract the item id of the selected spinner item.
My Code goes here:
public void dispspi()
{
spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter <String> adap= new ArrayAdapter(this, android.R.layout.simple_spinner_item, level);
spinner.setAdapter(adap);
spinner.setOnItemClickListener(new OnItemClickListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
int item = spinner.getSelectedItemPosition();
p=item;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
How to get the item id of the spinner? Any help is appreciated..Thanks in advance
IIRC, you should be using a selected listener, not click:
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
Then you can add the override tag to your selected method.
private String selecteditem;
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
selecteditem = adapter.getItemAtPosition(i).toString();
//or this can be also right: selecteditem = level[i];
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
}
});
spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v,
int postion, long arg3) {
// TODO Auto-generated method stub
String SpinerValue3 = parent.getItemAtPosition(postion).toString();
Toast.makeText(getBaseContext(),
"You have selected 222 : " + SpinerValue3,
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Yes you can use some OnItemSelectedListener for work with selected item. But sometimes we would like to handle exactly click for spinner. For example hide keyboard or send some analytics etc. In this case we should use TouchListener because OnClickListener doesn't work properly with Spinner and you can get error. So I suggest to use TouchListener like:
someSpinner.setOnTouchListener { _, event -> onTouchSomeSpinner(event)}
fun onTouchSomeSpinner(event: MotionEvent): Boolean {
if(event.action == MotionEvent.ACTION_UP) {
view.hideKeyBoard()
...
}
return false
}
you should have this in the listener(OnItemSelectedListener)
public void onNothingSelected(AdapterView<?> arg0) {
}
It might works without it but put it to be consistent
but there might be other errors also, can you provide the error log ?