Multiple dropdownlist's items clicks in Spinner - android

When U click on selected item in dropdown spinner's list spinner dont call onItemSelected method. I need to do multiple spinner's items clicks so I try to create my own spinner. Help me pls to do this thing. Maybe I need to #Override some methods in AdapterView or something.

Try this
import android.content.Context;
import android.widget.Spinner;
public class MySpinner extends Spinner {
public MySpinner(Context context) {
super(context);
}
#Override
public void setSelection(int position, boolean animate) {
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position, animate);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
#Override
public void setSelection(int position) {
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
}

Related

stopping Spinner ItemSelected from firing on SetSelection

I want to turn off getting onItemSelectedListener when I do
spinInsuranceList.setSelection(previousSpinnerPos);
I already made custom class, but now I want to override onItemSelected and make same method with my own extra parameter which help me to stop fire event.
Please Note: I already implemented spinner and it is working fine, so because of onItemSelected custom impl, my exisiting work should not effect.
Code:
CustomSpinnerClass: (I made this custom class, because that time I wanted to click on same item which is selected previously)
import android.content.Context;
import android.util.AttributeSet;
public class CustomSpinnerClass extends androidx.appcompat.widget.AppCompatSpinner {
public CustomSpinnerClass(Context context) {
super(context);
}
public CustomSpinnerClass(Context context, int mode) {
super(context, mode);
}
public CustomSpinnerClass(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void setSelection(int position, boolean animate) {
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position, animate);
if (sameSelected) {
//TODO:-> Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
#Override
public void setSelection(int position) {
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position);
if (sameSelected) {
//TODO:-> Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
}
I found this solution, but don't know how to use:
private Spinner.OnItemSelectedListener spinnerListener
= new Spinner.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
/* This function is called when you select something from the spinner */
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
/**This line sets the index of the spinner.
Here I have given it as position which
can be any number with in the index range ***/
my_spinner.setSelection(position);
}};
Please read, if you don't understood my requirement: https://forums.xamarin.com/discussion/6350/help-stopping-spinner-itemselected-from-firing-on-setselection
When I want to set something without firing the listener what I do is
spinner.setSelectionListener(null);
spinner.setSelection(20);
spinner.setSelectionListener(this);

Spinner on item click functionality to be add

I have a spinner in my android app which is used to call an api to change the data in list view bellow it. I have used OnItemSelectedListener for this, but when I click the same item again, nothing happens, unless I change it to some other item and again click on the item I needed. I want to use something similar to OnItemClick, as OnItemClick is not supported by spinner. Suggest me an alternative, please.
Took from this answer. Unfortunately, android Spinner doesn't provide functionality, that you want, but you can extend it and handle clicks manually:
public class NDSpinner extends Spinner {
public NDSpinner(Context context) {
super(context);
}
public NDSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NDSpinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void
setSelection(int position, boolean animate) {
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position, animate);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
#Override
public void setSelection ( int position){
boolean sameSelected = position == getSelectedItemPosition();
super.setSelection(position);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
}
}

How to update Activity UI based on ListView item selection?

I am creating a sample application containing an Activity [having ListView and Button] in layout file. ListView is custom containing [Label/Name and CheckBox]. I want to write some code which will change text of the Button from adapter class of ListView based on List Item CheckBox check [T/F].
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// When clicked, show a toast with the TextView text
AppListOfAllApps Selecteditems = (AppListOfAllApps) parent.getItemAtPosition(position);
if (view != null)
{
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
Selecteditems = (AppListOfAllApps) checkBox.getTag();
//here you will get the selected item you may also get the text from it accordingly and then using using button variable just set text
button.settext("whatever");
}
}
});
In Activity :
public class Your_Activity extends Activity implements OnCheckListener// Implement your listener here
#Override
public void OnCheck(int position) {
// TODO Auto-generated method stub
// notify your activity component here
}
In Adapter class :
private OnCheckListener listener;
public interface OnCheckListener {
public void OnCheck(int position);
}
public Your_adapter_constructor(OnCheckListener listener) {
// TODO Auto-generated constructor stub
this.listener = listener;
}
// On your getView()
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
listener.OnCheck(position);// If you want to pass some value add it here
}
});

