Java Can';无法从AlertDialog中找到Databasehelper
目前,我正在尝试将数据插入SQLite数据库。我已经做到了。但现在我可能遇到了上下文问题 按下按钮后,AlertDialog将打开。之后,您可以将文本放入EditText。现在,用户必须按ok,然后,EditText中的日期需要进入SQLite数据库 问题是“DataBaseHelper_DB DataBaseHelper_DB=new DataBaseHelper_DB(getActivity());”始终为空 我怎样才能改变这个Java Can';无法从AlertDialog中找到Databasehelper,java,android,Java,Android,目前,我正在尝试将数据插入SQLite数据库。我已经做到了。但现在我可能遇到了上下文问题 按下按钮后,AlertDialog将打开。之后,您可以将文本放入EditText。现在,用户必须按ok,然后,EditText中的日期需要进入SQLite数据库 问题是“DataBaseHelper_DB DataBaseHelper_DB=new DataBaseHelper_DB(getActivity());”始终为空 我怎样才能改变这个 private void openDialog() {
private void openDialog() {
Dialog Dialog = new Dialog();
Dialog.show(getSupportFragmentManager(), "Dialog");
}
__________________________________________________________________________________________________________________
public class Dialog extends AppCompatDialogFragment
{
private EditText Profile;
Date date;
DpD_DB DpD_DB;
DataBaseHelper_DB DataBaseHelper_DB = new DataBaseHelper_DB(getActivity());
@Override
public android.app.Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_dialog, null);
builder.setView(view)
.setTitle("Profile")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String editProfile = Profile.getText().toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdfT = new SimpleDateFormat("HHmmss");
date = new Date();
try {
DpD_DB = new DpD_DB(-1, sdf.format(date).toString(),
sdfT.format(date).toString(), editProfile);
}
catch (Exception e) {
e.toString();
}
DataBaseHelper_DB.addOne(DpD_DB);
}
});
Profile = view.findViewById(R.id.Profile);
return builder.create();
}
________________________________________________________________________________________________________________
public DataBaseHelper_DB(@Nullable Context Context){
super(Context, DB_TABLE, null, 1);
}
尝试为您的对话框实现一个侦听器,如下面的代码所示。在Activity类中,实现listener并使用listeners方法获取文本。希望我清楚
public class Dialog extends AppCompatDialogFragment{
private EditText Profile;
Date date;
DpD_DB DpD_DB;
ProjectDialogListener listener;
DataBaseHelper_DB DataBaseHelper_DB = new DataBaseHelper_DB(getActivity());
@Override
public android.app.Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_dialog, null);
builder.setView(view)
.setTitle("Profile")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String editProfile = Profile.getText().toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdfT = new SimpleDateFormat("HHmmss");
date = new Date();
try {
DpD_DB = new DpD_DB(-1, sdf.format(date).toString(),
sdfT.format(date).toString(), editProfile);
}
catch (Exception e) {
e.toString();
}
//DataBaseHelper_DB.addOne(DpD_DB);
//Use the listener to get the text
listener.applyTexts(Dpd_DB)
}
});
Profile = view.findViewById(R.id.Profile);
return builder.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listener = (ProjectDialogListener) getActivity();//I am guessing u are using an activity else your the getActivity to getTargetFragment.
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + "must implement ProjectDialogListener");
}
}
public interface ProjectDialogListener {
void applyTexts(String title);
}
现在,对话框甚至不再打开。但是你的想法让我想到了另一种解决问题的方法。谢谢大家!@吉比·吉布森很乐意帮忙