txtt.setText(getString(R.string.(“i” + j++)));

隐身守侯 提交于 2019-12-08 14:18:05

问题


What's wrong with this code? I want to use id automatically. I think after R.string there is a mistake. What can ı do


回答1:


Do it like this

public static int getStringIDFromName(String stringName)
{
    int stringID= 0;

    if(stringName == null
            || stringName.equalsIgnoreCase(""))
    {
        return 0;
    }

    try
    {
        @SuppressWarnings("rawtypes")
        Class res = R.string.class;
        Field field = res.getField(stringName);
        stringID = field.getInt(null);
    }
    catch(Exception e)
    {
        // Error
    }

    return stringID;
}

Set your value like this

int stringVal = getStringIDFromName("i" + j++);
if( stringVal != 0)
    txtt.setText(getResource().getString(stringVal));

This would work only if you are doing everything else right.




回答2:


// initialization for TextView
TextView txtt = (TextView) findViewById(R.id.myTextViewId);
// set the text
txtt.setText(getResources().getString(R.string.mystring));


来源:https://stackoverflow.com/questions/28132214/txtt-settextgetstringr-string-i-j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!