0

I have a spinner and an adapter.

Spinner spinner = findViewById(R.id.spinner);
SpinnerAdapter adapter = new SpinnerAdapter(this, R.layout.spinner_item, getList());
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
    {
        Log.e("PROGRAM", "Selected |" + position + "|");
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView)
    {
    }
});

This is my SpinnerAdapter dropdown creation

@Override
public View getDropDownView(final int position, final View convertView, final ViewGroup parent)
{
    MyObject object = getItem(position);

    View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_object_item, parent, false);
    ((TextView) layout.findViewById(R.id.name)).setText(object.getName());

    return layout;
}

And my_object_item xml is

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/item"
              android:clickable="true"
              android:focusable="true">

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/icon"/>

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/name"/>

</LinearLayout>

For some reason I can not get setOnItemSelectedListener to trigger. Should I be adding listener to every component of my_object_item and from there triggering setOnItemSelectedListener?

Quillion
  • 6,346
  • 11
  • 60
  • 97

1 Answers1

1

As you have suggested above, you can try to do things manually, by adding a listener that listens to clicks on either the TextView or ImageView (the components of each item in the spinner dropdown view), and then update the spinner view according to what was selected.

1) MainActivity.class:------------

public class MainActivity extends AppCompatActivity implements Listener {

private Spinner sp;
private SpinnerAdapter spinnerAdapter;

private String[] name = {
        "N1",
        "N2",
        "N3",
        "N4"
};

private int[] icon = {
        R.drawable.icon,
        R.drawable.icon,
        R.drawable.icon,
        R.drawable.icon
};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    sp = (Spinner) findViewById(R.id.sp);
    spinnerAdapter = new SpinnerAdapter(getApplicationContext() , name , icon , name[0] , icon[0] , MainActivity.this); // TODO: Every time the activity (spinner) is created, the item selected is changed
    //TODO to the initial name[0] and icon[0]. In order to solve this issue you can save the selected item (name and icon) value in sharedPreferences and use the saved value every time the spinner is opened.
    sp.setAdapter(spinnerAdapter);
}

@Override
public void clicked(int position) {
    if(spinnerAdapter != null){
        spinnerAdapter.setCurrentNameIcon(name[position] , icon[position]);
    }
}

2) SpinnerAdapter.class:---------------

public class SpinnerAdapter extends BaseAdapter {

private Context mContext;
private LayoutInflater layoutInflater;
private Listener callback;
private String current_name;
private int current_icon;

private String[] name;
private int[] icon;


public SpinnerAdapter(@NonNull Context context , String[] name_ , int[] icon_ , String inital_name_ , int initial_icon_ , Listener l) {

    mContext = context;
    layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(l != null){
        callback = l;
    }

    name = name_;
    icon = icon_;

    current_name = inital_name_;
    current_icon = initial_icon_;

}

public int getCount() {
    return name.length;
}

public Object getItem(int position) {
    return name[position];
}

public long getItemId(int position) {
    return position;
}

@NonNull
public View getView(final int position, View convertView, ViewGroup parent) {

    View view = convertView;
    final TextView tv;
    ImageView iv;

    if (convertView == null) {
        if (layoutInflater != null) {
            view = layoutInflater.inflate(R.layout.my_object_item, parent , false);
        }
    }

    tv = (TextView) view.findViewById(R.id.name);
    tv.setText(current_name);


    iv = (ImageView) view.findViewById(R.id.icon);
    iv.setBackground(mContext.getResources().getDrawable(current_icon));


    return view;
}

@Override
public View getDropDownView(final int position, @Nullable View convertView, @NonNull final ViewGroup parent) {

    View view = convertView;
    final TextView tv;
    ImageView iv;

    if (convertView == null) {
        if (layoutInflater != null) {
            view = layoutInflater.inflate(R.layout.my_object_item, parent , false);
        }
    }

    tv = (TextView) view.findViewById(R.id.name);
    tv.setText(name[position]);
    tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(callback != null){

                //https://stackoverflow.com/questions/7287195/android-spinner-close?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
                View root = parent.getRootView();
                root.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
                root.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));

                callback.clicked(position);
            }
        }
    });


    iv = (ImageView) view.findViewById(R.id.icon);
    iv.setBackground(mContext.getResources().getDrawable(icon[position]));
    iv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(callback != null){

                //https://stackoverflow.com/questions/7287195/android-spinner-close?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
                View root = parent.getRootView();
                root.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
                root.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));

                callback.clicked(position);
            }
        }
    });

    return view;

}


public void setCurrentNameIcon(String name , int icon){
    current_name = name;
    current_icon = icon;
    this.notifyDataSetChanged();
}
}

3) Listener interface:------------

public interface Listener {

public void clicked(int position);
}

4) main_activity.xml:---------

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/sp">
</Spinner>

</android.support.constraint.ConstraintLayout>

5) my_object_item.xml:----------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/item"
android:clickable="true"
android:focusable="true">

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@drawable/icon"
    android:layout_gravity="center_vertical"
    android:id="@+id/icon"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="center_vertical"
    android:gravity="center"
    android:text="HI"
    android:id="@+id/name"/>

</LinearLayout>
Brainnovo
  • 1,749
  • 2
  • 12
  • 17