动态添加textViews到的LinearLayout动态、textViews、LinearLayout

2023-09-12 03:13:03 作者:爱硪 、伱不配

我在这里读这地方,我完全失去了它,但可以使用一些帮助。

I read this somewhere here and I totally lost it, but could use some assistance.

我的应用程序是拉动从sqlite的列名到一个数组。我想创建一个TextView和编辑文本为每一个(通过数组的大小),我记得读书的地方,你可以把该textViews变量的名称,如数组,但我不知道在哪里,现在是

My app is pulling the column names from sqlite into an array. I want to create a textview and edit text for each one (via the size of the array), and I remember reading somewhere that you can treat the textViews variable names like an array, but I don't know where that is now.

那么我将如何动态地创建一个TextView和EDITTEXT然而,对于许多上市是一个数组?

So how would I dynamically create a textView and editText for however many listings are in an array?

这是像

TextView tv[] = new TextView()...

for(...){
tv[i]...
}

这是正确的?

Is this right?

我AP preciate您的帮助!

I appreciate your help!

推荐答案

类似下面的应该是你所需要的:

Something like the following should be what you need:

final int N = 10; // total number of textviews to add

final TextView[] myTextViews = new TextView[N]; // create an empty array;

for (int i = 0; i < N; i++) {
    // create a new textview
    final TextView rowTextView = new TextView(this);

    // set some properties of rowTextView or something
    rowTextView.setText("This is row #" + i);

    // add the textview to the linearlayout
    myLinearLayout.addView(rowTextView);

    // save a reference to the textview for later
    myTextViews[i] = rowTextView;
}