Parameter passing between Activity

1, a first, simple transfer.

send:

switch (v.getId()) {

case R.id.btn1:

EditText editText = findViewById(R.id.editText);

Intent intent = new Intent(this, my1Activity.class);

intent.putExtra("data", editText.getText().toString());

startActivity(intent);

break;

receive:

Intent intent=getIntent();

String str1 = intent.getStringExtra("data");

TextView textView=findViewById(R.id.textView3);

textView.setText(str1);

2, second: use bundle. Forex MT4 Tutorial

send:

Bundle bundle=new Bundle();

bundle.putString("data1",editText.getText().toString()+editText.getText().toString());

intent.putExtras(bundle);

startActivity(intent);

receive:

Bundle bundle = getIntent () getExtras (),.;

textView1.setText(bundle.getString("data1").toString());

3, relatively complex:

The sender:

case R.id.btn1:

EditText editText = findViewById(R.id.editText);

Intent intent = new Intent(this, my1Activity.class);

//definition.

Mapmap=new HashMap<>();

map.put("key1","Value1");

map.put("key2","Value1");

List> list=new ArrayList<>();

list.add(map);

// must be defined for transmitting a list of required transfer budnle

ArrayList<Object>,这个是必须要的!
Bundle bundle=new Bundle();
ArrayList bundlelist=new ArrayList();
bundlelist.add(list);
bundle.putParcelableArrayList("list",bundlelist);
intent.putExtras(bundle);
startActivity(intent);
break;

receive:

// receive parameters
the Bundle the bundle its getIntent = () getExtras ();.
The ArrayList list = bundle.getIntegerArrayList ( "list");
// in the list of parameters, the conversion
List <Map <String, Object >> lists = (List <Map <String, Object >>) list.get (0);

String sResult = "";
for (Map<String, Object> m : lists) {
for (String k : m.keySet()) {
sResult += "\r\n" + k + " : " + m.get(k);
}
}

TextView textView=findViewById(R.id.textView3);
TextView textView1=findViewById(R.id.textView4);
textView1.setText(sResult);

4, define global variables:
Definition:

public class my_data{
public static String m1;
public static Integer d1;
}
使用:


textView.setText(my_data.m1);

Guess you like

Origin www.cnblogs.com/benming/p/12468090.html
Recommended