2012-11-01 11 views
7

मैं एंड्रॉइड एप्लिकेशन में स्पिनर नियंत्रण का उपयोग कर रहा हूं, और मैं उपयोगकर्ता चयन को onItemSelectedListener() विधि के माध्यम से संभालता हूं। यह ठीक काम करता है जब वर्तमान से एक अलग चयन किया जाता है। मैं चाहता हूं कि, कुछ स्थितियों के तहत सभी स्पिनरों को डिफ़ॉल्ट मानों पर रीसेट करने और यह सुनिश्चित करने के लिए कि onItemSelectedListener() सभी के लिए बुलाया गया है।OnItemSlectedListener केवल तभी बुलाया जाता है जब कोई आइटम बदलता है, लेकिन प्रत्येक उपयोगकर्ता चयन पर नहीं?

क्या यह एंड्रॉइड के अर्थशास्त्र का हिस्सा है कि onItemSelectedListener() केवल उपयोगकर्ता चयन में परिवर्तन होने पर ही बुलाया जाता है। क्या onItemSelectedListener() को कॉल करने के लिए मजबूर करने का कोई तरीका है?

+0

प्रतीक्षा, आप कैसे की _opposite_ वर्णित एक स्पिनर काम करता है। जब आप OnItemClickListener का उपयोग करते हैं और OnItemSlectedListener को _regardless_ कहा जाता है, तो उपयोगकर्ता ने पिछले चयन को फिर से चुना है या एक नई पसंद बनाई है ... – Sam

+0

प्रतिक्रिया के लिए धन्यवाद, मैंने गलत वर्तनी की है। मैं इटैम सेलेक्टेड लिस्टेमर() पर उपयोग कर रहा हूं। मैंने अब अपना संदेश संपादित कर लिया है। – user1400716

उत्तर

3

डिफ़ॉल्ट स्पिनर किसी भी ईवेंट को ट्रिगर नहीं करता है जब आप अपने वर्तमान में चुने गए आइटम के समान आइटम चुनते हैं। ऐसा करने के लिए आपको एक कस्टम स्पिनर बनाना होगा। देखें How can I get an event in Android Spinner when the current selected item is selected again?

+0

आपकी मदद के लिए धन्यवाद। यह एक अच्छा समाधान की तरह दिखता है। – user1400716

11

यदि आप स्पिनर के "इटिम चयन" को निकालना चाहते हैं, भले ही स्पिनर में आइटम चुना गया हो/यदि आइटम चुना गया है और इसे फिर से क्लिक किया गया है। फिर, जो स्पिनर फैली इस कस्टम वर्ग का उपयोग यह मेरे लिए काम किया

तो इस तरह स्पिनरों के साथ अपनी गतिविधि को संपादित, मैं

static Spinner spinner1; 

बदल

static NDSpinner spinner1; 

और

variables.spinner1 = (Spinner) findViewById(R.id.spinner1); 
को

से

variables.spinner1 = (NDSpinner) findViewById(R.id.spinner1); 

इसके अलावा, मैं XML लेआउट जहां स्पिनर स्थित है बदल

<Spinner 
    android:id="@+id/spinner1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/place" /> 

को
<com.yourpackagename.NDSpinner 
    android:id="@+id/spinner1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/place" /> 

स्पिनर विस्तार वर्ग:

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); 
     } 

    } 
संबंधित मुद्दे