无法将设置的文本值动态传递给android中的文本视图

时间:2016-05-04 13:40:51

标签: android textview layout-inflater

我在我的应用程序中使用布局充气器以满足我的要求。我在布局中有文本视图,edittext和按钮。这是我的xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayoutid"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textviewid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />

    <EditText
        android:id="@+id/edittextid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linearlayoutid"
    android:layout_centerHorizontal="true"
    android:text="button" />

我想生成'n'个文本视图并传递数据库中的值。 我在这里使用布局充气机,下面是我的代码

setContentView(R.layout.activity_main);

LinearLayout lv = (LinearLayout) findViewById(R.id.linearlayoutid);
View v = null;


for (int i = 0; i < 41; i++) {

    v = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
    lv.addView(v);
}

//  EditText editText = (EditText) v.findViewById(R.id.edittextid);
final TextView[] myTextViews = new TextView[41];
for (int i = 0; i < 41; i++) {

    TextView tv = (TextView) v.findViewById(R.id.textviewid);
    d = mydb.getValues();

    myTextViews[i] = tv;
    tv.setText(d.get(i));
    Log.d(tag,"run"+d.get(i));

}

它动态生成'n'个文本视图,但问题是我无法使用'setText'显示文本。它的空白完全。任何人都可以告诉我我在哪里弄错了?我想传递动态值而不是硬编码值。任何帮助都会很棒!!感谢

1 个答案:

答案 0 :(得分:0)

如果您想要显示某些数据的列表,最好使用here。 或者您可以使用 RecyclerView 。 您可以找到有关此布局的更多信息以及如何在Internet上使用它。