Search code examples
androidandroid-sqlite

How to do a increment for the String? I put two String together But I only want lastTxnNo to be increase. Android studio


my txn.getUnit_Code() is "A001" and txn.getLastTxnNo() is "0001". Both are String. I want to increase the last txn number whenever I save the record. But since this is string, so I put + 1, It will become "A00100011" instead of "A0010002". If I want to make it "A0010001" to "A0010002", How to do it?

String txnNo = txn.getUnit_Code() + txn.getLastTxnNo() + 1;
        D_Txn.setText(txnNo);

this is the button save. So I put the above code to make it to be "A0010001", but I don't know how to make it after I click the button, The next txnNo becomes "A0010002"

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();

                    Toast.makeText(Donation_Page.this, "Add successfully", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }

Can I have a help...? Thanks in advance.

This is my Update

I initialized an int increaseNumber = 1; at the page and I put increaseNumber++ at the button click. But It doesn't work. why?

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();

                    Toast.makeText(Donation_Page.this, "Add successfully", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                increaseNumber++;
            }
        });

and this is the format that I set

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;
    D_Txn.setText(txnNo);


}

Solution

  • You cannot give int increments to a string. What you can do is:

    String numberPrefix = txn.getUnit_Code(); //String = A001
    int numberSufix = 1;
    
    String sufixAndPrefix = numberPrefix + "000" + numberSufix;
    
    //OnClickListener
    numberSufix++;
    D_Txn.setText(sufixAndPrefix);