user1234123
user1234123

Reputation: 22

swithcing to next textview

Ok im making app and it have 15 button's and 6 textview's.I want when I press first button to change value of first textview(value is "") to something (number one for example).But problem is when i press second button if first textview is already set to some value to set set second textview to second value.

If you need something else ask in comments (sorry for bad English)

here is what I was doing(this is under onclick)when i press second button

if(textview1.equals("1")){
            textview2.setText("2");}
            else if (textview1.equals("")){
            textview1.setText("2");
            }

Upvotes: 0

Views: 86

Answers (3)

Tautvydas
Tautvydas

Reputation: 2077

It sounds like you wish to show last 6 buttons pressed. Store all pressed buttons in a List (i.e. LinkedList) of size 6. Initially, it will be empty.

Then whenever any button is pressed do two things:
1) add it to the List and delete old elements if size exceeds six.
2) set button values from List.

Second step can be achieved like this:

// all TextViews should be in a list
private List<TextView> textViews;

// declare as field
private List<String> memoryQueue = new ArrayList<String>();

public void update() {
    //set fields for the pressed buttons
    for (int i=0; i<6 && i<memoryQueue.size(); i++) {
        String text = memoryQueue.get(i);
        textViews.get(i).setText(text);
    }

    // set empty fields
    for (int i = memoryQueue.size(); i<6; i++) {
        textViews.get(i).setText("");
    }
}

This code snippet assumes that you store your TextViews in a List.

And Easiest way to keep track of last six button:

public void buttonPressed(Button button) {
    //get text based on your button
    String text = button.getText();

    if (memoryQueue.contains(text)) {
         return;
    }

    memoryQueue.add(text);
    if (memoryQueue.size() > 6) {
        memoryQueue.remove(0);
    }
}

Upvotes: 1

RichS
RichS

Reputation: 943

Since you're concerned with the text inside of your text view, you should be using the object's getText method:

if( textview1.getText().equals("1") ){ // Edited
    textview2.setText("2");
} else if (textview1.getText().equals("")){ //Edited
    textview1.setText("2");
}

Upvotes: 1

Hamid Shatu
Hamid Shatu

Reputation: 9700

At first, you have to get the String text from TextView using getText() method then you can compare that String with another String. Now, change your condition as follows...

if(textview1.getText().toString().equals("1")){
    textview2.setText("2");}
else if (textview1.getText().toString().equals("")){
    textview1.setText("2");
}

Upvotes: 0

Related Questions