简体   繁体   中英

Garbage value getting produced in textView

I have two activities : lets say A and B

A has a button which opens B using intent concept.

Through B I am saving data in database using SQLite concept.

A is the main activity which has a textView which is showing some value !

code:

Using SharedPreferences to store int value and display in MainActivity A

c=sp.getInt(Salaryflag, 0);
         str=Integer.toString(c);

        tv.setText(str);

Now I am using some algorithm to calculate sum() in one row in the database a store its value in an int variable

Now in onResume I am using this concept to change the value of the A textView ! :

@Override
    protected void onResume() {
        // TODO Auto-generated method stub
         super.onResume();
        ItemsDataBase xyx=new ItemsDataBase(this);
        xyx.open();
        int lola;
        lola=xyx.getSum();

        xyx.close();
        c=c-lola;
         str=Integer.toString(c);
        tv.setText(str);

    } 

Now what I want is that when I go back from second Acitvity B to A it should immediately show the changes.

But actually It is showing some garbage value and when I restart the app after closing then it shows the desired changes in the textView

How to remove this logical error ?

Garbage value:

文本视图中的垃圾值

After closing the app and reopening it:

在此处输入图片说明

You should write c=sp.getInt(Salaryflag, 0); in your on resume too as you are assigning c-lola to c.

When you are restarting the activity, line c=sp.getInt(Salaryflag, 0) is getting executed first so it displaying perfactly.

When you coming from B to A , the onResume() of A gets called so you have to initialize/assign variable c in onResume() as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM