Chulbul Pandey
Chulbul Pandey

Reputation: 506

How to Store value for EditText in Session

In Cart Activity, i am allowing user to Edit Quantity of an item, and also getting changes in Total amount as user does change in Quantity, but here i am facing a small problem, whenever i do click on back button, it will reset my quantity, why?

please see below screen shot:

![enter image description here][1]

For an example, in above screen i have edited Quantity for my product Veggie from 1 to 15 and also getting change in Total, but the problem is once i do click on back button, then i will get again value for Quantity 1 not 15, which i have entered earlier

Please tell me how can i control on this ?

CartAdapter.java:

    if(cost.getText().length() > 0)
    {
        try
        {
           itemamount = Double.parseDouble(cost.getText().toString());
           Log.d("ProductInformationActivity", "ItemAmount :: " + itemamount);
        }
        catch(NumberFormatException e)
        {
            itemamount=0.00;
            Log.d("ProductInformationActivity", "NumberFormatException :: " + e);
        }
    }

    qty.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            if (!qty.getText().toString().equals("")
                    || !qty.getText().toString().equals("")) {

                // accept quantity by user
                itemquantity = Integer.parseInt(qty.getText()
                        .toString());

                total.setText(new DecimalFormat("##.#").format(itemamount*itemquantity));

            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        });

    return vi;      
}       
  }

Upvotes: 0

Views: 194

Answers (3)

Joaquin Fernandez
Joaquin Fernandez

Reputation: 63

If you are already storing the value and just want to control how the back button works you could use this in your cart activity:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        //Your code
        return true; // so the super method doesn't get called
    }
    return super.onKeyDown(keyCode, event);
}

Upvotes: -1

Srikanth Pai
Srikanth Pai

Reputation: 926

using SQLite DB looks like the best option considering the functionality you require...

Upvotes: 0

MarsAtomic
MarsAtomic

Reputation: 10673

Why don't you save your values and populate them in your onCreate()? You can use any of three mechanisms to get what you want.

  1. Shared Preferences
  2. SQLite DB
  3. Application

Upvotes: 4

Related Questions