Привязка двух классов данных к одному фрагменту - Android Kotlin - PullRequest
0 голосов
/ 13 апреля 2020

В последнее время я был занят изучением android dev с kotlin, и мне удалось сделать несколько забавных вещей, но в последнее время застрял в одном месте. Проблема в том, что я не знаю, как получить привязку имени пользователя в моем фрагменте, я связал один класс данных, который является классом данных PostItem, и я связываю файлы во фрагменте с соответствующими полями этого класса, но моя проблема в том, что я просто не могу связать имя пользователя. И мой вопрос, как это сделать?

 <?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".fragments.PostsFragment">

    <data>
        <variable
            name="post"
            type="com.lswarss.ing_project.domain.PostItem" />

        <variable
            name="user"
            type="com.lswarss.ing_project.domain.UserItem" />

    </data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/post_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.242"
        android:text="@{post.title}" />

    <TextView
        android:id="@+id/post_body"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="4dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/post_title"
        app:layout_constraintVertical_bias="0.019"
        android:text="@{post.body}"/>

    <TextView
        android:id="@+id/post_user"
        android:layout_width="80dp"
        android:layout_height="20dp"
        android:layout_marginTop="8dp"
        android:height="20dp"
        android:text="@{user.username}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/post_comment"
        app:layout_constraintHorizontal_bias="0.144"
        app:layout_constraintStart_toEndOf="@+id/userView"
        app:layout_constraintTop_toBottomOf="@+id/post_body"
        tools:text="Username" />

    <TextView
        android:id="@+id/post_comment"
        android:layout_width="80dp"
        android:layout_height="20dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="24dp"
        android:text="comments"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/post_body"
        app:layout_constraintVertical_bias="0.483"
        tools:text="comments" />

    <ImageView
        android:id="@+id/userView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:contentDescription="@string/usericon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/post_body"
        app:srcCompat="@drawable/user_img" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Это мой фрагмент поста, и я подумал о том, чтобы сделать какую-нибудь функцию, которая будет извлекать имя пользователя по идентификатору из класса PostItem, но, может быть, есть какой-то другой лучший способ?

Вот ссылка на мой репо с проектом на главной ветке - самый свободный sh код прямо сейчас https://github.com/LSWarss/ing_project

извините, если вопрос глупый но я просто не могу найти хорошую ссылку для этого, и я действительно noob в android, поэтому, пожалуйста, наведите меня: D

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...