0

こんにちは、私はXMLにある配列リストを通過させようとしていますが、それらをテーブルレイアウトに入れていますが、XMLから配列リストを呼び出すのに問題があります

私はこのようなことをしようとしています

ArrayList list = Collection(R.array.arraylist);
    int total = list.size();

for (int current = 0; current < total; current++)
    {
        // Create a TableRow and give it an ID
        TableRow tr = new TableRow(this);
        tr.setId(100+current);
        tr.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

 TextView labelTV = new TextView(this);
        labelTV.setId(200+current);
        labelTV.setText(list);
        labelTV.setTextSize(dip, 14);
        labelTV.setGravity(Gravity.CENTER);
        labelTV.setTextColor(Color.WHITE);
        labelTV.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        tr.addView(labelTV);

実際にarraylistデータを通過しているとは思わず、テキストビューにテキストが表示されていないので、助けてください

4

3 に答える 3

1

Java ファイルまたは xml ファイルを使用してテーブル レイアウトを作成する

ステップ 1 : (ステップ 1 には 2 つの方法があり、いずれかを実行します)

/*create tablelayout in java no need for create activity that can be handle in java file if u want xml file use or condition loop*/

 TableLayout tableLayout=new TableLayout(this);
    tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
    setContentView(tableLayout);
/*Follow java code only then add the activity in manifest file then Go to step 2*/
  • --------------------または--------------------

    / xml で tablelayout を作成し、Java で getView を作成します/

XML:

<?xml version="1.0" encoding="utf-8"?>
  <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/table"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

 </TableLayout>

ジャワ:

       setContentView(R.layout.table_layout);
 TableLayout tableLayout= (TableLayout) findViewById(R.id.table);

ステップ2:

    int colors[]=getResources().getIntArray(R.array.value);
    List<String> str=new ArrayList<String>();
    Collections.addAll(str, getResources().getStringArray(R.array.arrayvalues));
    Log.d(TAG,str.toString());
    for(int i=0;i<str.size();i++){
        TableRow row=new TableRow(this);
        row.setId(101+i);
        row.setBackgroundColor(Color.BLACK);
        row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));

        TextView text=new TextView(this);
        text.setId(201+i);
        text.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
        text.setText(str.get(i));
        text.setPadding(0,10,0,10);
        text.setTextColor(colors[i]);


        row.addView(text);
        tableLayout.addView(row);
    }       

Strings.xml

  <resources>
   <string-array name="arrayvalues">
    <item>Red</item>
    <item>Orange</item>
    <item>Yellow</item>
    <item>Green</item>
    <item>Blue</item>
    <item>Indigo</item>
    <item>Violet</item>
   </string-array>

   <integer-array name="value">
    <item>@color/Red</item>
    <item>@color/Orange</item>
    <item>@color/Yellow</item>
    <item>@color/Green</item>
    <item>@color/Blue</item>
    <item>@color/Indigo</item>
    <item>@color/Violet</item>
   </integer-array>
</resources>

色.xml

<resources>
<color name="Red">#FF0000</color>
<color name="Orange">#FF7F00</color>
<color name="Yellow">#FFFF00</color>
<color name="Green">#00FF00</color>
<color name="Blue">#0000FF</color>
<color name="Indigo">#4B0082</color>
<color name="Violet">#9400D3</color>
</resources>

出力:

ここに画像の説明を入力

-------------------------------------------------- - - - - - - - - - - - -終了 - - - - - - - - - - - - - ----------------------

string.xml ファイルから配列を取得する場合のみ (string ファイルから配列を取得する場合のみ、これに従わないでください)

    ArrayList<String> str=new ArrayList<String>();
    String[] val=getResources().getStringArray(R.array.value);
    str.addAll(Arrays.asList(val));
    Log.d(TAG,str.toString());
于 2017-06-12T10:06:43.697 に答える
0

テキストの設定に使用する ArrayList の特定の要素を参照します。おそらく次のようになります。

labelTV.setText(list.get(current)); // no idea what type you're dealing with, .toString() or a similar accessor may be necessary
于 2012-06-04T21:41:01.297 に答える
0

labelTV.setText(list);あなたが必要labelTV.setText(list.get(current));とする代わりに

于 2012-06-04T21:38:57.493 に答える