У меня есть CardView
, который содержит RelativeLayout
с несколькими TextView's и кнопку. Мне удалось добавить их динамически в макет. Но я хочу изменить текст TextView's программным способом (разный текст для разных карт), и я не знаю, как это сделать. Как мне это достичь?Android - Изменение текста для текстовых элементов для динамически добавленной компоновки
CardView XML:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/CardView_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="20dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/imageView2"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="23dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="45dp"
android:gravity="center"
android:id="@+id/textView4"
android:layout_alignTop="@+id/imageView2"
android:layout_alignStart="@+id/imageView2"
android:layout_alignBottom="@+id/imageView2"
android:layout_alignEnd="@+id/imageView2" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView2"
android:layout_alignParentStart="false"
android:visibility="gone"
android:id="@+id/relativeLayout">
<View
android:layout_width="fill_parent"
android:layout_height="1.25dp"
android:background="@android:color/darker_gray"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:id="@+id/view" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Assignments"
android:id="@+id/textView8"
android:layout_marginLeft="50dp"
android:textColor="#000000"
android:layout_marginTop="28dp"
android:layout_below="@+id/textView11" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Homework"
android:id="@+id/textView9"
android:textColor="#000000"
android:layout_marginTop="10dp"
android:layout_below="@+id/textView8"
android:layout_alignStart="@+id/textView8" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Exams"
android:layout_marginTop="10dp"
android:id="@+id/textView10"
android:textColor="#000000"
android:layout_below="@+id/textView9"
android:layout_alignStart="@+id/textView9" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="TASKS"
android:layout_marginTop="10dp"
android:textColor="#000000"
android:id="@+id/textView11"
android:layout_below="@+id/view"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Personal Notes"
android:textColor="#000000"
android:layout_marginTop="10dp"
android:id="@+id/textView12"
android:layout_below="@+id/textView10"
android:layout_alignStart="@+id/textView10" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="0"
android:id="@+id/textView13"
android:textColor="#000000"
android:layout_marginStart="21dp"
android:layout_above="@+id/textView9"
android:layout_toEndOf="@+id/textView11" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="0"
android:id="@+id/textView14"
android:textColor="#000000"
android:layout_alignTop="@+id/textView9"
android:layout_alignStart="@+id/textView13" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="0"
android:textColor="#000000"
android:id="@+id/textView15"
android:layout_alignTop="@+id/textView10"
android:layout_alignStart="@+id/textView14" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="0"
android:textColor="#000000"
android:id="@+id/textView16"
android:layout_alignBottom="@+id/textView12"
android:layout_alignStart="@+id/textView15" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:id="@+id/view2"
android:layout_alignBottom="@+id/textView12"/>
<ImageButton
android:layout_width="30dp"
android:layout_height="35dp"
android:background="@drawable/note_button"
android:id="@+id/button2"
android:layout_alignBottom="@+id/textView15"
android:layout_alignParentEnd="true"
android:layout_marginBottom="7dp"
android:layout_marginEnd="38dp" />
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SCHEDULE"
android:id="@+id/button"
android:layout_weight="0.09"
android:layout_below="@+id/relativeLayout"
android:layout_alignParentStart="true"
android:layout_alignEnd="@+id/linearLayout" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="@+id/imageView2"
android:layout_toEndOf="@+id/imageView2"
android:layout_alignBottom="@+id/imageView2"
android:id="@+id/linearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Class No: "
android:textColor="#000000"
android:id="@+id/t_dur"
android:layout_marginLeft="20dp"
android:layout_marginTop="8dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Subject"
android:textColor="#000000"
android:id="@+id/t_sub"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Room No: "
android:textColor="#000000"
android:id="@+id/t_roomno"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
код для добавления макета (b, c, d являются TextViews):
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.badd:
LinearLayout ll = (LinearLayout) findViewById(R.id.cardtest);
LinearLayout ll2 = (LinearLayout) findViewById(R.id.lladd);
View v1 = getLayoutInflater().inflate(R.layout.card, ll2, false);
try{
b = (TextView) findViewById(R.id.t_dur);
c = (TextView) findViewById(R.id.t_sub);
d = (TextView) findViewById(R.id.t_roomno);
if (i > 1) {
ll2.addView(v1);
Log.v("DEBUG: ", "Inside IF");
b.setId(View.generateViewId());
c.setId(View.generateViewId());
d.setId(View.generateViewId());
b.setText("Class No: " + i);
c.setText("Subject: " + i);
d.setText("Room No: " + i);
}else{
Log.v("DEBUG: ", "Inside ELSE");
CardView cv = (CardView) findViewById(R.id.CardView_1);
cv.setVisibility(View.VISIBLE);
b.setText("Class No: " + i);
c.setText("Subject: " + i);
d.setText("Room No: " + i);
}
i++;
}catch (Exception e){
Log.v("DEBUG: ", e.toString());
}
break;
}
}
показать свой код пожалуйста ... –
Написал код – WhiplashOne