0

in an activity I load some text values from a custom object.

These text values go into textViews, the textViews are displayed on screen.

I have previous and next buttons that, when tapped, should refresh the textViews with the new values.

Here is some code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    myObj = (Object)getIntent().getSerializableExtra("Stuff");

    myTextView.setText(myObj.currentData().getText());

    prevButton = (Button) findViewById(R.id.prevButton);
    nextButton = (Button) findViewById(R.id.nextButton);

        prevButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.d("TAG", "prevButton tapped");
            myObj.prevData();
                            //Would like to call refresh view here
        }
    });

    nextButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.d("TAG", "nextButton tapped");
            myObj.nextData();   
                            //Would like to call refresh view here too
        }
    });

 }

Now I notice that when I hit the previous or next buttons and then rotate the device, the view updates. I am trying to reproduce that without rotating the device.

Thanks

M Jesse
  • 2,213
  • 6
  • 31
  • 37
  • Why do you want to refresh the views? Just asking, because they'll get updated on the next pass if you change the text normally. – Tushar Mar 12 '13 at 00:10
  • To have the textView show the new text. Right now the prev and next buttons point the myObj's current data (an array), either previous or next. The textView should display whatever currentData() is pointing to. – M Jesse Mar 12 '13 at 00:12

1 Answers1

1

You can keep it simple and do something like this:

public void onCreate(Bundle onSavedInstanceState) {
     // Other code
     refreshTextView();
}

public void onClick(View v) {
     // Advance currentData()
     refreshTextView();
}

private void refreshTextView() {
     myTextView.setText(myObj.currentData().getText());
}
Tushar
  • 8,019
  • 31
  • 38