android findviewbyid() use int at id field

孤人 提交于 2019-12-12 01:59:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!