2010-11-17 49 views
0

我正在編寫允許用戶維護產品列表的Android應用程序。在EnterProductData活動中,用戶可以將有關產品的信息輸入到表單域中,然後將這些信息保存到SQLite數據庫中。 EnterProductData活動還允許用戶通過Barcode Scanner應用程序啓動條形碼掃描以捕獲UPC代碼。Android - 避免在onResume與onActivityResult中設置文本字段值的衝突

我正面臨的問題是試圖在條碼掃描活動完成後設置onActivityResult()中的UPC文本字段的值並返回該值。結果發生的是我的onResume()方法正在調用一個函數(populateFields()),該函數將文本字段的值設置爲當前保存在數據庫中的任何值。並且在 onActivityResult()被調用之後,這似乎正在發生。這意味着被掃描的UPC被設置爲文本字段值,僅用於在其後立即設置一個空值。應該指責的代碼行旁邊帶有星號。

我想如果我立即將掃描的UPC保存到數據庫的onActivityResult()方法中,我可以避免這個問題,但在我看來,這似乎不是最佳實踐。有人能告訴我我應該做什麼嗎?

EnterProductData.java

public class EnterProductData extends Activity { 

    private Button mScanButton; 
    private EditText mUPC; 
    private Button mSaveButton; 
    private Long mRowId; 
    private DbAdapter mDbHelper; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mDbHelper = new DbAdapter(this); 
     mDbHelper.open(); 

     setContentView(R.layout.enter_product_data); 

     mUPC = (EditText) findViewById(R.id.UPC); 
     mScanButton = (Button) findViewById(R.id.scanButton); 
     mSaveButton = (Button) findViewById(R.id.saveButton); 

     mRowId = (savedInstanceState == null) ? null : 
     (Long) savedInstanceState.getSerializable(DbAdapter.KEY_PRODUCT_ROWID); 
     if (mRowId == null) { 
      Bundle extras = getIntent().getExtras(); 
      mRowId = extras != null ? extras.getLong(DbAdapter.KEY_PRODUCT_ROWID): null; 
     } 

     populateFields(); 

     mScanButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       IntentIntegrator.initiateScan(EnterProductData.this); 
      } 
     }); 

     mSaveButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       setResult(RESULT_OK); 
       finish(); 
      } 
     }); 

    } 

    private void populateFields() { 
     if (mRowId != null) { 
      Cursor product = mDbHelper.fetchProduct(mRowId); 
      startManagingCursor(product); 
      mUPC.setText(product.getString(
       product.getColumnIndexOrThrow(DbAdapter.KEY_UPC))); //****** 
     } 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     saveState(); 
     outState.putSerializable(DbAdapter.KEY_PRODUCT_ROWID, mRowId); 
    } 

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

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

    private void saveState() { 
    String upc= mUPC.getText().toString(); 
     if (mRowId == null) { 
      long id = mDbHelper.createProduct(mRowId, UPC); 
      if (id > 0) { 
       mRowId = id; 
      } 
     } else { 
      mDbHelper.updateProduct(mRowId, UPC); 
     } 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch(requestCode) { 
    case IntentIntegrator.REQUEST_CODE: { 
    if (resultCode != RESULT_CANCELED) { 
    IntentResult scanResult = 
    IntentIntegrator.parseActivityResult(requestCode, resultCode, data); 
    if (scanResult != null) { 
     String upc = scanResult.getContents(); 
     mUPC.setText(upc); 
    } 
    } 
    break; 
    } 
    } 
} 
} 

回答

1

您可以在啓動時即在的onCreate並將其存儲在成員變量讀取數據庫中的值。需要時更新這些值。退出應用程序時將值存儲回數據庫。這不僅可以解決您的問題,還可以防止許多數據庫交互。

+0

好吧,我已經修改了代碼,因此它執行以下操作: -In onCreate(),從數據庫讀取數據,存儲到成員變量,然後設置爲表單字段。 - 在saveState()中,表單字段的值保存回成員變量,然後成員變量作爲參數傳遞到update/createProduct()方法並保存到數據庫。 現在每當onResume被調用時,我將表單字段的值設置爲成員變量。這聽起來是對的嗎? – Keeb13r 2010-11-17 05:29:19