2011-12-07 5 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(); 
関連する問題