如何将数据从alertdialog插入数据库

时间:2018-01-02 15:45:29

标签: java android

我试图从edt_nominal获取文本并将其输入我的数据库。但我不知道代码。

这是DonasiDetail.class代码:

 private void showAlertDialog() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(DonasiDetail.this);
    alertDialog.setTitle("Melakukan Donasi");
    alertDialog.setMessage("Masukan Nominal Donasi: ");

    final EditText edt_nominal = new EditText(DonasiDetail.this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
          LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
    );

    edt_nominal.setLayoutParams(lp);
    alertDialog.setView(edt_nominal); //Menambahkan edittest ke alertdialog
    alertDialog.setIcon(R.drawable.ic_handshake);

    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            new Database(getBaseContext()).addToDonasi(new Transaksi(
                    donasiId,
                    currentDonasi.getNama(),
                    edt_nominal.getText().toString()
            ));

            Toast.makeText(DonasiDetail.this, "Data Telah Masuk ke Donasi", Toast.LENGTH_SHORT).show();
            finish();
        }
    });

    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    alertDialog.show();
}

这是Transaksi.class代码:

public class Transaksi {
private String Id_Donasi;
private String Nama_Donasi;
private String Nominal_Donasi;

public Transaksi(String id_Donasi, String nama_Donasi, String nominal_Donasi) {
    Id_Donasi = id_Donasi;
    Nama_Donasi = nama_Donasi;
    Nominal_Donasi = nominal_Donasi;
}

public String getId_Donasi() {
    return Id_Donasi;
}

public void setId_Donasi(String id_Donasi) {
    Id_Donasi = id_Donasi;
}

public String getNama_Donasi() {
    return Nama_Donasi;
}

public void setNama_Donasi(String nama_Donasi) {
    Nama_Donasi = nama_Donasi;
}

public String getNominal_Donasi() {
    return Nominal_Donasi;
}

public void setNominal_Donasi(String nominal_Donasi) {
    Nominal_Donasi = nominal_Donasi;
} }

我想在Transaksi.class中将edt_nominal.getText()。toString()中的数据设置为Nominal_Donasi

我对Android编程很新,我仍然习惯了,任何帮助都将不胜感激!感谢

1 个答案:

答案 0 :(得分:0)

你必须学习如何在android中存储数据。浏览this文章。确定哪种选择最适合您。如果您的数据较少,则可以将其存储在SharedPreferences中,否则请转到SQLite database