1

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

例子:

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

我需要在我的文本视图上用“我的名字”设置这个文本,我将如何在没有引用的情况下在我的演示者层上直接引用 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 类,我在此类中没有任何上下文,但我需要使用 R.string.hello,因为翻译,我怎么能在没有这方面破坏这个 MVP 模式

4

2 回答 2

3

快速回答:你没有

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

@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 将使您的项目受益

于 2017-02-21T23:21:23.600 回答
0

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

于 2017-02-21T22:54:51.187 回答