2013-12-18 26 views
2

有沒有辦法讓下面的腳本更高效? 我想讓這個代碼易於維護,這就是爲什麼我想擺脫if if else。 我希望你們能幫助我。在底部是我想看到的東西,如果它可能的話。爲R.color使用字符串。「string」

 if (category.equals("infusion")){ 
     layout.setBackgroundResource(R.color.infusion); 
      title.setText(R.string.title_infusion);    
    } else if (category.equals("pills")){ 
      layout.setBackgroundResource(R.color.pills); 
      title.setText(R.string.title_pills); 
    } else if (category.equals("pumps")){ 
      layout.setBackgroundResource(R.color.pumps); 
      title.setText(R.string.title_pumps); 
    } else if (category.equals("oxygen")){ 
      layout.setBackgroundResource(R.color.oxygen); 
      title.setText(R.string.title_oxygen); 
    } else if (category.equals("scores")){ 
      layout.setBackgroundResource(R.color.scores); 
      title.setText(R.string.title_scores); 
    } else if (category.equals("converters")){ 
      layout.setBackgroundResource(R.color.converters); 
      title.setText(R.string.title_converters); 
    } 

是這樣的嗎?

layout.setBackgroundResource(R.color.*category*); 
title.setText(R.string.title_*category*); 

回答

3

我敢肯定,所有的事情,你會做的「簡化」這將涉及反思,並有可能結束了使你的代碼慢,更難理解。你所擁有的是完成這種設置的完全有效的方式,對讀者來說非常清楚,並且不涉及任何奇怪的技術。

I.E.這有效,爲什麼修復它?

好吧,編輯:

在啓動時,你可以映射你的字符串值通過哈希映射資源ID。

喜歡的東西:

HashMap map = new HashMap(); 
map.put("infusion",R.id.infusion); 

再後來就:

layout.setBackgroundResource(map.get(category)); 
title.setText(category); 

威力工作,但同樣它不是真正國際海事組織的改善。

+2

如果不破 – JoeC

2

退房Resources.getIndentifier()可以像一個輔助函數:

public static int resourceNameToId(Context context, String name, String resType) { 
    if (name != null && name.length() > 0) { 
     return context.getResources().getIdentifier(name, resType, context.getPackageName()); 
    } 

    return 0; 
} 

然後使用它:

layout.setBackgroundResource(resourceNameToId(getContext(), category, "color")); 
+0

它,我也應該說,這在內部使用反射,應輕輕 – FunkTheMonk

+0

使用從文檔: 注:使用此功能被勸阻。通過標識符而不是按名稱檢索資源要高效得多。 –

+0

是的,我不會建議添加適配器,但它對於動態生成的內容非常有用。另外通過高效的方式,我認爲OP對可維護性更感興趣 – FunkTheMonk

0

你不能用你的代碼結構實現這一目標。

使用類似:

layout.setBackgroundResource(getColorByCategory(category)); 
title.setText(getCategoryTitle(category)); 

其中getColorByCategory()getCategoryTitle()是你自己的功能。

0

有兩種方法我可以從頭頂上思考。哈希映射和或使用資源對象。

A有地圖將包括一個小設置,就像這樣:

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

static { 
    COLOURS.put("pills", R.color.red); 
    COLOURS.put("pumps", R.color.green); 
} 

那麼它很容易稍後設置顏色:

layout.setBackgroundColor(COLOURS.get(category)); 

您可能需要爲null檢查色彩來如果您擁有的數據不能保證完美,請在設置它們之前從地圖中移出。

或者,您可以像這樣使用Resources對象。

Resources resources = context.getResources(); 
resources.getColor(resources.get("colour_" + category, "color", context.getPackageName())); 
2

你可以使用一個枚舉像這樣把你的價值觀和顏色:

public enum ColorValue { 

InFusion(android.R.color.black), 
Pills(android.R.color.white); 

int color; 

ColorValue(int Value) { 
    color = Value; 
} 

public int getColorResource() { 
    return color; 
} 

} 

然後訪問與此類似

ColorValue x=ColorValue.InFusion; 
x.getColorResource(); 
0

枚舉值,您可以使用一個枚舉做這更有效:

(在你的班級裏)申報

public enum Category { 
    infusion (R.color.infusion, R.string.title_infusion, "infusion"), 
    pills (R.color.pills, R.string.title_pills, "pills") //, 
    //etc... comma-seperated values. Those are handled like any other enum 
    ; 
    public final int relatedBackground; 
    public final String relatedTitle; 
    public final String identifier; //To keep the identifiers you used previously 

    private Category (int back, String title, String id) { 
     this.relatedBackground = back; 
     this.relatedTitle = title; 
     this.identifier = id; 
    } 
} 

而且你的方法是這樣的:

public void foo(Category cat) { //You pass in your enum type 
    //Do what you want 
    if(cat != null) { //Avoid NullPointerException if necessary 
     layout.setBackgroundResource(cat.relatedBackground); 
     title.setText(cat.relatedTitle); 
    } 
} 

這種方法是解決你有,因爲你可以保持項目問題增加了新的價值到一個偉大的方式逗號分隔的列表。你也不需要擔心不得不每次從某個表查找值

缺點是,它是硬/ 不可能運行期間調整枚舉。作爲一個枚舉所有變量都必須最終作爲enumtype如果你想這樣做,你將不得不使用一個外部表本身是不可修改的,你不能做這樣的事情

cat.relatedbackground = someValue; // Can't do that: all fields are final 

。除此之外,它是一個優秀的解決方案

相關問題