Android custom listview adapter to stay highlighted selected item

I have a problem with my listview. What I want to do is when I click an item in the listview, it should remain highlighted, and when I click another item, new item should be highlighted.
Here's what I've done so far:
public void setSelection(int position) {
if (selectedPos == position) {
selectedPos = NOT_SELECTED;
} else {
selectedPos = position;
}
notifyDataSetChanged();
}
Adapter GetView:
if (position == selectedPos) {
// your color for selected item
viewHolder.tvTitle.setTextColor(context.getResources().getColor(R.color.main_blue));
viewHolder.tvDetails.setTextColor(context.getResources().getColor(R.color.main_blue));
}
else {
// your color for non-selected item
viewHolder.tvTitle.setTextColor(context.getResources().getColor(R.color.dark_grey));
viewHolder.tvDetails.setTextColor(context.getResources().getColor(R.color.dark_grey));
}
Activity:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
episodesAdapter.setSelected(position);
}
});
This is working as expected, but the problem is with notifyDataSetChanged, it's delaying the time to highlight item that is being selected next. What should I do to automatically get it highlighted? Any help would be truly appreciated. Thanks!
Update:
I tested and it took 8-10secs before a new item is highlighted. There is a slight delay in highlighting an item.

Why is OnItemSelectedListener only called when an item changes, but not on every user selection?

I'm using spinner controls in an Android application, and I handle user selections via an onItemSelectedListener() method. This seems to work okay when a different selection from the current one is made. I would like, under certain conditions to reset all spinners to default values and ensure that onItemSelectedListener() is called for all.
Is it part of Android's semantics that onItemSelectedListener() is only called when user selection changes. Is there a way to force onItemSelectedListener() to be called?
If you want "onItemSelected" of Spinner to be fired, even if the item in the spinner is selected /if item selected and it is clicked again .
Then use this custom class which extends spinner ,this worked for me
Then edit your activity with spinners like this , I changed
static Spinner spinner1;
to
static NDSpinner spinner1;
and
variables.spinner1 = (Spinner) findViewById(R.id.spinner1);
to
variables.spinner1 = (NDSpinner ) findViewById(R.id.spinner1);
Also I changed the xml layout where the spinner is located
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/place" />
to
<com.yourpackagename.NDSpinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/place" />
Spinner extension class:
package com.yourpackagename;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Spinner;
import android.widget.Toast;
import java.lang.reflect.Field;
/** Spinner extension that calls onItemSelected even when the selection is the same as its previous value.
* ie This is extended "Customized class of Spinner" to get the "onItemSelected" event even if the item in the
* Spinner is already selected by the user*/
public class NDSpinner extends Spinner {
public NDSpinner(Context context)
{ super(context); }
public NDSpinner(Context context, AttributeSet attrs)
{ super(context, attrs); }
public NDSpinner(Context context, AttributeSet attrs, int defStyle)
{ super(context, attrs, defStyle); }
private void ignoreOldSelectionByReflection() {
try {
Class<?> c = this.getClass().getSuperclass().getSuperclass().getSuperclass();
Field reqField = c.getDeclaredField("mOldSelectedPosition");
reqField.setAccessible(true);
reqField.setInt(this, -1);
} catch (Exception e) {
Log.d("Exception Private", "ex", e);
// TODO: handle exception
}
}
#Override
public void setSelection(int position, boolean animate)
{
boolean sameSelected = position == getSelectedItemPosition();
ignoreOldSelectionByReflection();
super.setSelection(position, animate);
if (sameSelected) {
// Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now
getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId());
}
}
#Override
public void setSelection(int position) {
ignoreOldSelectionByReflection();
super.setSelection(position);
}
}
The default Spinner doesn't trigger any event when you select the same item as your currently selected item. You will need to make a custom Spinner in order to do this. See How can I get an event in Android Spinner when the current selected item is selected again?
Add the same adapter every time if you want to do it using default spinner :
#Override
public void onItemSelected(AdapterView adapter, View v, int position, long lng) {
spinner.setAdapter(adapter);
}

Categories

Resources