我必须为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);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fullscreen_dialog, container, false);
ImageButton close = view.findViewById(R.id.fullscreen_dialog_close);
TextView action = view.findViewById(R.id.fullscreen_dialog_action);
EditText titleEdit = view.findViewById(R.id.fullscreen_dialog_title);
EditText infoEdit = view.findViewById(R.id.fullscreen_dialog_info);
EditText fullEdit = view.findViewById(R.id.fullscreen_dialog_full);
close.setOnClickListener(this);
action.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.fullscreen_dialog_close:
dismiss();
break;
case R.id.fullscreen_dialog_action:
this.titleArray.add(titleEdit.getText().toString());
this.infoArray.add(infoEdit.getText().toString());
this.fullArray.add(fullEdit.getText().toString());
this.imageArray.add(R.drawable.ic_launcher_foreground);
callback.onActionClick("Memo saved!");
dismiss();
break;
}
}
public interface Callback {
void onActionClick(String name);
}
}
我想我在使用findViewById的方式上肯定犯了一些错误,但是Android对我来说是一种新事物,我似乎找不到错误的地方。
titleArray,infoArray和fullArray是存储我的数据的ArrayList。
imageArray是我的图像ID的ArrayList。
答案 0 :(得分:0)
您调用或使用未初始化的实例变量(即fullEdit,titleEdit等)。要在onCreateView方法中更正这些初始化的内容,因此它应该看起来像这样
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_fullscreen_dialog, container, false);
ImageButton close = view.findViewById(R.id.fullscreen_dialog_close);
TextView action = view.findViewById(R.id.fullscreen_dialog_action);
titleEdit = view.findViewById(R.id.fullscreen_dialog_title);
infoEdit = view.findViewById(R.id.fullscreen_dialog_info);
fullEdit = view.findViewById(R.id.fullscreen_dialog_full);
close.setOnClickListener(this);
action.setOnClickListener(this);
return view;
}
使用上述方法,实例化了类实例EditText变量,可用于获取文本onClick方法
答案 1 :(得分:0)
您在类中定义了变量titleEdit,infoEdit和完整编辑 global ,但问题是您在onCreateView方法中再次 local 定义了变量并将它们分配给通过使用findViewById查看。 因此,您现在有了2种变量:
当您尝试获取editText的值时,您将访问未链接到View的全局文本。
要解决的是删除本地的。您的onCreateView应该看起来像这样:
mat <- matrix(c(1:5, 2,2,1,5,2, 8,9,10,10,12), ncol = 3)
mean(mat[, 3][which(mat[, 2] == 2)])