0

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);

        }

    }


}
Phil3992
  • 1,059
  • 6
  • 21
  • 45
user3780144
  • 15
  • 1
  • 7

3 Answers3

2

Something like this should work:

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

Android docs

Coeffect
  • 8,772
  • 2
  • 27
  • 42
1

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.

Sarthak Mittal
  • 5,794
  • 2
  • 24
  • 44
  • So if i create it dynamically i can access it later? could you give me an example? i updated the code – user3780144 Dec 09 '14 at 18:11
  • check this link out, the guy with most upvotes has shown how to add a button to the linear layout programmatically, you just need to set an id to the view too: http://stackoverflow.com/questions/2395769/how-to-programmatically-add-views-to-views – Sarthak Mittal Dec 09 '14 at 18:16
0

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);.

Eli Rising
  • 485
  • 1
  • 3
  • 15
  • So if i have a textview with id=1, i still cant use this? Is there a way to scroll through my entire layout's ids? – user3780144 Dec 09 '14 at 18:03
  • It is very bad practice. Your textview Id could change, since it is typically generated by your editor when it builds (usually your project builds when you save), and appears to be based on alphabetical order of your other resource Ids. As an example, one of my Id resources from R.java looks like: `public static int front=0x7f060002;`, which is an extremely large hex value. By the way, you could not use that value in your project. – Eli Rising Dec 09 '14 at 18:07
  • Sarthak has a snippet for you that can set the Id to a number, but this only works if you have correctly found the View first, which you haven't shown you know how to do yet. You cannot put a string value after `R.id.`, it is expecting a variable name from your R.java file. – Eli Rising Dec 09 '14 at 18:15