Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Spinner not hiding the dropdown menu after selection

(Android API version 9)I created a spinner with a custom adapter and overrided getView() to inflate it with my xml file which has a text view. But now, my spinner is not closing the dropdown list after user selects an item. Is there anyway to close the spinner dropdown upon selecting an item?

Code

//Code in onCreate function
    Spinner list = (Spinner) findViewById(R.id.spn_purchaseList);
    listAdapter = new ItemListAdapter(this, new MyItemList());
    list.setAdapter(listAdapter);
    listAdapter.item_list.addItem(new MyItem("Test", "Test Item"));
    listAdapter.notifyDataSetChanged();
//onCreate end
//the class below is inside "MainActivity extends Activity"
class ItemListAdapter extends BaseAdapter
{
    Context context;
    MyItemList item_list;
    MyItem selectedItem;


    ItemListAdapter(Context con,MyItemList k)
    {
        super();
        this.context=con;
        this.item_list=k;
        selectedItem=null;
    }

    @Override
    public int getCount() {

        return item_list.getCount();
    }

    @Override
    public MyItem getItem(int arg0) {

        return this.item_list.getList().get(arg0);
    }

    @Override
    public long getItemId(int arg0) {

        return  this.item_list.getPosition(this.item_list.getList().get(arg0));
    }

    @Override
    public View getView(int position, View arg1, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        View spinner_item = inflater.inflate(R.layout.spinner_layout, parent, false);

        TextView tx = (TextView)spinner_item.findViewById(R.id.txt_spinner);
        tx.setId((int) item_list.getPosition(item_list.getList().get(position)));


        tx.setText(this.item_list.getList().get(position).name.toString());
        tx.setBackgroundResource(R.drawable.spinner_item);

        tx.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                selectedItem = item_list.getItem(v.getId());
                list.setSelection(v.getId());



            }
        });

        return spinner_item;
    }

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


        return getView(position,convertView,parent);

    }

}
like image 498
Sachin Murali G Avatar asked Nov 11 '22 17:11

Sachin Murali G


1 Answers

Calling setVisibility(View.GONE) works to hide the dropdown but it seems to cause issues with the Spinner state, i.e. you will be unable to reopen the dropdown after it has been closed.

The preferred way is to get a handle to the Spinner and call its onDetachedFromWindow() from your onClick() listener.

@Override
public void onClick(View v) {
    // code here to get selected item and do something with it

    // hide the spinner dropdown
    Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
    if (spinner != null) {
        try {
            Method method = Spinner.class.getDeclaredMethod("onDetachedFromWindow");
            method.setAccessible(true);
            method.invoke(spinner);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
like image 102
Matt Avatar answered Nov 15 '22 04:11

Matt