android studioでタブ操作のできるアプリを作っています
javaのクラスは extends Fragment していて Binding を使っています
fragment_a.xmlにincludeしたcardsets_conj.xmlのTextView(id:conj)に
javaからテキストを追加したいのですが難航しています
タブに表示するテキストビューに文章を追加したいんですが、includeしたやつのIDに接続するにはどうしたらいいでしょうか
あと、ID指定するときに
binding.idname.setText("untara");
ってすると思うんですがidのところを変数で扱うやりかたも教えていただきたいです
例えば、
String name = "tg";
binding.name.setText("hoge");
みたいにしてもnameというid自体を参照しようとするので困っています
よろしくお願いします
以下コードです
fragment_a.xmlにincludeしたcardsets_conj.xmlのTextView(id:conj)に
javaからテキストを追加したいです
xml
1<!--fragment_a.xml--> 2<?xml version="1.0" encoding="utf-8"?> 3<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 android:padding="16dp"> 8 9 <include 10 android:id="@+id/tg" 11 layout="@layout/cardsets_conj" /> 12 <include 13 android:id="@+id/ttk" 14 layout="@layout/cardsets_conj" /> 15 16 <!--この調子でincludeが10個ほど続きます。 17 IDはそれぞれに固有のものを割り当ててります--> 18 19</LinearLayout>
xml
1<!-- 設定はしょってます --> 2<!--cardsets_conj.xml--> 3<?xml version="1.0" encoding="utf-8"?> 4<LinearLayout> 5 <androidx.cardview.widget.CardView 6 android:id="@+id/cardView_content"> 7 <LinearLayout> 8 9<!-- 下のID:conjにテキストをセットしたい --> 10 <TextView 11 android:id="@+id/conj" /> 12 13 </LinearLayout> 14 </androidx.cardview.widget.CardView> 15</LinearLayout>
java
1/* nameにfragment_aのincludeしたレイアウトのID名代入 2 "tg"とか"ttk"など 3StringBuilder builder = new StringBuilder()のbuilderに文章をいれてます 4 */ 5private FragmentABinding binding; 6binding= FragmentABinding.inflate(inflater, container, false); 7TextView textView; 8Resources res = getResources(); 9String name; 10//普通は下のでいけるんですがbinding使ったやり方がわかりません 11int cardID = res.getIdentifier(name, "id", .getPackageName()); 12textView = findViewById(cardID).findViewById(R.id.conj); 13textView.setText(builder.toString()); 14 15 16 17
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。