-2

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

  • What's wrong!! Everything. This is not how its done. I guess you need to learn Java before Android. Anyways see my answer, that is what you need to do. – Rohit5k2 Jan 25 '15 at 02:47

2 Answers2

0
// initialization for TextView
TextView txtt = (TextView) findViewById(R.id.myTextViewId);
// set the text
txtt.setText(getResources().getString(R.string.mystring));
Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87
0

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.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57