I am trying to get one string, two integers and one timepicker result from a dialog to the activity where this dialog is created. How can I pass this information to this activity?
Below there is my code.
package ilachatirlatici.pack.net;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class HapHatirlatici extends Activity{
Button ekleButton;
boolean eklendiMi;
EditText ilacAdi;
String ilacAdiString;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.haphatirlatici);
// After creating the activity setting other things for running
ekleButton = (Button) findViewById(R.id.EkleButton);
eklendiMi = false;
ilacAdiString = "";
ekleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
ilacAdi = (EditText) findViewById(R.id.KayitAdiEditText);
View layout = getLayoutInflater().inflate(R.layout.ekle_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(HapHatirlatici.this);
builder.setPositiveButton(R.string.ekle,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
eklendiMi = true;
ilacAdiString = ilacAdi.getText().toString();
//ilacAdiString = ilacAdi.getEditableText().toString();
databaseEkle(eklendiMi, ilacAdiString);
}
});
builder.setNegativeButton(R.string.vazgec, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
eklendiMi = false;
databaseEkle(eklendiMi, "");
}
});
builder.setView(layout);
AlertDialog alertDialog = builder.create();
alertDialog.setTitle("Ilac Ekleme");
alertDialog.show();
}
});
}
public boolean databaseEkle(boolean eklemeVarMi, String ilacAdi)
{
boolean sonuc = false;
System.out.println(ilacAdi);
return sonuc;
}
}
I am trying to get the result in positive button part. I only implemented the string part. Since the other parts will be same. How can I pass this string to the databaseEkle() function?
You are actually trying to access EditText which is actually inside AlertDialog’s layout R.layout.ekle_dialog. You need to refer to your inflated layout to reference to edittext.
set your button’s onClick listener like this:
in this example i’m using
myEditTextinstead ofilacAdibecause you have declared your editText global, which doesn’t need to be in this case