GridViewで動的に新しい行を追加する方法
GridViewに動的に新しい行を追加するには、次の手順を実行します。
- 空のGridViewをGridViewのレイアウトファイルで定義します。
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</GridView>
- アクティビティで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);
- notifyDataSetChanged()
dataList.add("New Row");
adapter.notifyDataSetChanged();
そうすれば動的に新しい行を追加することができます。