简体   繁体   English

在textView中产生垃圾值

[英]Garbage value getting produced in textView

I have two activities : lets say A and B 我有两个活动:假设AB

A has a button which opens B using intent concept. A有一个使用intent概念打开B的按钮。

Through B I am saving data in database using SQLite concept. 通过B,我使用SQLite概念将数据保存在数据库中

A is the main activity which has a textView which is showing some value ! A是具有textView的主要活动,它显示了一些价值!

code: 码:

Using SharedPreferences to store int value and display in MainActivity A 使用SharedPreferences存储int值并在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 现在我正在使用某种算法在数据库的一行中计算sum()并将其值存储在int变量中

Now in onResume I am using this concept to change the value of the A textView ! 现在在onResume我正在使用此概念来更改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. 现在我想要的是,当我从第二个Acitvity B返回A时,它应该立即显示更改。

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 但实际上它显示了一些垃圾值,当我关闭后重新启动应用程序时,它在textView显示了所需的更改

How to remove this logical error ? 如何清除此逻辑错误?

Garbage value: 垃圾价值:

文本视图中的垃圾值

After closing the app and reopening it: 关闭应用程序并重新打开后:

在此处输入图片说明

You should write c=sp.getInt(Salaryflag, 0); 您应该写c = sp.getInt(Salaryflag,0); in your on resume too as you are assigning c-lola to c. 您也在简历中分配c-lola到c。

When you are restarting the activity, line c=sp.getInt(Salaryflag, 0) is getting executed first so it displaying perfactly. 重新启动活动时,将首先执行c = sp.getInt(Salaryflag,0)行,以便准确显示。

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. 当您从B到A时,A的onResume()被调用,因此您还必须在onResume()中初始化/分配变量c。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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