I have problem in listening user interaction of the spinner when item selected.
I know the below listener work perfect
spinner.setOnItemSelectedListener(this);
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
but In my application scenario I will change the spinner value through program,that time also it fire the onitemselected event.but I need this event should fired only when user interaction happened on the spinner
Note:I am not talking on initial spinner value selected item.
Please Let me know how can I accomplish this
Thanks in advance,
Naveenkumar.R
tempListener = spinner.getOnItemSelectedListener();
spinner.setOnItemSelectedListener(null);
//change the spinner value...
spinner.setOnItemSelectedListener(tempListener)
More elegantly, you could subclass the Spinner class, and create 'disableOnItemSelectedListener()' and 'enableOnItemSelectedListener()' methods, which do the same as the code above.
Undesired calls to onItemSelected can be avoided using a simple technique I describe in the following link. This way, you can yourself setSelection anytime without a worry. Check the accepted answer to this question:
Undesired onItemSelected calls
Related
I want to develop an Android app that should run on devices with an external keyboard. The user should be able to go through a form using the enter key. Now, I have an issue with a Spinner and a Button.
The only way (I found) to transfer the focus from the Spinner to the Button is to use an setOnItemSelectedListener:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
root.findViewById(R.id.button).requestFocus();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {}
});
This works when the user selects another item from the Spinner than the current value. However, it doesn't work when the user opens the dialog (with entering), then doesn't select another value (but presses enter to confirm the current choice). I guess the OnItemSelected-Event isn't triggered when the value doesn't change.
Does anyone have a clue how I could implement that?
As the onItemSelected in the spinner is not called if you don't change the selection then you would have to do something like this
Force the user to select something from the spinner by adding a default value to the spinner like "Please select". Thus you could also show some error message if he doesn't select and eventually your onItemSelected listener would get invoked.
I ended up by using the solution from this answer: (Which extends the Spinner-class)
How can I get an event in Android Spinner when the current selected item is selected again?
Then, it is possible to shift the focus to the next item with
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
root.findViewById(R.id.Button).requestFocus();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
[update] I fix the mistake I made. now the spinner can update dynamically. But every time I launch this new activity, the spinner will be empty, how can I keep the old data when the activity be created again?
I have an EditView, a Button, and a spinner. When the user types in an item in EditView that is not in the spinner, they can click the button to add to the spinner for the next time to save time.
I search for some solutions in StackOverflow and google, but after I click the button, it still not work, the data still not add to the spinner. Here is the part of the code:
purchasedProduct=(EditText)findViewById(R.id.purchasedP);
spinner=(Spinner)findViewById(R.id.spinner);
spinnerBtn=(Button)findViewById(R.id.purchasedDBtn);
items =new ArrayList<String>();
items.add("");
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinnerBtn.setOnClickListener(spinnerBtnListener);
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
purchasedProduct.setText(adapterView.getSelectedItem().toString());
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
private Button.OnClickListener spinnerBtnListener = new Button.OnClickListener(){
#Override
public void onClick(View view) {
String listItem = purchasedProduct.getText().toString();
//check if already there
for(int i = 0;i < adapter.getCount();i++)
{
if(listItem.equals(adapter.getItem(i)))
return;
}
//check if type in is empty
if(!listItem.equals(""))
{
adapter.clear();
items.add(listItem);
adapter.addAll(items);
adapter.notifyDataSetChanged();
}
}
};
Can anyone tell me how to fix it? thank you!
You need to store new data in SQLite database. On start activity you just get data from database. All data in database save on device. See how to use SQLite in Android Studio.
Also you can try store data in XML JSON or text file, but this is bad idea.
You can use Room Persistence Library, Look Here for details.
This library helps you create a cache of your app's data on a device that's running your app. This cache, which serves as your app's single source of truth, allows users to view a consistent copy of key information within your app, regardless of whether users have an internet connection.
I have an issue which I just cannot get my head around. In my experience, code follows the sequence it is run in, but this does not appear to be the case in this instance.
I have two UI items, namely a spinner and an edittext field which are populated from a DB table in a function called from onCreate(). When the spinner is set (using setSelection) it kicks off the onItemSelected which sets the edittext value to a default value. After this, I then set the edittext field (using setText) to the value stored in the DB, but the new value does not appear.
Here is a snippet of the code:
spinnerdresslength.setSelection(backuplengthoptionid);
edittextskirtshortestlength.setText(backupshortestlength);
The event handler looks like as follows (I have simplified it for the sake of ease):
spinnerdresslength.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
edittextskirtshortestlength.setText("43");
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});`
Now despite the edittextskirtshortestlength.setText(backupshortestlength) following the spinner setSelection, the edittext shows 43 instead of backupshortestlength.
Can you anybody assist with this as I am at my wits end?
Thanks,
NicVegas
I'm trying to programmatically click on an item of a recyclerView. I'm using:
recyclerView.findViewHolderForAdapterPosition(index).itemView.performClick();
This perfectly works when the index is of a visible item. If the item is not visible (at the last position of the recyclerview, for istance), an Exception is thrown.
What can I do?
I just had a similar question with yours.And I had solve it! Here is what I did.
xxx.mRecyclerView.postDelayed(new Runnable() {
#Override
public void run() {
xx.mRecyclerView.scrollToPosition(position);
}
},300);
xxx.mRecyclerView.postDelayed(new Runnable() {
#Override
public void run() {
xxx.mRecyclerView.findViewHolderForAdapterPosition(position).itemView.performClick();
}
},400);
}
You can scroll to the specific item, then perform click.
Because the doc say
If the item at the given position is not laid out, it will not create a new one.
But I know the adapter has the data, so scroll to it first, and findViewHolderForAdapterPositionwill not be null.
One more thing, I do not know how you use the RecyclerView. In my application, I use it in a fragment, and I don not know why we should delay it scroll and perform click. (Maybe it is because of the life circle?).But this really works.
You could call onClick directly, assuming that view manages its own click listener.
View view = recyclerView.findViewHolderForAdapterPosition(index).itemView;
view.onClick(view);
If the click listener is located somewhere else, you just need to get a reference to the object with the onClick method and call it with the correct view as a parameter.
try this for kotlin and viewBinding
viewBinding.catList.post {
val view = viewBinding.catList.findViewHolderForAdapterPosition(0)?.itemView?.findViewById<CheckBox>(R.id.catButton)
view?.callOnClick()
}
I have problem with android. I'm adding data from adapter to spinner, then I set the selection on item on position 0, after that I create OnItemSelectedListener and it is triggered but I don't know why...
onCreate method:
this.spinner = (Spinner)findViewById(R.id.spinner);
this.spinner.setAdapter(MyAdapter.GetAdapter());
this.spinner.setSelection(0);
this.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do some things
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Maybe here is something wrong, I also try to remove the listener, change the selection and then add listener to it. Maybe I do sth wrong here:
AdapterView.OnItemSelectedListener listener = this.spinner.getOnItemSelectedListener();
this.spinner.setOnItemSelectedListener(null);
this.spinner.setSelection(((SpinnerAdapter) this.spinner.getAdapter()).getElementIndexByTitle(array.get("ProductName")));
listener.onItemSelected(null, null, -1, -1);
this.spinner.setOnItemSelectedListener(listener);
It seems that it triggers the event also after: this.spinner.setOnItemSelectedListener(listener); Maybe is there possibility to select item without triggering OnItemSelected ?
I found out that inside the AdapterView there is a SelectionNotifier and it wokrs automatically. So I need to remember last selection index and then compare it with position in OnItemSelected.