简体   繁体   English

我想将整数,字符串,对象,双精度,位图,哈希图从一个活动发送到android中的另一个活动

[英]I want to send integer,String,Object,double,Bitmap,HashMap from one activity to another in android

I am begnning in android , I want to send String,Object,double,ArrayList from one activity to another . 我在android中开始,我想将String,Object,double,ArrayList从一个活动发送到另一个活动。 I already done with integer but when i want to send other data like object and ArrayList i am facing problem. 我已经用整数完成了,但是当我想发送对象和ArrayList之类的其他数据时,我遇到了问题。

SenderActivity 发件人活动

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);

ReceiverActivity 接收者活动

Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0); 

Passing double data: 传递双重数据:

SenderActivity 发件人活动

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("doubleVariableName", doubleValue);
startActivity(myIntent);

ReceiverActivity 接收者活动

Intent mIntent = getIntent();
double doubleValue = mIntent.getDoubleExtra("doubleVariableName", 0.00); // set 0.00 as the default value if no value for doubleVariableName found

Passing String data: 传递字符串数据:

SenderActivity 发件人活动

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("stringVariableName", stringValue);
startActivity(myIntent);

ReceiverActivity 接收者活动

Intent mIntent = getIntent();
String stringValue = mIntent.getExtras().getString("stringVariableName");

or 要么

Intent mIntent = getIntent();
String stringValue = mIntent.getStringExtra("stringVariableName");

Passing ArrayList data : 传递ArrayList数据:

SenderActivity 发件人活动

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putStringArrayListExtra("arrayListVariableName", arrayList);
startActivity(myIntent);

ReceiverActivity 接收者活动

Intent mIntent = getIntent();
arrayList = mIntent.getStringArrayListExtra("arrayListVariableName");

Passing Object data : 传递对象数据:

SenderActivity 发件人活动

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("ObjectVariableName", yourObject);
startActivity(myIntent);

ReceiverActivity 接收者活动

Intent mIntent = getIntent();
yourObj = mIntent.getSerializableExtra("ObjectVariableName");

Note : Keep in mind your custom Class must implement the Serializable interface. 注意:请记住,自定义类必须实现Serializable接口。

Passing HashMap data : 传递HashMap数据:

SenderActivity 发件人活动

HashMap<String, String> hashMap;
Intent mIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
mIntent.putExtra("hashMap", hashMap);
startActivity(mIntent);

ReceiverActivity 接收者活动

Intent mIntent = getIntent();    
HashMap<String, String> hashMap = (HashMap<String, String>)
mIntent.getSerializableExtra("hashMap");

Passing Bitmap data : 传递位图数据:

SenderActivity 发件人活动

Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("image",bitmap);
startActivity(mIntent);

ReceiverActivity 接收者活动

Intent mIntent = getIntent();
Bitmap bitmap = mIntent.getParcelableExtra("image");

You can send object one activity to another by using Serializable class 您可以使用Serializable类将对象的一个​​活动发送到另一个活动

check following example: http://hmkcode.com/android-passing-java-object-another-activity/ 检查以下示例: http : //hmkcode.com/android-passing-java-object-another-activity/

http://www.technotalkative.com/android-send-object-from-one-activity-to-another-activity/ http://www.technotalkative.com/android-send-object-from-one-activity-to-another-activity/

you can send like: 您可以像这样发送:

    Intent intent = new Intent(mcontext, activity.class);
    intent .putExtra("Key",product);
    startActivity(intent );

and get value like: 并获得价值,例如:

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();`enter code here`
    product = (ClassName) bundle.getSerializable("Key");

You can make a bean of the data you want to send like this 您可以像这样将要发送的数据做成一个bean

public class YourBean implements Serializable {

String yourString;
Object yourObject;

double yourDouble;
ArrayList<String> yourList;

public String getYourString() {
    return yourString;
}

public void setYourString(String yourString) {
    this.yourString = yourString;
}

public Object getYourObject() {
    return yourObject;
}

public void setYourObject(Object yourObject) {
    this.yourObject = yourObject;
}

public double getYourDouble() {
    return yourDouble;
}

public void setYourDouble(double yourDouble) {
    this.yourDouble = yourDouble;
}

public ArrayList<String> getYourList() {
    return yourList;
}

public void setYourList(ArrayList<String> yourList) {
    this.yourList = yourList;
}
}

then when you want to pass data in intent do like this 然后,当您想要按意图传递数据时,请执行以下操作

Intent intent = new Intent(MainActivity.this,SecondActivity.class);
    YourBean yourBean = new YourBean();
    yourBean.setYourString("your string");
    yourBean.setYourDouble(Your double);
    yourBean.setYourObject(Your Object);
    yourBean.setYourList(array list);
    intent.putExtra("bean",yourBean);
    startActivity(intent);

then you can get it like this in your SecondActivity like this 那么你可以像这样在SecondActivity中得到它

YourBean yourBean1 = (YourBean) getIntent().getSerializableExtra("bean");

You can use intents. 您可以使用意图。 In a intent you can put all sort of data, String, int, whatever you want. 您可以随意放置各种数据,String,int。

In your case, in activity1, before going to activity2, you will store a String message this way : 对于您的情况,在活动1中,在进入活动2之前,您将以以下方式存储String消息:

Intent intent = new Intent(activity1.this, activity2.class);
intent.putExtra("message", message);
startActivity(intent);

In activity2, in onCreate(), you can get the String message by retrieving a Bundle (which contains all the messages sent by the calling activity) and call getString() on it : 在activity2的onCreate()中,您可以通过检索Bundle(其中包含调用活动发送的所有消息)来获取String消息,然后在其上调用getString():

Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");

Then you can set the text in the TextView or where you want 然后,您可以在TextView或所需的位置设置文本

Hope this helps you ! 希望这对您有帮助!

Intent mIntent = new Intent(MainActivity.this, AboutActivity.class);
ArrayList<String> mArray = new ArrayList<>();
mArray.add("Mon");
mArray.add("Tue");
mIntent.putStringArrayListExtra("Array", mArray);
startActivity(mIntent);

// In AboutActivity.java recieved data
ArrayList<String> mArray= getIntent().getExtras().getStringArrayList("Array");
Toast.makeText(AboutActivity.this, ""+mArray, Toast.LENGTH_LONG).show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中将哈希映射数组从一个活动发送到另一个活动 - How to send array of Hashmap from one activity to another activity in android 如何将 Bitmap 对象从一个活动传递到另一个活动 - How can I pass a Bitmap object from one activity to another 如何在android中将Calendar对象从一个活动发送到另一个活动? - How to send a Calendar object from one activity to another in android? 我需要不断地将数据从一个活动发送到Android中的另一个活动 - I need to constantly send data from one activity to another in Android 将对象的数组列表从一个活动发送到另一个活动 - Send an Arraylist of an object from one activity to another 我想使用json将Listview数据发送到Android中的另一个活动 - i want to send Listview Data to Another Activity in Android using json 投射HashMap <String, Integer> 与HashMap <String, Double> - Cast HashMap<String, Integer> with HashMap<String, Double> 如何在一个活动中将图像从ImageView发送到另一个活动中的ImageButton。 Android Studio - How can I send an image from an ImageView in one activity to an ImageButton in another activity. Android Studio 阅读HashMap <String, HashMap<Double, Integer> - Read HashMap<String, HashMap<Double, Integer> 从一个活动向另一个Android Studio发送两次 - Sending double from one activity to another Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM