Динамически присваивайте идентификаторы элементам внутри макета в строке таблицы, используя android - PullRequest
0 голосов
/ 03 мая 2020

У меня есть Tablelayout, в котором строки добавляются динамически при нажатии кнопки. Эти строки содержат макет, который включает два текста редактирования и изображение. Итак, в основном каждая строка содержит два edittext и imageview. Но я хочу назначить идентификаторы этим элементам, чтобы я мог извлечь и сохранить данные из них. Пожалуйста, помогите мне. Заранее спасибо. Вот мой код для добавления новой строки при нажатии кнопки -

TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
        TableRow row = new TableRow(getApplicationContext());
        counter++;
        LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
        RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.item_add_document, null);
        row.addView(relativeLayout);
        table.addView(row);

Вот мой код для раздуваемого макета -

 <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/relative"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.CardView
    app:cardUseCompatPadding="true"
    app:cardElevation="@dimen/size_5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/size_10dp"
        android:padding="@dimen/size_10dp"
        android:orientation="vertical">

     <ImageView
         android:id="@+id/imgclose"
         android:layout_gravity="end"
         android:src="@drawable/ic_close_black_24dp"
         android:layout_marginBottom="@dimen/size_10dp"
         android:layout_width="@dimen/size_30dp"
         android:layout_height="@dimen/size_30dp"/>

        <Button
            android:id="@+id/btn_add"
            android:layout_width="match_parent"
            android:layout_height="@dimen/size_40dp"
            android:layout_weight="1"
            android:background="@color/yellow"
            android:fontFamily="@font/lato_bold"
            android:text="@string/add"
            android:layout_marginBottom="@dimen/size_10dp"
            android:textAllCaps="true"
            android:textColor="@color/text_dark_color"
            android:textSize="@dimen/size_12sp"
            app:fontFamily="@font/lato_bold" />

        <ImageView
            android:id="@+id/imgDoc"
            android:layout_marginTop="@dimen/size_5dp"
            android:layout_marginBottom="@dimen/size_10dp"
            android:src="@drawable/placeholder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <EditText
            android:id="@+id/edit_title"
            android:layout_width="match_parent"
            android:layout_height="@dimen/size_40dp"
            android:background="@color/edit_background"
            android:cursorVisible="true"
            android:fontFamily="@font/lato_regular"
            android:padding="@dimen/size_10dp"
            android:hint="Title"
            android:textColorHint="@color/white"
            android:singleLine="true"
            android:textColor="@color/hint_color"
            android:textSize="@dimen/size_12sp"
            app:fontFamily="@font/lato_regular"
            tools:ignore="Autofill,LabelFor,TextFields" />


        <EditText
            android:id="@+id/edit_description"
            android:layout_width="match_parent"
            android:layout_height="@dimen/size_40dp"
            android:background="@color/edit_background"
            android:cursorVisible="true"
            android:layout_marginTop="@dimen/size_10dp"
            android:fontFamily="@font/lato_regular"
            android:padding="@dimen/size_10dp"
            android:minLines="5"
            android:hint="Description"
            android:textColorHint="@color/white"
            android:textColor="@color/hint_color"
            android:textSize="@dimen/size_12sp"
            app:fontFamily="@font/lato_regular"
            tools:ignore="Autofill,LabelFor,TextFields" />


        <EditText
            android:id="@+id/edit_image"
            android:layout_width="match_parent"
            android:layout_height="@dimen/size_40dp"
            android:background="@color/edit_background"
            android:cursorVisible="true"
            android:fontFamily="@font/lato_regular"
            android:padding="@dimen/size_10dp"
            android:hint="Path"
            android:visibility="gone"
            android:textColorHint="@color/white"
            android:singleLine="true"
            android:layout_marginTop="@dimen/size_10dp"
            android:textColor="@color/hint_color"
            android:textSize="@dimen/size_12sp"
            app:fontFamily="@font/lato_regular"
            tools:ignore="Autofill,LabelFor,TextFields" />



        <TextView
            android:id="@+id/txtid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
...