2011-12-07 148 views
0

在我的项目中,当我按下确认按钮时,数据将保存在sqlite上。 我想取消作品取消按钮,不插入数据到数据库。 我该怎么办? 我正在使用onSaveInstanceState。 请帮帮我。谢谢。取消按钮被按下时的Android取消事件

cancelBtn.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      finish(); 
     } 
    }); 

protected void onSaveInstanceState(Bundle outState) 
{ 
    super.onSaveInstanceState(outState); 
    saveState(); 
    outState.putSerializable(FridgeDbAdapter.KEY_ROWID, mRowId); 
} 

@Override 
protected void onPause() 
{ 
    super.onPause(); 
    saveState(); 
} 

@Override 
protected void onResume() 
{ 
    super.onResume(); 
    populateFields(); 
} 

private void saveState() 
{ 
    String name = (String) nameEdit.getText().toString(); 
    String category = (String) categoryEdit.getText().toString(); 
    String expired_date = (String) expired_Date_Btn.getText().toString(); 
    byte[] image = ConvertDrawableToByteArray(mImageView.getDrawable()); 

    if(mRowId == null) 
    { 
     long id = mDbHelper.insertItem(category, name, expired_date, image); 

     if(id>0) 
     { 
      mRowId = id; 
     }   
    } 
    else 
    { 
     mDbHelper.updateItem(mRowId, category, name, expired_date, image); 
    } 
} 

回答

0

你可以做这样的事情:

 AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Choosing time"); 
     builder.setMessage("Do you want to save the data?"); 
     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       saveData(); //Function that saves your the data. 
      } 
     }); 
     builder.setNegativeButton("NO", 
       new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       finish();// Finish Activity. 
      } 
     }); 
     builder.create(); 
     builder.show();