0

I want to display messages based on selection of value from spinner. so i used folowing code.

sp=(Spinner)findViewById(R.id.spinner1);
        String ar[]={"hello","abc"};
        ArrayAdapter<String> adapt=new ArrayAdapter<String>(SpinnerEampleActivity.this, android.R.layout.simple_spinner_dropdown_item,ar);
        sp.setAdapter(adapt);
        sp.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) 
        {
            if(arg2==0)
            {
            AlertDialog.Builder alert=new AlertDialog.Builder(SpinnerEampleActivity.this);
            alert.setMessage("hello");
            alert.show();
            }
            else
            {
                AlertDialog.Builder alert=new AlertDialog.Builder(SpinnerEampleActivity.this);
                alert.setMessage("abc");
                alert.show();
            }

        }

but if i select the already selected value from spinner i am not getting any message. could any help me to solve the problem. Thanks in advance.

krishna
  • 4,069
  • 2
  • 29
  • 56
  • As alternative case you can use single choice dialog and aggregate selected item, instead spinner – Yahor10 Jan 29 '13 at 10:15
  • Try this link [Custom Spinner][1] [1]: http://stackoverflow.com/questions/13182284/why-is-onitemselectedlistener-only-called-when-an-item-changes-but-not-on-every/18693760#18693760 This worked for me – Jerry Abraham Sep 09 '13 at 08:04

3 Answers3

0

When you clicks again the currently selected item, then it can not fire any event. So you can not catch setOnItemSelectedListener for spinner to respond.

You need to create your custom spinner adapter to handle the click for the already selected item.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • i tried inflating a textview to spinner and added onclicklistener to it.i gives my requirement.but when the option is selected,the dropdown options were not hiding. – krishna Jan 29 '13 at 08:10
0

I tried the following to get custom spinner

ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, selects)
                    {
                       @Override
                       public View getDropDownView(int position, View convertView, ViewGroup parent)
                       {
                        View v = convertView;
                         if (v == null) 
                           {
                             Context mContext = this.getContext();
                             LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                              v = vi.inflate(R.layout.spinner_item, null);
                           } 
                         final TextView tv=(TextView) v.findViewById(R.id.custopspin);
                         int pos=position;
                         if(pos==0)
                         {
                             tv.setText("hello");
//                           sy
                         }
                         else if(pos==1)
                         {
                             tv.setText("abc");
                         }
                         else
                         {
                             tv.setText("hi");
                         }
                         tv.setTextColor(Color.RED);
                         tv.setClickable(true);
                         tv.setOnClickListener(new OnClickListener() 
                         {

                        @Override
                        public void onClick(View arg0) 
                        {
                            if(tv.getText().toString().equals("hello"))
                            {
                                                                    }
                            else if(tv.getText().toString().equals("hi"))
                            {

                            }
                            else
                            {
                            }
                        }
                     });    
                     return v;  
                    }              
                 };     
        timeslot.setAdapter(spinnerAdapter);

as said in above comment after selecting the option were not hiding.

krishna
  • 4,069
  • 2
  • 29
  • 56
0
   sp=(Spinner)findViewById(R.id.spinner1);
    String ar[]={"hello","abc"};
    ArrayAdapter<String> adapt=new ArrayAdapter<String>(SpinnerEampleActivity.this, android.R.layout.simple_spinner_dropdown_item,ar);
    sp.setAdapter(adapt);
    sp.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int position, long arg3) 
    {
 AlertDialog.Builder alert=new AlertDialog.Builder(SpinnerEampleActivity.this);
        alert.setMessage(ar[position]);
        alert.show();

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

That's all.........

Vinoj Vetha
  • 726
  • 6
  • 8