我不知道如何在我的自定义对话框中获取我的 EditText 的值

我必须在 Android 上为一个学校项目做一个小应用程序,我想通过全屏对话框更新我的 listView,但我找不到一种方法来获取我的 EditText 字段中的值以将它们添加到我的 ArrayLists。


这是我的 Dialog 类的代码:


package com.example.memo;



import android.annotation.SuppressLint;

import android.os.Bundle;

import android.support.annotation.NonNull;

import android.support.annotation.Nullable;

import android.support.v4.app.DialogFragment;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.EditText;

import android.view.ViewGroup;

import android.widget.ImageButton;

import android.widget.TextView;


import java.util.ArrayList;


@SuppressLint("ValidFragment")

public class FullscreenDialog extends DialogFragment implements View.OnClickListener {


    private EditText titleEdit;

    private EditText infoEdit;

    private EditText fullEdit;


    private ArrayList<String> titleArray;

    private ArrayList<String> infoArray;

    private ArrayList<Integer> imageArray;

    private ArrayList<String> fullArray;



    private Callback callback;


    public FullscreenDialog(ArrayList<String> titleArray, ArrayList<String> infoArray, ArrayList<String> fullArray, ArrayList<Integer> imageArray){

        this.titleArray = titleArray;

        this.infoArray = infoArray;

        this.fullArray = fullArray;

        this.imageArray = imageArray;

    }


    static FullscreenDialog newInstance(ArrayList<String> titleArray, ArrayList<String> infoArray, ArrayList<String> fullArray, ArrayList<Integer> imageArray) {

        return new FullscreenDialog(titleArray,infoArray,fullArray,imageArray);


    }


    public void setCallback(Callback callback) {

        this.callback = callback;

    }


    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setStyle(DialogFragment.STYLE_NORMAL, R.style.FullscreenDialogTheme);



    }


  

我想我在使用 findViewById 或其他东西的方式上一定犯了一些错误,但 Android 对我来说有点新,我似乎无法找到错误的地方。


titleArray、infoArray 和 fullArray 是存储我的数据的 ArrayList。


imageArray 是我的图像 ID 的 ArrayList。


慕的地6264312
浏览 151回答 2
2回答

四季花海

您调用或使用未初始化的实例变量(即 fullEdit、titleEdit 等)。为了纠正这个在方法 onCreateView 中初始化它们,所以它应该看起来像这样@Nullable&nbsp; &nbsp; @Override&nbsp; &nbsp; public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; View view = inflater.inflate(R.layout.activity_fullscreen_dialog, container, false);&nbsp; &nbsp; &nbsp; &nbsp; ImageButton close = view.findViewById(R.id.fullscreen_dialog_close);&nbsp; &nbsp; &nbsp; &nbsp; TextView action = view.findViewById(R.id.fullscreen_dialog_action);&nbsp; &nbsp; &nbsp; &nbsp; titleEdit = view.findViewById(R.id.fullscreen_dialog_title);&nbsp; &nbsp; &nbsp; &nbsp; infoEdit = view.findViewById(R.id.fullscreen_dialog_info);&nbsp; &nbsp; &nbsp; &nbsp; fullEdit = view.findViewById(R.id.fullscreen_dialog_full);&nbsp; &nbsp; &nbsp; &nbsp; close.setOnClickListener(this);&nbsp; &nbsp; &nbsp; &nbsp; action.setOnClickListener(this);&nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; }用上面的类实例 EditText 变量被实例化并且可以用来获取文本的 onClick 方法

小怪兽爱吃肉

您在类中全局定义变量 titleEdit、infoEdit 和 full Edit&nbsp;,但问题是您在 onCreateView 方法中再次在本地定义它们,并使用 findViewById 将它们分配给视图。所以你现在有两种变量:全球的(尚未链接)本地的(链接到视图)当您尝试获取未链接到 View 的全局值时,您访问的 editText 的值。为了解决这个问题,删除本地的。你 onCreateView 应该是这样的:@Nullable&nbsp; &nbsp; @Override&nbsp; &nbsp; public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; View view = inflater.inflate(R.layout.activity_fullscreen_dialog, container, false);&nbsp; &nbsp; &nbsp; &nbsp; ImageButton close = view.findViewById(R.id.fullscreen_dialog_close);&nbsp; &nbsp; &nbsp; &nbsp; TextView action = view.findViewById(R.id.fullscreen_dialog_action);&nbsp; &nbsp; &nbsp; &nbsp; titleEdit = view.findViewById(R.id.fullscreen_dialog_title);&nbsp; &nbsp; &nbsp; &nbsp; infoEdit = view.findViewById(R.id.fullscreen_dialog_info);&nbsp; &nbsp; &nbsp; &nbsp; fullEdit = view.findViewById(R.id.fullscreen_dialog_full);&nbsp; &nbsp; &nbsp; &nbsp; close.setOnClickListener(this);&nbsp; &nbsp; &nbsp; &nbsp; action.setOnClickListener(this);&nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java