问题
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);
}
}
}
回答1:
Something like this should work:
int id = resources.getIdentifier(String.valueOf(i), "id", "com.my.package");
n = (TextView) findViewById(id);
Android docs
回答2:
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.
回答3:
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);
.
来源:https://stackoverflow.com/questions/27385604/android-findviewbyid-use-int-at-id-field