0

Я пишу приложение заметки для Android и у меня возникла проблема. Не могу получить вводимое в EditText значение, что бы передать его в БД FireBase.

create_note_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/note"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/note_them"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:ems="10"
        android:hint="@string/d_dialog_create_note_them"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/note_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:ems="10"
        android:hint="@string/d_dialog_create_note_text"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/note_them" />
</androidx.constraintlayout.widget.ConstraintLayout>

NotificationFragment.java

package com.example.plaffrie.ui.home;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.plaffrie.R;
import com.example.plaffrie.dialogs.CreateDialogFragment;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class NotificationFragment extends Fragment {

    private FloatingActionButton n_fab;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_notification, container, false);

        n_fab = view.findViewById(R.id.new_note_fab);

        n_fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager manager = getChildFragmentManager();
                CreateDialogFragment createDialogFragment = new CreateDialogFragment();
                createDialogFragment.show(manager, "create_note_dialog");
            }
        });

        return view;
    }
}

fragment_notification.xml

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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.GroupsFragment">

<LinearLayout
    android:id="@+id/note_linear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/new_note_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:src="@drawable/ic_notifications_black_24dp"
        android:tint="#FFFFFF"
        app:fabSize="mini" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:paddingLeft="8dp"
        android:text="@string/new_note" />

</LinearLayout>

<View
    android:id="@+id/divider5"
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:background="?android:attr/listDivider"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/note_linear" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/note_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/divider5" >

</androidx.recyclerview.widget.RecyclerView>

CreateDialogFragment.java

package com.example.plaffrie.dialogs;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import com.example.plaffrie.R;

import java.util.Objects;

public class CreateDialogFragment extends DialogFragment {

    private static final String TAG = "projectLog";

    private static EditText n_theme;
    private static EditText n_text;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        View view = getActivity().getLayoutInflater().inflate(R.layout.create_note_dialog, null);
        n_theme = view.findViewById(R.id.note_them);
        n_text = view.findViewById(R.id.note_text);

        return new AlertDialog.Builder(Objects.requireNonNull(getActivity()))
                .setTitle(getString(R.string.new_note))
                .setIcon(R.drawable.ic_notifications_black_24dp)
                .setView(R.layout.create_note_dialog)
                .setPositiveButton(getString(R.string.d_button_create), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        n_theme.getText().toString();
                        n_text.getText().toString();
                        Log.d(TAG, "Test - " + n_theme);
                    }
                })
                .setNegativeButton(getString(R.string.d_button_cancel), null)
                .create();
    }
}

Собственно и был создан лог, для проверки (выявления) получаемого значения

Log.d(TAG, "Test - " + n_theme);

но вместо значения я в логах вижу вот это:

D/projectLog: Test - androidx.appcompat.widget.AppCompatEditText{d4ce893 VFED..CL. ......I. 0,0-0,0 #7f080110 app:id/note_them}

Кто поможет с этим вопросом!?

2 ответа 2

0

Вы пытаетесь вывести в лог сам EditText, а не его содержимое. Нужно делать так:

String theme = n_theme.getText().toString();
Log.d(TAG, "Test - " + theme);
2
  • Такой вариант я пробовал, но в логах пустое значение. Так же я пробовал и getHint().toString(). Ответ: D/projectLog: Test - Название. То есть значения берутся с EditText, но не как вводимый текст.
    – Bakston
    14 мая 2020 в 4:35
  • Добавил текст в сам EditText, в логах он показывается. Всё плохо, когда вводишь текст, его как будто не вводил.
    – Bakston
    14 мая 2020 в 5:37
0

Проблема решена!

CreateDialogFragment.java

package com.example.plaffrie.dialogs;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import com.example.plaffrie.R;

public class CreateDialogFragment extends DialogFragment {

    private static final String TAG = "projectLog";

    private EditText n_theme;
    private EditText n_text;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.create_note_dialog, null);
        n_theme = view.findViewById(R.id.note_theme);
        n_text = view.findViewById(R.id.note_text);
        builder
                .setTitle(getString(R.string.new_note))
                .setIcon(R.drawable.ic_notifications_black_24dp)
                .setView(view)
                .setPositiveButton(getString(R.string.d_button_create), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        String h_theme = n_theme.getHint().toString();
                        String theme = n_theme.getText().toString();
                        String h_text = n_text.getHint().toString();
                        String text = n_text.getText().toString();
                        Log.d(TAG, h_theme + ": " + theme);
                        Log.d(TAG, h_text + ": " + text);
                    }
                })
                .setNegativeButton(getString(R.string.d_button_cancel), null);
        return builder.create();
    }
}

Проблема была в

.setView(R.layout.create_note_dialog)

Поменяв на

.setView(view)

вводимые значения появились в логах. Теперь осталось их запихнуть в БД FireBase.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.