GridViewで動的に新しい行を追加する方法

GridViewに動的に新しい行を追加するには、次の手順を実行します。

  1. 空のGridViewをGridViewのレイアウトファイルで定義します。
<GridView
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</GridView>
  1. アクティビティでGridViewインスタンスを取得し、空のアダプターを作成する。
GridView gridView = findViewById(R.id.gridView);
ArrayList<String> dataList = new ArrayList<>();
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList);
gridView.setAdapter(adapter);
  1. notifyDataSetChanged()
dataList.add("New Row");
adapter.notifyDataSetChanged();

そうすれば動的に新しい行を追加することができます。

bannerAds