Android 如何在对话框中调用setAction?
我想创建添加数据到SQL lite与弹出对话框,在对话框中只有一个文本编辑和按钮保存。在Snackbar中,通常使用setAction()。但在对话中,我不知道如何调用“AddData” 我想调用这段代码AddData to onCreate,这段代码将文本保存在sqlLite中Android 如何在对话框中调用setAction?,android,android-fragments,android-dialogfragment,android-dialog,Android,Android Fragments,Android Dialogfragment,Android Dialog,我想创建添加数据到SQL lite与弹出对话框,在对话框中只有一个文本编辑和按钮保存。在Snackbar中,通常使用setAction()。但在对话中,我不知道如何调用“AddData” 我想调用这段代码AddData to onCreate,这段代码将文本保存在sqlLite中 public View.OnClickListener AddData = new View.OnClickListener() { @Override public void onCl
public View.OnClickListener AddData = new View.OnClickListener() {
@Override
public void onClick(View view) {
View customLayoutView = View.inflate(getActivity(), R.layout.layout_add_category_expanses, null);
final EditText edNim = customLayoutView.findViewById(R.id.inputcategoryexpanses);
final AlertDialog.Builder builder;
builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(R.string.strTitleAlert);
builder.setView(customLayoutView);
builder.setPositiveButton(R.string.btnKlikstr, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String nim = edNim.getText().toString();
if (databaseHelper.addCategoryExpanses(new CategoryExpanses(nim))) {
getDataFromSQLite();
Toast.makeText(getActivity(), "Data berhasil disimpan", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Data gagal disimpan", Toast.LENGTH_LONG).show();
}
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
};
请帮我解决这个问题。谢谢。你必须做一些类似的事情:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.list_category_expanses, container, false);
mTextMessage = (TextView) view.findViewById(R.id.message);
textSubcategoryExpanses = (AppCompatTextView) view.findViewById(R.id.textSubcategoryExpanses);
recyclerViewCategoryExpanses = (RecyclerView) view.findViewById(R.id.recyclerViewCategoryExpanses);
Button btn1 = view.findViewById(R.id.buttonListCategoryExpanses);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View customLayoutView = View.inflate(getActivity(), R.layout.layout_add_category_expanses, null);
final EditText edNim = customLayoutView.findViewById(R.id.inputcategoryexpanses);
final AlertDialog.Builder builder;
builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(R.string.strTitleAlert);
builder.setView(customLayoutView);
builder.setPositiveButton(R.string.btnKlikstr, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String nim = edNim.getText().toString();
if (databaseHelper.addCategoryExpanses(new CategoryExpanses(nim))) {
getDataFromSQLite();
Toast.makeText(getActivity(), "Data berhasil disimpan", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Data gagal disimpan", Toast.LENGTH_LONG).show();
}
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
initObjects();
return view;
}
谢谢,但我有一个新错误:android.database.sqlite.SQLiteConstraintException:唯一约束失败(代码1555 sqlite\u constraint\u PRIMARYKEY)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.list_category_expanses, container, false);
mTextMessage = (TextView) view.findViewById(R.id.message);
textSubcategoryExpanses = (AppCompatTextView) view.findViewById(R.id.textSubcategoryExpanses);
recyclerViewCategoryExpanses = (RecyclerView) view.findViewById(R.id.recyclerViewCategoryExpanses);
Button btn1 = view.findViewById(R.id.buttonListCategoryExpanses);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View customLayoutView = View.inflate(getActivity(), R.layout.layout_add_category_expanses, null);
final EditText edNim = customLayoutView.findViewById(R.id.inputcategoryexpanses);
final AlertDialog.Builder builder;
builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(R.string.strTitleAlert);
builder.setView(customLayoutView);
builder.setPositiveButton(R.string.btnKlikstr, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String nim = edNim.getText().toString();
if (databaseHelper.addCategoryExpanses(new CategoryExpanses(nim))) {
getDataFromSQLite();
Toast.makeText(getActivity(), "Data berhasil disimpan", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Data gagal disimpan", Toast.LENGTH_LONG).show();
}
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
initObjects();
return view;
}