0

so i have this code:

package com.sunil.phpconnect;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
        Button nao;
        Button foto;
        Button novamen;
        TextView ola;    
        HttpClient httpclient1, httpclient;
        HttpGet request1, request;
        HttpResponse response1, response;
        String url,iduser, urlmensagens;


             @Override
             public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              Bundle  extras = getIntent().getExtras();
              iduser= extras.getString("userid");


               TextView result = (TextView) findViewById(R.id.tvResult);
               //String g = result.getText().toString();
              // String h = "valor";

               urlmensagens = ("http://mywebsite.php?iduser="+iduser);
               novamen = (Button) findViewById(R.id.mensagens);


                   //cenas da net
                    try {
                     httpclient1 = new DefaultHttpClient();
                     request1 = new HttpGet(urlmensagens);
                     response1 = httpclient1.execute(request1);

                    }
                    catch (Exception e){

                    }
                    try{ 
                      BufferedReader dr = new BufferedReader(new InputStreamReader(
                        response1.getEntity().getContent()));


                      String mensage = "";


                      mensage = dr.readLine();

                      String check_sms = mensage;

                      Toast.makeText(getApplicationContext(), check_sms,
                              Toast.LENGTH_LONG).show();



                //novamen.setText(check_sms + " Mensagens por ler!");





                      switch(check_sms) {
                      case "b":
                          novamen.setVisibility(View.GONE);
                          break;
                      case "a":
                          novamen.setVisibility(View.VISIBLE);
                          break;
                      default:
                          novamen.setVisibility(View.GONE);
                  }


                    } catch (Exception e) {
                          // Code to handle exception  

                         }

              url = ("http://mywebsite.php?action=requestuserdata&userid="+iduser);


              try {
               httpclient = new DefaultHttpClient();
               request = new HttpGet(url);
               response = httpclient.execute(request);
              }

              catch (Exception e) {
               }


              try {
               BufferedReader rd = new BufferedReader(new InputStreamReader(
                 response.getEntity().getContent()));
               String line = "";
               line = rd.readLine();



                   if(line == null){
                       Toast.makeText(getApplicationContext(), "Numero nao atribuido.",
                                  Toast.LENGTH_LONG).show();
                       Intent wowmuch = new Intent(getApplicationContext(), pin.class);
                     startActivity(wowmuch);    
                   }else{
                       result.append(line);

                   }

              } catch (Exception e) {

              }

              novamen = (Button) findViewById(R.id.mensagens);
                novamen.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                            chamaMensagens();
                            }

                        public void chamaMensagens () {

                            Intent mens = new Intent(getApplicationContext(),mensagens.class);
                            mens.putExtra("userid", iduser);
                            startActivity(mens);}});


              foto = (Button) findViewById(R.id.button1);
              foto.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    Chamafoto();
                }
                public void Chamafoto() {

                    Intent wowmuch = new Intent(getApplicationContext(), Foto.class);
                    wowmuch.putExtra("userid", iduser);        
                    startActivity(wowmuch);
                }
            });
              Toast.makeText(getApplicationContext(), iduser, 
                      Toast.LENGTH_SHORT).show();
              nao = (Button) findViewById(R.id.button2);
                nao.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                            chamaConsulta();
                            }
                    }
                );
                }

                        public void chamaConsulta () {

                            Intent wowmuch = new Intent(getApplicationContext(),pin.class);

                            startActivity(wowmuch); 
                        }


}

when i get to this part:

switch(check_sms) {
                          case "b":
                              novamen.setVisibility(View.GONE);
                              break;
                          case "a":
                              novamen.setVisibility(View.VISIBLE);
                              break;
                          default:
                              novamen.setVisibility(View.GONE);
                      }

The button is supost to be invisible/gone when whe variable "check_sms" value is "b".

I tested if the variable is showing and it is in the Toast.

I tried with if statement too and doesn't do nothing.

6
  • 1
    switch on String?? go for Enum... and I guess you have compiled src code with jdk1.7.. compile it using jdk1.6.. Commented May 22, 2014 at 8:55
  • 1
    @DerGolem that was my thought too but if you look at the bottom they say that an if statement did not work either. I wonder could it be a case issue, are you getting B and checking it against b, i.e. try an if using .equalsIgnoreCase() Commented May 22, 2014 at 8:57
  • 1
    You can use characters for switch case. Get character for your string and add cases like case 'a' case 'b'. Commented May 22, 2014 at 8:58
  • @feldoh: Because they probably tried if (check_sms == "a"), instead of using the equals() method Commented May 22, 2014 at 8:58
  • thats a good point too +1 Commented May 22, 2014 at 9:03

4 Answers 4

5

switch(someInteger) will work.
switch(someChar) will work.
switch(someEnum) will work.
switch(someString) will work only in JAVA 1.7.

As far as I know, Java 1.7 isn't yet supported by Android.

Sign up to request clarification or add additional context in comments.

6 Comments

i want to use Int insted String, but my code to connect to url and get data is getting on String variable: BufferedReader dr = new BufferedReader(new InputStreamReader( response1.getEntity().getContent())); String mensage = ""; mensage = dr.readLine();
You can convert a character to an ASCII code. So, 'a' would be 97, 'b' would be 98, ... So, once you have your char: int code = (int) character;. Then use switch(code) - OR simply use switch(character) to save the integer conversion.
So i can do the convertion from String to Int like this : int code = (int) mensage; ?
Obviously, NOT. mensage is a String. You could take a character out of it (say the first) and use it. Such as: char character = mensage.toLowerCase().charAt(0); But my best choice would be an int or an Enum. Since mensage could contain a key word that could make the difference between switching a case rather than another.
i put character = mensage.toLowerCase().charAt(1); and it works. In position 0 is giving me blank value
|
0

switch on String?? go for Enum... and I guess you have compiled src code with jdk1.7.. compile it using jdk1.6... jdk1.7 isn't yet supported..

Comments

0

You can use string in java 1.7 only as Der Golem specified. You can use enum, char, int types. You can refer http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html for more info.

From the above link

Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).

Comments

0

Of course you can use a swich with Java 7, but in this case (only 2 options), I will use

if ( "a".equalsIgnoreCase( check_sms ) ){
   ....
} else {
   ....
}

Never use check_sms == "a" for this purpose.

6 Comments

your solution is not working. i previously switch have this code: if(check_sms.equals("a")){...}else{...} but someting apens when i try to switch aplication properties encode to ISO-8859-1
Check the check_sms value with a trace, please.
how do i do a trace ?
Just before the if (your swich) write System.out.println( check_sms ); This will help us to see the value
System.out.println( check_sms ); didn't do nothing, but i put it in a Toast and it shows. I have my problem solved. Tnks all
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.