Хотите получить дату в текстовом поле от DatePicker - PullRequest
0 голосов
/ 29 марта 2020

Я хочу получить дату от выбора даты из Datekt.class в текстовое представление date_of_birth в PersonalDetailsUpdaterkt.class Я получаю сообщение об ошибке java .lang.IllegalStateException: date_of_birth не должно быть пустым Как я могу получить дату в текстовом поле


Date.kt

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.DatePickerDialog
import android.app.Dialog
import android.graphics.Color
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import android.widget.DatePicker
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import kotlinx.android.synthetic.main.updaterpersonaldetails.*
import java.util.*

class Date : DialogFragment(),DatePickerDialog.OnDateSetListener {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

        val calander: Calendar = Calendar.getInstance()
        val year = calander.get(Calendar.YEAR)
        val month = calander.get(Calendar.MONTH)
        val day = calander.get(Calendar.DATE)

        val datePickerDialog =
            DatePickerDialog(activity!!, AlertDialog.THEME_HOLO_LIGHT, this, year, month, day)
        val textView = TextView(activity)
        val layoutParams = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
        )

        textView.layoutParams = layoutParams
        textView.setPadding(20, 20, 20, 20)
        textView.gravity = Gravity.CENTER
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25f)
        textView.setText("Select Date Of Birth")
        textView.setTextColor(Color.parseColor("#ffffff"))
        textView.setBackgroundColor(Color.parseColor("#5Ac18e"))

        datePickerDialog.setCustomTitle(textView)
        return datePickerDialog
    }

    @SuppressLint("SetTextI18n")
    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
        date_of_birth.text="$dayOfMonth-$month-$year"
    }
}

PersonalDetailsUpdater.kt


import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import com.example.lanjewartutorial.R
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.storage.StorageReference

class PersonalDetailsUpdater :Fragment() {
    lateinit var mStorageReference: StorageReference;
    lateinit var mAuth: FirebaseAuth
    lateinit var user: FirebaseUser
    lateinit var refUsers: DatabaseReference
    lateinit var date:TextView
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {


        val root = inflater.inflate(R.layout.fragment_personaldetailsupdater, container, false)

        mAuth = FirebaseAuth.getInstance()

        val userr = mAuth.currentUser
        refUsers = FirebaseDatabase.getInstance().getReference("Users")
        date=root.findViewById(R.id.date_of_birth)
        date.setOnClickListener {
            val fragment : DialogFragment=Date()
            fragment.show(fragmentManager!!,"Date")
        }



        return root

    }
}

updaterpersonaldetails. xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout android:orientation="vertical"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">

        <LinearLayout android:orientation="vertical"
            android:layout_weight="1"
            android:layout_height="0dp"
            android:layout_width="fill_parent">

            <EditText
                android:layout_marginTop="15dp"
                android:id="@+id/uname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="none"
                android:hint="Username"
               />
        </LinearLayout>
        <LinearLayout android:orientation="vertical"
            android:layout_weight="1"
            android:layout_height="0dp"
            android:layout_width="fill_parent">

            <EditText
                android:layout_marginTop="15dp"
                android:id="@+id/sname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="none"
                android:hint="Name"
                />
        </LinearLayout>
        <LinearLayout android:orientation="vertical"
            android:layout_weight="1"
            android:layout_height="0dp"
            android:layout_width="fill_parent">

            <EditText
                android:layout_marginTop="15dp"
                android:id="@+id/fname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="none"
                android:hint="Father's Name"
                />
        </LinearLayout>
        <LinearLayout android:orientation="vertical"
            android:layout_weight="1"
            android:layout_height="0dp"
            android:layout_width="fill_parent">

            <EditText
                android:layout_marginTop="15dp"
                android:id="@+id/mname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="none"
                android:hint="Mother's Name"
                />

        </LinearLayout>
        <LinearLayout android:orientation="vertical"
            android:layout_weight="1"
            android:layout_height="0dp"
            android:layout_width="fill_parent">

            <EditText
                android:layout_marginTop="15dp"
                android:id="@+id/lname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="none"
                android:hint="Last Name"
                />
        </LinearLayout>
        <LinearLayout android:orientation="vertical"
            android:layout_weight="1"
            android:layout_height="0dp"
            android:layout_width="fill_parent">


            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="match_parent" >


                <TextView
                    android:id="@+id/textView2"
                    android:textSize="18dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Gender : " />

                <RadioButton
                    android:id="@+id/male"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Male" />

                <RadioButton
                    android:id="@+id/female"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Female" />

            </RadioGroup>

        </LinearLayout>
        <LinearLayout android:orientation="vertical"
            android:layout_weight="1"
            android:layout_height="0dp"
            android:layout_width="fill_parent">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:textSize="18dp"
                    android:id="@+id/tex"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Date of Birth : " />

                <TextView
                    android:layout_below="@+id/tex"
                    android:id="@+id/date_of_birth"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:text="dd/mm/yyyy"
                    android:inputType="text"
                    android:textSize="18dp"
                 />


            </RelativeLayout>
        </LinearLayout>
        <LinearLayout android:orientation="vertical"
            android:layout_weight="1"
            android:layout_height="0dp"
            android:layout_width="fill_parent">

          <Button
              android:id="@+id/submit"
              android:layout_marginTop="20sp"
              android:layout_marginLeft="50dp"
              android:layout_marginRight="50dp"
              android:layout_marginBottom="20sp"
              android:layout_height="match_parent"
              android:layout_width="match_parent"
              android:text="Submit"
              android:textSize="25dp"/>
        </LinearLayout>



    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

frag_personaldetailsupdater. xml

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


<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:background="@drawable/background">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">

    <include
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        layout="@layout/updaterpersonaldetails"
        />

        <ProgressBar
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="#ffffff"
            android:visibility="visible"/>

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