huang
huang

Reputation: 89

android textview value

I have some problem

I want to change my textview value. when my code change it will change

here is the code

public class SubMenuActivity extends Activity {
private static final int GALLERY = 0;
private static final int SUBMANU01 = 7;
private static final int MANU01 = 1;
private static final int MANU02 = 2;
private static final int MANU03 = 3;
private static final int MANU04 = 4;
private static final int MANU05 = 5;
TextView tx1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tx1 =(TextView)this.findViewById(R.id.textView1);
   if(tx1.toString()=="1".toString())
   {
       tx1.setText("7");
   }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    SubMenu fileMenu = menu.addSubMenu(GALLERY, SUBMANU01, Menu.NONE, "File");    
    fileMenu.add(GALLERY, MANU01, Menu.NONE, "new");  
    fileMenu.add(GALLERY, MANU02, Menu.NONE, "open");  
    fileMenu.add(GALLERY, MANU03, Menu.NONE, "save");  
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MANU01:
    case MANU02:
    case MANU03:
    final   String itemid = Integer.toString(item.getItemId());
         tx1.setText(itemid);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

tx1.text value did not show 7 ,where is the problem? I hope someone could tell me the problem.

Upvotes: 1

Views: 6475

Answers (6)

user1292458
user1292458

Reputation:

first of you should print the value of txt1.

System.out.println("value of tx1:"+tx1.getText.toString());
if(tx1.getText().toString().equals("1"))

 {
   tx1.setText("7");
 }

Upvotes: 0

kkker
kkker

Reputation: 1

in java "==" means the address is the same, instead, you can use .equel() which comes from the basic class "object".

Upvotes: 0

raghav chopra
raghav chopra

Reputation: 837

You need to Compare the variable like.equal(Object/String)

if(tx1.toString().equals("1"))
 {
   tx1.setText("7");
 }

Upvotes: 0

Umesh
Umesh

Reputation: 4256

Instead of

if(tx1.toString()=="1".toString())    {
    tx1.setText("7");   
} 

try this

if(tx1.getText().toString().equals("1"))    { 
   tx1.setText("7");    
} 

Upvotes: 1

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

compare like this

 if(tx1.getText().toString().equals("1"))
 {
   tx1.setText("7");
 }

Upvotes: 5

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21181

Strings can not be compared with == operator they can be compared with .equals method

so change your code to this

if(tx1.toString().equals("1"))
 {
   tx1.setText("7");
 }

Upvotes: 1

Related Questions