2011-06-08 52 views
0

我有枚舉與例外數量和說明。我將異常編號傳遞給MessageHandler。 然後我想處理它們並顯示帶有錯誤描述的用戶消息。 在不使用開關構造的情況下對結果進行迭代的最佳方式是什麼?android枚舉迭代無開關

我有幾個解決方案,但我不知道哪個更適合android。

謝謝。

+0

開關有什麼問題? – 2011-06-08 07:45:30

+0

我想保持我的價值觀這樣的: ALL_OK \t \t(0, 「沒有錯誤」), SOME_ERROR \t(1, 「不明錯誤發生」), CONNECTION_ERROR(2, 「連接」), 保持一切包裝。 然後根據數字獲取值 – saikek 2011-06-08 07:47:04

回答

0
import java.util.HashMap; 
import java.util.Map; 

import android.content.Context; 
import android.content.res.Resources; 

import com.cyberneticscore.voliastatis.R; 

public enum ExceptionEnums { 
NO_ERROR(1), 
CONNECTION_ERROR(2), 
LOGIN_ERROR  (3), 
SSL_ERROR  (4), 
WRONG_DATE  (5), 
INTERRUPTED  (6), 
CLIENT_PROTOCOL_ERROR(7), 
UNKNOWN_HOST_ERROR(8), 
IO_ERROR  (9), 
XPATHER_ERROR(10); 

private static final Map<Integer, String> map = new HashMap<Integer, String>(); 

private static Context context; 
private static Resources reso; 

public static void getContext(Context _context) 
{ 
    context =_context; 
    reso = context.getResources(); 
} 

private int pos; 

public static void putMapValues(){ 
//static{ 
    map.put(NO_ERROR.getPos(), reso.getString(R.string.exceptions_no_error)); 
    map.put(CONNECTION_ERROR.getPos(), reso.getString(R.string.exceptions_connection_error)); 
    map.put(LOGIN_ERROR.getPos(), reso.getString(R.string.exceptions_login_error)); 
    map.put(SSL_ERROR.getPos(), reso.getString(R.string.exceptions_ssl_error)); 
    map.put(WRONG_DATE.getPos(), reso.getString(R.string.exceptions_wrong_date)); 
    map.put(INTERRUPTED.getPos(), reso.getString(R.string.exceptions_interrupted)); 
    map.put(CLIENT_PROTOCOL_ERROR.getPos(), reso.getString(R.string.exceptions_client_protocol_error)); 
    map.put(UNKNOWN_HOST_ERROR.getPos(), reso.getString(R.string.exceptions_unknown_host_error)); 
    map.put(IO_ERROR.getPos(), reso.getString(R.string.exceptions_io_error)); 
    map.put(XPATHER_ERROR.getPos(), reso.getString(R.string.exceptions_xpather_error)); 
} 

ExceptionEnums(int pos){ 
    this.pos = pos; 
} 

public int getPos(){ 
    return pos; 
} 

public static String getErrorDescription(int position_number){ 
    return map.get(position_number); 
} 
} 
1

實際上,有做到這一點,並做到在不依賴於特定enum的方式相當直接的方式。當想要使用enum來填充UI的不同部分時,我遇到了這個問題。

下面是示例代碼一點點告訴你我是如何做的:

public int enumPosition(Enum<?> lookingFor, Enum<?>[] lookingIn) 
{ 
    for (int i = 0; i < lookingIn.length; i++) 
    { 
     if (lookingIn[i].getValue().equals(lookingFor.getValue())) 
     { 
      //Found Answer 
     } 
    } 
} 

當然,這取決於你的需要有好的方法來設置你的enum,以便更好地訪問和搜索,這是我的意思:

public enum ExampleEnum 
{ 
firstValue("#1", 1), 
secondValue("#2", 2), 
thirdValue("#3", 3); 

private final String id; 
private final int somethingUseful; 

ExampleEnum(String id, int usefulValue) 
{ 
    this.id = id; 
    this.somethingUseful = usefulValue; 
} 

public String getValue() { return id; } 
public int getSomethingUseful() { return somethingUseful; } 

public static ExampleEnumfromString(String text) 
{ 
    if (text != null) 
    { 
     for (ExampleEnumt : ExampleEnum.values()) 
     { 
      if (text.equalsIgnoreCase(t.id)) 
      { 
        return t; 
      } 
     } 
    } 

    //Usually best to throw an exception here 
    return ExampleEnum.firstValue; 
} 


//Useful for passing to adapters 
public static String[] getValues() 
{ 
    String[] vals = new String[ExampleEnum.values().length]; 
    int i = 0; 
    for (ExampleEnumt : ExampleEnum.values()) 
    { 
     vals[i] = t.getValue(); 
     i++; 
    } 
    return vals; 
} 

@Override 
public String toString() 
{ 
    return getValue(); 
} 
}