user3780144
user3780144

Reputation: 15

android findviewbyid() use int at id field

I have a pretty simple question:

int i=0;
n = (TextView) findViewById(R.id. "value of i" );

How can I get this working? I want in the place of id to use my int value, is it possible? if so how do I go about doing this?

I'll put the code:

private void sxhmatismos7(String[][] pinakas)
{

    TextView n;
    int i=0;
    for (i=0;i<12;i++)
    {

        n = (TextView) findViewById(R.id."HERE VALUE OF i");
        if (n.getText().equals(pinakas[0][0]))
        {
            n.setVisibility(View.VISIBLE);

        }

    }


}

Upvotes: 0

Views: 3600

Answers (3)

Coeffect
Coeffect

Reputation: 8866

Something like this should work:

int id = resources.getIdentifier(String.valueOf(i), "id", "com.my.package");
n = (TextView) findViewById(id);

Android docs

Upvotes: 3

Sarthak Mittal
Sarthak Mittal

Reputation: 6104

There is only one way, you will have to create the TextView Dynamically and add it to your layout, there is a method called:

view.setId(int i);

here you can set the id of your view and can access it.

Upvotes: 1

Eli Rising
Eli Rising

Reputation: 485

You don't use it that way. R.id.value is a Resource Id, typically from your layouts.

R.id.value is a public final int from the R.java file. The Id number is generated by your editor and is something like -11222388. There is almost never a time to not use code that looks like (View) findViewById(R.id.itemId);.

Upvotes: 0

Related Questions