@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) {
final Dialog fbDialogue = new Dialog(getActivity());
fbDialogue.setContentView(R.layout.layout_add_category_expanses);
fbDialogue.setCancelable(true);
fbDialogue.show();
}
});
initObjects();
return view;
}
我想将此代码AddData调用到onCreate,此代码将文本保存在sqlLite中。
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();
}
};
请帮助我解决此问题。谢谢。
答案 0 :(得分:0)
您必须执行以下操作:
@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;
}