如何在动态文本视图

时间:2016-08-26 06:28:23

标签: java android arraylist

每当我设置rowTextView.setText(" row value #" + str);然后arraylist显示那里的最后一个值。 我的列表数据 60,65,70,75,80 。只显示那里的最后一个值。我想要所有的数据。我怎么能得到这个????

 int size = al_getAllProductSize.size();

            System.out.println("The size of array is: " + size);
            for (int i = 0; i < size; i++) {


            }

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

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

            for (int i = 0; i < N; i++) {
            final TextView rowTextView = new TextView(MainActivity.this);

            for (String str : al_getAllProductSize)
            {

            rowTextView.setText(" row value #" + str); // i want all value through loop

              }

                ll_textViewObj.addView(rowTextView);

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

3 个答案:

答案 0 :(得分:1)

如果你想要每个文本视图中的每个字符串,你可以使用类似的东西:

for (int i = 0; i < N; i++) {
    final TextView rowTextView = new TextView(MainActivity.this);

    // getting respective string val from list
    rowTextView.setText(" row value #" + al_getAllProductSize.get(i));

    ll_textViewObj.addView(rowTextView);

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

答案 1 :(得分:1)

试试这个。希望它会对你有所帮助。

final int X = 5; // total number of textviews to add
final TextView[] myTextViews = new TextView[X]; // create an empty array;

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

         // set value in textview
          rowTextView.setText("row value #" + i);

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

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

答案 2 :(得分:0)

试试这个

int size = al_getAllProductSize.size();
TextView[] myTextViews = new TextView[size];
for (int i = 0; i < size; i++) {
     myTextViews[i] = new TextView(MainActivity.this);
     rowTextView.setText( al_getAllProductSize.get(i));
     ll_textViewObj.addView(myTextViews[i]);
     }