简体   繁体   中英

TextView isn't updating in Android

I'm making a simple "novelty" app that counts how many sneezes are done, it's more of a fun app that builds up experience until I do my huge project during the summer. I have two buttons, one adds a sneeze and the other clears how many sneezes there currently are. It holds the highest number of sneezes that there were previously. The problem is, the TextViews never update, they only initialize to zero. I used a Toast.makeText() to make sure that the buttons are working (they are). Any help is appreciated. Thanks

Java code:

public class MainActivity extends ActionBarActivity {

   private int record_number = 0;
   private int current_number = 0;

   protected void onCreate(Bundle savedInstanceState)
   {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);


        Button add_one = (Button) findViewById(R.id.addone);
        Button clear_1 = (Button) findViewById(R.id.clear);

        add_one.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                current_number += 1;
                Toast.makeText(MainActivity.this, "Button Clicked " + current_number, Toast.LENGTH_SHORT).show();
            }
        });

        clear_1.setOnClickListener(new OnClickListener() {
            public void onClick(View v)
            {
                 current_number = 0;
                 Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
            }
        });

        TextView rec_text = (TextView) findViewById(R.id.record_num);
        TextView cur_text = (TextView) findViewById(R.id.current_num);


        if (current_number >= record_number)
        {
             record_number = current_number;
        }

        rec_text.setText(String.valueOf(record_number));
        cur_text.setText(String.valueOf(current_number));

    }
 }

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.michail.sneezecounter.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Sneeze Counter"
        android:id="@+id/title"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Record Number of Sneezes:"
        android:id="@+id/textView"
        android:layout_below="@+id/title"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="72dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add one Sneeze"
        android:id="@+id/addone"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="76dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear Current Number of Sneezes"
        android:id="@+id/clear"
        android:layout_marginBottom="13dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/record_num"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:singleLine="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Current Number of Sneezes:"
        android:id="@+id/currentLabel"
        android:layout_centerVertical="true"
        android:layout_alignRight="@+id/textView"
        android:layout_alignEnd="@+id/textView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_below="@+id/currentLabel"
        android:layout_alignLeft="@+id/record_num"
        android:layout_alignStart="@+id/record_num"
        android:layout_marginTop="21dp"
        android:id="@+id/current_num" />

</RelativeLayout>

You need to update the text of the TextViews inside the onClickListeners. In fact, all your logic for counting, clearing, and recording the record needs to be done in your onClickListeners (or methods called by them). Right now you only do it once in onCreate, then never again. You can do this in onCreate:

final TextView cur_text = (TextView) findViewById(R.id.current_num);
add_one.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
        current_number += 1;
        cur_text.setText(Integer.toString(current_number);
    } 
});

And similar for the other TextView & onClickListener.

You only set the contents of your TextView s once. You should update them every time you get a click event. Specifically:

add_one.setOnClickListener(new OnClickListener()
{
    public void onClick(View v)
    {
        current_number += 1;

        if (current_number >= record_number)
        {
            record_number = current_number;
            rec_text.setText(String.valueOf(record_number));
        }

        cur_text.setText(String.valueOf(current_number));
    }
});

clear_1.setOnClickListener(new OnClickListener() {
    public void onClick(View v)
    {
         current_number = 0;
         cur_text.setText(String.valueOf(current_number));
    }
});

NOTE: if your variables current_number, record_number, cur_text, and rec_text aren't already declared as class member variables, you'll want to do that do that so that they're accessible once you leave the scope of the method you're doing all this in (I assume it's onCreate(...) .

What you are going to need to do here is update the labels during the on click events of the button. You currently only update them on activity create. This doesn't execute every time there is a click. Can I answer any questions about the fixed up version below?

   protected void onCreate(Bundle savedInstanceState)
   {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        final TextView rec_text = (TextView) findViewById(R.id.record_num);
        final TextView cur_text = (TextView) findViewById(R.id.current_num);
        Button add_one = (Button) findViewById(R.id.addone);
        Button clear_1 = (Button) findViewById(R.id.clear);

        add_one.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                current_number += 1;

                if (current_number >= record_number)
                     record_number = current_number;

                cur_text.setText(String.valueOf(current_number));
                rec_text.setText(String.valueOf(record_number));
            }
        });

        clear_1.setOnClickListener(new OnClickListener() {
            public void onClick(View v)
            {
                 current_number = 0;
                 cur_text.setText(String.valueOf(current_number));
                 rec_text.setText(String.valueOf(record_number));
            }
        });

        cur_text.setText(String.valueOf(current_number));
        rec_text.setText(String.valueOf(record_number));
    }

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