-2

I am trying to implement a contacts application. Here's the source :

call = (Button) findViewById(R.id.button3);
    message = (Button) findViewById(R.id.button4);
    call.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    registerForContextMenu(call);
                    openContextMenu(call);
                }
            });
    message.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            registerForContextMenu(message);
            openContextMenu(message);
        }
    });

Now, this is the Context menu part :

 public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(menu,v,menuInfo);
    String num1 = ((TextView)findViewById(R.id.num1)).getText().toString();
    String num2 = ((TextView)findViewById(R.id.num2)).getText().toString();
    if(v == call)
    {
        menu.setHeaderTitle("Call");
        menu.add(0,v.getId(),0,num1);
        if(num2 != "")
        {
            menu.add(0,v.getId(),0,num2);
        }
    }
    else if(v == message)
    {
        menu.setHeaderTitle("Message");
        menu.add(0,v.getId(),0,num1);
        if(num2 != "")
        {
            menu.add(0,v.getId(),0,num2);
        }
    }
}

The main functionality is , If the user has only one contact no, then only one number should be shown in the context menu. But, THIS is the problem :

The Problem

It shows a blank space if there's no 2nd contact no. And, it's clickable !

My LOGIC : I am using TextView for showing the numbers stored in database. And I am using String to store the data of the TextView. If the 2nd number's String is not null, then add the item to the context menu.

Please help me with this ! Thanks in advance.

beingadityak
  • 69
  • 13

2 Answers2

0

In java you should use

!num2.isEmpty()

instead of

num2 != ""

String comparision is done like

string1.equals(string2) ; //to match 2 strings
string.isEmpty() //to check if string is empty

in java.

Read this for more details.

Community
  • 1
  • 1
Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
0

Best one is..

  if(num2.trim().lenght()>0)
    {
        menu.add(0,v.getId(),0,num2);
    }
SRB Bans
  • 3,096
  • 1
  • 10
  • 21