私はこの(RTL)のようなものを実装したい:レイアウトにビューを追加することができること、言い換えればクラッシュ(アンドロイド)
を3列。 私はこれをやってみました:
my_fragment.xml:
...
<TableLayout
android:id="@+id/myTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:stretchColumns="*"
android:layout_margin="10dp"
android:background="@color/background_color">
</TableLayout >
...
myFragment.java:
TableRow[] tableRows = new TableRow[ serverResponse.getData().size()/3 ];
for(int i=0, j=0; i<serverResponse.getData().size() ;i++)
{
View customView = inflater.inflate(R.layout.data_item, tableRows[j], true);
((TextView) customView.findViewById(R.id.viewText)).setText(serverResponse.getData().get(i).getItemData());
if(i%3 == 0) {
j++;
}
myTable.addView(tableRows[i] , new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
}
をクラッシュ:Cannot add a null child view to a ViewGroup
EDIT:マイク・M.へ 感謝」今のところうまくいきます。しかし、一つの列は常にカットされています
data_item.xmlは単純です:あなたはTableRow
配列の個々の要素を初期化していないので、あなたはそのエラーを取得している
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">
<TextView
android:id="@+id/viewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="end"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:background="@drawable/circle"/>
</LinearLayout>
'TableRow'の配列を初期化しようとしていますが、個々の要素を初期化していないので' null'です。 –
私は混乱しています..要素は、 "inflate"部分で発生するcustomViews ..を受け取る必要があります。いいえ? – BVtp
'Object'の配列の各要素を初期化する必要があります。配列を初期化すると、各要素は 'null'になります。つまり、新しい行ごとに 'new TableRow(getContext())'を呼び出す必要があります。また、あなたのロジックはあなたのテーブルを作成するための少しオフです。 2つのネストされた 'for'ループを使う方が良いでしょう。 –