Android 向充气机内部的文本视图添加文本

Android 向充气机内部的文本视图添加文本,android,textview,layout-inflater,Android,Textview,Layout Inflater,我让我的充气机显示我想要的行数。我无法将文本插入充气机内的每个文本视图。它只填充了第一个TextView,其余为空。我尝试使用数组,但一直出现运行时错误 for (int i = 1; i <= numberOfGuests; ++i) { LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

我让我的充气机显示我想要的行数。我无法将文本插入充气机内的每个文本视图。它只填充了第一个TextView,其余为空。我尝试使用数组,但一直出现运行时错误

            for (int i = 1; i <= numberOfGuests; ++i) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.row_per_person, table);
            float numberInsertedForPerson = (float) (Math.round(tipPerPersons * 100.0) / 100.0);
            String sTipPerPerson = Float.toString(numberInsertedForPerson);
            tipPerPerson = (TextView) findViewById(R.id.tipPerPerson);
            tipPerPerson.setText(sTipPerPerson);

        }

for(int i=1;i您应该添加view.findViewById,而不是使用findViewById您的问题是(在我看来)LayoutInflater的行为非常混乱

首先,您应该缓存一个引用,而不是每次迭代都获取
LayoutInflater
(无论是否应附加
视图
)为false。这将为您提供膨胀的
视图
,然后您可以将其附加到父
视图组
。正确的方式如下所示:

LayoutInflater in = getLayoutInflater();

for (int i = 1; i <= numberOfGuests; i++) {
    View v = in.inflate(R.layout.row_per_person, table, false);
    float num = (float) (Math.round(tipPerPersons * 100.0) / 100.0);
    String tip = Float.toString(num);
    tipPerPerson = (TextView) v.findViewById(R.id.tipPerPerson);
    tipPerPerson.setText(tip);
    table.addView(v);
}
LayoutInflater in=getLayoutInflater();

对于(int i=1;i它有效,我只是忘了提及另一个事实,即当您传递false时,它不会被添加到父级
ViewGroup
。检查我最近的编辑--您需要调用
addView()
最后。谢谢,这是我一年半以来的第一个android项目,我记不太清楚了,也没有在工作中使用java