是否有可能通过id循环到findViewById android

时间:2015-02-26 16:25:52

标签: java android xml findviewbyid

我在GUI上创建了一个表,但我仍然在循环查看视图的ID,因为它不是路径中的字符串。请帮助任何人!

例如ID是tMon9,tMon10,tMon11等......

    String classgroup = "CO.DNET3";
    int semester = 2;

    Timetable t = null;
    try {
        t = new Timetable(classgroup, semester);


    } catch (IOException e) {
        e.printStackTrace();
    }


    if(t!=null)
    {
        TextView tv;
        for (int i = 9; i < 18; i++) {
            if (t.getModule("wednesday", i + ":00") != null) {


                tv=(TextView)findViewById(R.id. (LOOP HERE)    ); // I want to loop through ids

                String s = "";
                s+= t.getModule("wednesday", i + ":00") + "\n";
                s+= t.getRoomNumber("wednesday", i + ":00") + "\n";
                tv.setText(s);
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

使用getIdentifier使用字符串名称获取View的ID:

int textViewID = getResources().getIdentifier("tMon"+i, "id", getPackageName());
tv=(TextView)findViewById(textViewID);