在层演示器 MVP 上使用资源 "R.String" 中的字符串

Using Strings from resource "R.String" on layer presenter MVP

在 MVP 模式中,我的字符串具有动态值 %

示例:

<string name="hello">%s hello</string>

并且我需要在我的文本视图上使用 "my name" 设置此文本,我将如何在我的演示层上直接引用 R.String 来执行此操作。

public void onItemClicked(String name) {
        if (mainView != null) {
             //HOW use R.string.hello from Strings  here? [presenter layer]
            mainView.showMessage(String.format("%s hello", name));
        }
    }

在 MVP 模式上,我无法在演示层中引用 Android class,我在此 class 中没有任何上下文,但我需要使用 R.string.hello ,因为翻译,我怎么能接受这个 without 毁了这个 MVP 模式

getString() 有一个重载版本,它采用可变参数进行格式化。

快速回答:你不知道

你构建你的代码,所以你的视图方法是:

@Override
public void showMessage(String name){

    if (mTextView != null){
        mTextView.setText(String.format(getString(R.string.hello), name));
    }
}

那么您的演示者代码是:

public void onItemClicked(String name) {
    if (mainView != null) {
        mainView.showMessage(name);
    }
}

MVP 是关于干净的可测试代码的,在这种情况下,您想要在演示者中测试的只是演示者将正确的名称传递给视图。您不需要测试 String.format() 或从资源中获取字符串(其他开发人员已经这样做了,即 Android 开发人员)。我建议也许更深入地了解为什么 MVP 将使您的项目受益