简体   繁体   中英

Android Passing values from one activity to other activity (string) or from one class to another

I have two class Profile.class and Details.class,

In profile class i have used a spinner with values like (ATM,Banking,Personal,Others etc) and a button (OK). on clicking ok button it will go to next activity that is details activity where i will be taking some details like-name,description etc.

after filling the details i have given a button (save). on clicking button save i will be saving the name and description in database but i want to save the profile name also along with details. i am unable to transfer selected spinner text from Profile.class to Details.class

how to transfer?

create.class code

public class Create extends Activity {

    public ArrayList<String> array_spinner;
    Button button4;
    String spinnertext;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create);
        Spinner spinner = (Spinner) findViewById(R.id.spinner1);
        array_spinner=new ArrayList<String>();
        array_spinner.add("ATM");
        array_spinner.add("Bank");
        array_spinner.add("Mail");


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, array_spinner);
        adapter.setNotifyOnChange(true);
        spinner.setAdapter(adapter);

        spinner.setLongClickable(true);
        spinner.setOnLongClickListener(new OnLongClickListener(){

            public boolean onLongClick(View v) {
                // TODO Auto-generated method stub


                return false;

       }}
       );


        button4 = (Button)findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent4 = new Intent(view.getContext(), Details.class); 
                startActivityForResult(myIntent4, 0);

                myIntent4 .putExtra("key", array_spinner.getSelectedItem().toString());
                startActivity(myIntent4);
            }

        });

}}

details.class code

public class Details extends Activity {

EditText editText4,editText5,editText6;
Button button8,button9,button10;
TextView textView7;
String et4,et5,et6;
//SQLite Database db;


/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.details);

    String spinnervalue = getIntent().getExtras().getString("Key");

please kindly explain me what is this "key"?

首先选择一个微调器,然后向其提供所需的值,然后选定的微调器值将其更改为字符串值,然后在“确定”按钮中使用此字符串变量通过使用IntentShared首选项将值传递给该值另一个活动,然后您可以在数据库中使用它来显示此值。

If you want to send data to another activity, you can do it using intent .

Bundle bund = new Bundle();

bund.putString("myKey",name);

Intent intent = new Intent(Profile.this, Detail.class);

intent.putExtras(bund);

startActivity(intent);

Now in Detail class, receive this data in onCreate()

    @Override
protected void onCreate(Bundle savedInstanceState) {
      .......

    String nameReceived = getIntent().getExtras().getString("myKey");
}

I have given the example of passing String to another activity however, you can pass boolean, int, double etc to another activity. See the full list on here

You can use :

    Intent i = new Intent(MainActivity.this,SecondActivity.class);
    i.putExtra("YourValueKey", yourData.getText().toString());

then you can get it from your second activity by :

    Intent intent = getIntent();
    String YourtransferredData = intent.getExtras().getString("YourValueKey");

example

this is what you have to write in your first activity

 Intent i = new Intent(getApplicationContext(), Product.class);
 i.putExtra("productname", ori);
 i.putExtra("productcost", position);
 i.startActivityForResult(i,0);

then in your next activity you need to have this code

 String productname,productcost;

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

    tv1= (TextView)findViewById(R.id.tv1);
    tv2= (TextView)findViewById(R.id.tv2);

     Bundle extras= getIntent().getExtras();
     if(extras!=null)
     {
        position = extras.getString("position"); // get the value based on the key
        tv1.setText(productname);//use where ever you want
        productname = extras.getString("productname"); // get the value based on the key 
        tv2.setText(productname);

    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM