2013-06-26 80 views
0

我在使用list前綴時遇到了問題。我想一個列表PREF值匹配另一個值與if語句List Pref trouble android

我敢打賭,沒有道理,所以我會張貼代碼...

stripedMain:

String stripeType = ""; 
void drawStriped() { 
      final SurfaceHolder holder = getSurfaceHolder(); 
      Canvas c = null; 
      try { 
       c = holder.lockCanvas(); 
       if (c != null) { 
        if (stripeType == "vert") 
        { 
        android.util.Log.d("stripedLog", "working");  
         } 
       } 
      } finally { 
       if (c != null) 
        holder.unlockCanvasAndPost(c); 
      } 
............. 

的settings.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 

    <PreferenceCategory 
     android:key="firKey" 
     android:title="Wallpaper Settings" > 

     <ListPreference 
      android:defaultValue="hor" 
      android:entries="@array/stripe_entries" 
      android:entryValues="@array/stripe_values" 
      android:key="stripe_type" 
      android:title="Type" /> 

    </PreferenceCategory> 

</PreferenceScreen> 

shapes.xml:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> 

    <string-array name="stripe_entries"> 
     <item>"Horizontal"</item> 
     <item>"Vertical"</item> 
     <item>"Both"</item> 
      <item>"Both+CrossOver"</item> 
    </string-array> 

    <string-array name="stripe_values"> 
     <item>"hor"</item> 
     <item>"vert"</item> 
     <item>"both"</item> 
     <item>"bothOver"</item> 
    </string-array> 

</resources> 

這不是工作,我不知道爲什麼....

回答

0

它不工作怎麼把玉還沒有獲取的默認共享偏好將它與一個字符串比較

,所以你必須做的就是獲取它然後像這樣比較:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 

    String stripeType = prefs.getString("stripe_type", "stripe_type") 


    void drawStriped() { 
       final SurfaceHolder holder = getSurfaceHolder(); 
       Canvas c = null; 
       try { 
        c = holder.lockCanvas(); 
        if (c != null) { 
         if (stripeType.equalsIgnoreCase("vert")) 
         { 
         android.util.Log.d("stripedLog", "working");  
          } 
        } 
       } finally { 
        if (c != null) 
         holder.unlockCanvasAndPost(c); 
       } 
+0

謝謝你,你的答案並不是真正的現貨,但它確實幫助我找出了問題。概率是,我沒有這樣做:if(stripeType.equalsIgnoreCase(「vert」)) –

+0

好吧現在你的代碼開始工作? –