public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_add) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
builder.setTitle("Tambah Daftar");
final View formsView = inflater.inflate(R.layout.detailbarang, null, false);
final EditText barang = (EditText) formsView.findViewById(R.id.nama_barang);
final EditText harga = (EditText) formsView.findViewById(R.id.harga_barang);
builder.setView(barang);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (barang.getText().toString().equals("") && (harga.getText().toString().equals(""))) {
Toast.makeText(MainActivity.this, "Teks Kosong, Masukkan teks", Toast.LENGTH_SHORT).show();
} else {
ListData.add(preferredCase(barang.getText().toString())+ harga.getText().toString());
storeArrayVal(ListData, getApplicationContext());
Toast.makeText(MainActivity.this, "Berhasil", Toast.LENGTH_SHORT).show();
lv.setAdapter(adapter);
}
}
});
builder.setNegativeButton("Batal", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
return true;
}
it was always forced close when i want to use the action_add function. it's start when i add function layoutinfleter for my layout.xml for alert dialog maybe someone can help me how to use correctly function to use in my layout.xml to alert dialog. thx before.
use dialog.cancel();
after if else scope
you have to close dialog after performing your action
remove builder.setView(barang) and do it like this
AlertDialog.Builder builder=new AlertDialog.Builder(ctx);
LayoutInflater inflater = LayoutInflater.from(ctx);
builder.setTitle("yoyo");
final View formsView=inflater.inflate(R.layout.dialog,null,false);
final EditText barang=(EditText)formsView.findViewById(R.id.textView);
final EditText harga=(EditText)formsView.findViewById(R.id.textView1);
builder.setView(formsView);
builder.setPositiveButton("OK",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
if(barang.getText().toString().equals("")&&(harga.getText().toString().equals(""))){
Toast.makeText(ctx,"first",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(ctx,"show",Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("Batal",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
dialog.cancel();
Toast.makeText(ctx,"cancel",Toast.LENGTH_SHORT).show();
}
});
builder.show();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.