Android dynamic String Resources

前端 未结 4 1350
北恋
北恋 2020-12-09 23:03

in my app I do a web request that returns some result code, e.g. 105. I have string resources that look like that

O.K.

        
相关标签:
4条回答
  • 2020-12-09 23:34

    A simple way to getting resource ID from string. Here resourceName is the name of resource ImageView in drawable folder which is included in XML file as well.Im getting "id" you can use "string".

    int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
    ImageView im = (ImageView) findViewById(resID);
    Context context = im.getContext();
    int id = context.getResources().getIdentifier(resourceName, "drawable",
    context.getPackageName());
    im.setImageResource(id);
    
    0 讨论(0)
  • 2020-12-09 23:43

    Try this getResources().getIdentifier(name, defType, defPackage) in a simple way.

    Toast.makeText(this, getResources().getIdentifier("r"+resultcode, "string", 
    getPackageName()), Toast.LENGTH_LONG).show();
    
    0 讨论(0)
  • 2020-12-09 23:53

    Try as below, its working for me. Use parent.getApplicationContext() for you.

    String str = getString(R.string.r)+resultCode;
    
            Toast.makeText(getApplicationContext(),
                    str, Toast.LENGTH_LONG).show();
    
    0 讨论(0)
  • 2020-12-10 00:00

    You can do it using getResources().getIdentifier(name, defType, defPackage). Something like this:

    // Assuming resultCode is an int, use %s for String
    int id = getResources().getIdentifier(String.format("r%d", resultCode), 
                                          "string", getPackageName());
    String result = getString(id);
    
    0 讨论(0)
提交回复
热议问题