【问题标题】:how to pass a String to another String variable in another class?如何将字符串传递给另一个类中的另一个字符串变量?
【发布时间】:2015-03-01 16:13:32
【问题描述】:

我有这个:

 @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String urlCorrecta="";


        //Si se escoge la posicion de el arreglo 0 abre la clase LasVegas
        if (position == 0)
        {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            urlCorrecta = "souphttpsrc location=http://69.54.28.188:7878/mjpg/video.mjpg ! multipartdemux single-stream=true ! jpegdec ! autovideosink sync=false ";


        }

        //Si se escoge la posicion de el arreglo 1 abre la clase LosGuayabos
        else if (position == 1)

        {
            Intent intent = new Intent(this, LosGuayabos.class);
            startActivity(intent);

            urlCorrecta = "souphttpsrc location=http://rax1.bsn.net/mjpg/video.mjpg?streamprofile=Balanced ! multipartdemux single-stream=true ! jpegdec ! autovideosink sync=false ";

        }

        //Si se escoge la posicion de el arreglo 2 abre la clase RegionalNorte
        else if (position == 2)

        {
            Intent intent = new Intent(this, RegionalNorte.class);
            startActivity(intent);

            urlCorrecta = "souphttpsrc location=http://trackfield.webcam.oregonstate.edu/mjpg/video.mjpg ! multipartdemux single-stream=true ! jpegdec ! autovideosink sync=false ";
        }


        //Si se escoge la posicion de el arreglo 3 abre la clase RegionalSur
        else if (position == 3)

        {
            Intent intent = new Intent(this, RegionalSur.class);
            startActivity(intent);

            urlCorrecta = "souphttpsrc location=http://wmccpinetop.axiscam.net/mjpg/video.mjpg ! multipartdemux single-stream=true ! jpegdec ! autovideosink sync=false ";

        }

    }

我想将字符串 urlCorrecta 的值传递给位于 MainActivity 类中的字符串 VIDEO_IN_PIPELINE。

public class MainActivity extends ActionBarActivity implements SurfaceHolder.Callback{




    private final String VIDEO_IN_PIPELINE = VALUE OF THE STRING urlCorrecta;
    private final String HOST_IP = "192.168.1.167";

}

我怎样才能把其他类的字符串带到这个类?

非常感谢您的阅读。

【问题讨论】:

  • 使用Intent.putExtra 将值发送到下一个活动

标签: java android string class


【解决方案1】:

从一个活动发送字符串,

Intent intent = new Intent(CurrentActivity.this, ReceiverActivity.class);
intent.puExtra("key","String Value");
startActivity(intent);

ReceiverActivity中获取字符串

Intent intent = getIntent();
String str = intent.getExtra().getString("key");

【讨论】:

    【解决方案2】:

    使用附加功能:

    Intent intent = new Intent(this, LosGuayabos.class);
    intent.putExtra("myString", myString);
    startActivity(intent);
    

    那么你只需要在你的 onCreate 方法中获取意图:

    String myString = getIntent().getExtras().getString("myString");
    

    【讨论】:

      【解决方案3】:

      要将其传递给另一个Activity,请将其作为额外的添加到您的新Intent

      Intent intent = new Intent(this, MainActivity.class);
      intent.putExtra("urlCorrecta", "souphttpsrc location=http://rax1.bsn.net/mjpg/video.mjpg?streamprofile=Balanced ! multipartdemux single-stream=true ! jpegdec ! autovideosink sync=false ");
      startActivity(intent);
      

      MainActivityonCreate()中检索它:

      String urlCorrecta = getIntent().getStringExtra("urlCorrecta");
      

      试试这个。这会起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-13
        • 2015-05-23
        • 1970-01-01
        相关资源
        最近更新 更多