在onClickListener中的Activities之间传递数据

时间:2021-07-01 17:02:13

I want to get a string and send it via the onClickListener method to other activity, I already have the code in another activity where it will be catched, I get it to work by doing this:
Example: String Culo = lblMensaje.getText().toString;

我想得到一个字符串,并通过onClickListener方法将其发送到其他活动,我已经在另一个活动中获取了代码,我将通过这样做来实现它:示例:String Culo = lblMensaje.getText()的ToString;

This way I could get the text and send it to another activity, but its not as efficent as sending the original string.. Would it be posible to send it?

通过这种方式,我可以获取文本并将其发送到另一个活动,但它不像发送原始字符串那样高效。它是否可以发送它?

final String sexos[] =
                new String[] {"00311712 Biología I","Mar-Jue 7:00am Prof.Briceño","Elem3","Elem4","Elem5"};

      final String[] datos =
                 new String[]{"0031711 Lab Biologia","Lun-Mar 9:30am prof. Marcarian","Elem3","Elem4","Elem5"};



        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.pipe);

            final TextView lblMensaje = (TextView)findViewById(R.id.LblMensaje);
            final Spinner cmbOpciones = (Spinner)findViewById(R.id.CmbOpciones);

            final String[] datos =
                    new String[]{"0031711 Lab Biologia","Lun-Mar 9:30am prof. Marcarian","Elem3","Elem4","Elem5"};


            ArrayAdapter<String> pipe =
                new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, datos);


            pipe.setDropDownViewResource(
              R.layout.multiline_spinner_dropdown_item);



            cmbOpciones.setAdapter(pipe);

            cmbOpciones.setOnItemSelectedListener(
             new AdapterView.OnItemSelectedListener() {
                    public void onItemSelected(AdapterView<?> parent,
                        android.view.View v, int position, long id) {
                            lblMensaje.setText("Seleccionado: " + datos[position]);



                    }

                    public void onNothingSelected(AdapterView<?> parent) {
                        lblMensaje.setText("");
                    }

            });


            final TextView lblMensaje1 = (TextView)findViewById(R.id.LblMensaje1);
            final Spinner cmbOpciones1 = (Spinner)findViewById(R.id.CmbOpciones1);

            final String sexos[] =
               new String[] {"00311712 Biología I","Mar-Jue 7:00am Prof.Briceño","Elem3","Elem4","Elem5"};

         ArrayAdapter<String> adaptador1 =
            new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, sexos);



        adaptador1.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);



        cmbOpciones1.setAdapter(adaptador1);

        cmbOpciones1.setOnItemSelectedListener(
         new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent,
                    android.view.View v, final int position, long id) {          
                        lblMensaje1.setText(sexos[position]);




                }

                public void onNothingSelected(AdapterView<?> parent) {
                    lblMensaje1.setText("");
                }


        });
        Button BotonPasar1;
        BotonPasar1 = (Button)findViewById(R.id.VB1);
         BotonPasar1.setOnClickListener(new View.OnClickListener(){

         public void onClick(View v) {
            // TODO Auto-generated method stub


            String Culo;

       Culo = datos[position];
            String Culoq;
            Culoq = sexos[position];



            SharedPreferences mypreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = mypreferences.edit();
            editor.putString("Culo",Culo);
            editor.commit();



               SharedPreferences mypreferences1 = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
              SharedPreferences.Editor editor1 = mypreferences1.edit();
            editor1.putString("Culoq", Culoq);
            editor1.commit();

                  String Culo1 = String.valueOf(Culoq + 1);

            SharedPreferences mypreferences2 = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor2 = mypreferences2.edit();
            editor2.putString("Culo1",Culo1);
            editor2.commit();

         }

        });
         Button BotonPasar2;
         BotonPasar2 = (Button)findViewById(R.id.VB2);
          BotonPasar2.setOnClickListener(new View.OnClickListener(){

          public void onClick(View v) {
             // TODO Auto-generated method stub
           Intent pasarahorario = new Intent("com.reversiblelabs.unisvenecas.udobeta.HORARIO");                          
          startActivity(pasarahorario);

          }

         });


        }

    }

2 个解决方案

#1


0  

this may helps you

这可能对你有所帮助

like SharedPreferences in your First ActivityA

比如你的First ActivityA中的SharedPreferences

           Intent myintent= new Intent(FirstActivity.this,SecondActivity.class);
            SharedPreferences spref = this.getSharedPreferences("mynotifyid", MODE_WORLD_WRITEABLE);
            SharedPreferences.Editor spreedit = spref.edit();
            spreedit.putString("Name1", str1.toString());              
            spreedit.commit();
            startActivity(myintent);

in your Second ActivityB

在你的第二个ActivityB中

        SharedPreferences spref = context.getSharedPreferences("mynotifyid", Context.MODE_WORLD_WRITEABLE);
        String str1 = spref.getString("Name1","");

for your object saving purpose use SharedPreferences

为了节省对象,请使用SharedPreferences

#2


0  

By using intents you can just use the putextra method and catch it in the other class?

通过使用意图,您可以使用putextra方法并在其他类中捕获它?

in the onClick method where you say:

在onClick方法中你说:

Intent pasarahorario = new Intent("com.reversiblelabs.unisvenecas.udobeta.HORARIO");                          
startActivity(pasarahorario);

just add

pasarahorario.putExtra("title", lblMensaje.getText().toString);

and in the other activity you call:

在你打电话的其他活动中:

String text = getIntent().getExtras().get("title").toString();

#1


0  

this may helps you

这可能对你有所帮助

like SharedPreferences in your First ActivityA

比如你的First ActivityA中的SharedPreferences

           Intent myintent= new Intent(FirstActivity.this,SecondActivity.class);
            SharedPreferences spref = this.getSharedPreferences("mynotifyid", MODE_WORLD_WRITEABLE);
            SharedPreferences.Editor spreedit = spref.edit();
            spreedit.putString("Name1", str1.toString());              
            spreedit.commit();
            startActivity(myintent);

in your Second ActivityB

在你的第二个ActivityB中

        SharedPreferences spref = context.getSharedPreferences("mynotifyid", Context.MODE_WORLD_WRITEABLE);
        String str1 = spref.getString("Name1","");

for your object saving purpose use SharedPreferences

为了节省对象,请使用SharedPreferences

#2


0  

By using intents you can just use the putextra method and catch it in the other class?

通过使用意图,您可以使用putextra方法并在其他类中捕获它?

in the onClick method where you say:

在onClick方法中你说:

Intent pasarahorario = new Intent("com.reversiblelabs.unisvenecas.udobeta.HORARIO");                          
startActivity(pasarahorario);

just add

pasarahorario.putExtra("title", lblMensaje.getText().toString);

and in the other activity you call:

在你打电话的其他活动中:

String text = getIntent().getExtras().get("title").toString();