如何使数字保持最新的数字,而不是返回到默认数字?

时间:2018-09-28 06:30:32

标签: android

我的TxnNo(A0010001)来自单位代码(A001)+ LastTxnNo(0001)。这是我的按钮点击。

Db_Save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Model txn = new Model();// initialize your model class first
                BigDecimal paidAmt = new BigDecimal(D_Amount.getText().toString()).setScale(2, RoundingMode.HALF_UP);
                txn.setName(D_Name.getText().toString());
                txn.setTxnNo(D_Txn.getText().toString());
                txn.setTxnDate(Select_Date.getText().toString());
                txn.setAmount(paidAmt);
                txn.setDescription1(D_Description.getSelectedItem().toString());
                txn.setDescription2(Ds_Description.getText().toString());

                try {

                    SQLiteDatabase db = mSQLiteHelper.getWritableDatabase();
                    ContentValues cv = new ContentValues();
                    cv.put("name", txn.getName());
                    cv.put("TxnNo", txn.getTxnNo());
                    cv.put("TxnDate", txn.getTxnDate());
                    cv.put("Amount", txn.getAmount().toPlainString());
                    cv.put("Description1", txn.getDescription1());
                    cv.put("Description2", txn.getDescription2());
                    db.insert("Donation_Details", null, cv);
                    db.close();
                    increaseNumber++;
                    populate_TxnNo();
                    Toast.makeText(Donation_Page.this, "Add successfully", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }



            }
        });

我输入了increaseNumber++;以确保它会自动增加。所以每次我单击按钮。该数字会将“ A0010001”更改为“ A0010002”,依此类推。但是一旦我离开此页面,然后再次进入。它将返回到“ A0010001”。如何使其保持最新状态,而不是返回到“ A0010001”。 这是设置编号的方法。

 private void populate_TxnNo() {

        int lastNumber = Integer.valueOf(txn.getLastTxnNo());
        lastNumber = lastNumber + increaseNumber;
        String stringLast = String.format("%04d", lastNumber);
        String txnNo = txn.getUnit_Code() + stringLast;
        //increaseNumber++;
        D_Txn.setText(txnNo);
    }

更新

因此,为了让我理解共享的偏好设置,我将这些代码添加到了新按钮上。

Db_New.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                increaseNumber++;
                populate_TxnNo();

            }
        });

但是,即使我看到了代码示例,我也不知道如何使用共享首选项 ...有人可以为我解释吗?如何使用此代码?

SharedPreferences prefs;
    SharedPreferences.Editor edit;
prefs=this.getSharedPreferences("yourPrefsKey",Context.MODE_PRIVATE);

        edit=prefs.edit();  
edit.putString("yourKey", txnNo);
            edit.commit();  

String txnNo = prefs.getString("yourKey", null);

3 个答案:

答案 0 :(得分:1)

尝试使用共享首选项

onCreate()

之前的这两行
 SharedPreferences prefs;
    SharedPreferences.Editor edit;  

onCreate()中的这两行

prefs=this.getSharedPreferences("yourPrefsKey",Context.MODE_PRIVATE);
            edit=prefs.edit();  

并在onPause()上保存字符串

 @Override
protected void onPause() {
    super.onPause();
    edit.putString("yourKey", txnNo);
    edit.commit();
}

要访问它,只需在onStart中编写

@Override
    protected void onStart() {
        super.onStart();
        String txnNo = prefs.getString("yourKey", null);
    }

答案 1 :(得分:1)

首先,声明这两个变量

SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
int count;

接下来用onCreate方法添加它们

sharedPreferences = getApplicationContext().getSharedPreferences("counters", MODE_PRIVATE);
editor = sharedPreferences.edit();
count = sharedPreferences.getInt("counter", 0);

最后修改populate_TxnNo方法

  private void populate_TxnNo() {

        int a = count++;
        int lastNumber = Integer.valueOf(txn.getLastTxnNo());
        lastNumber = lastNumber + a;
        String stringLast = String.format("%04d", lastNumber);
        String txnNo = txn.getUnit_Code() + stringLast;
        D_Txn.setText(txnNo);
        editor.putInt("counter", ++a);
        editor.commit();
    }

答案 2 :(得分:0)

只需将您的号码添加到shared preferences

int yourNumber;

SharedPreferences number = getSharedPreferences("number", MODE_PRIVATE);

保存您的电话号码

number.edit.putInt("number",yourNumber).apply;

并在下一次阅读该号码

int savedNumber = number.getInt("number",0);