我正在尝试从一个“活动”发送客户类的对象,并在另一个“”中显示它。
客户类别的代码:
public class Customer {
private String firstName, lastName, address;
int age;
public Customer(String fname, String lname, int age, String address) {
firstName = fname;
lastName = lname;
age = age;
address = address;
}
public String printValues() {
String data = null;
data = "First Name :" + firstName + " Last Name :" + lastName
+ " Age : " + age + " Address : " + address;
return data;
}
}
我想将其对象从一个“活动”发送到另一个“,然后在另一个活动”上显示数据。
我怎样才能做到这一点?
我使用parcelable将数据从一个活动发送到另一个活动。这是我的代码,在我的项目中运行良好。
public class Channel implements Serializable, Parcelable {
/** */
private static final long serialVersionUID = 4861597073026532544L;
private String cid;
private String uniqueID;
private String name;
private String logo;
private String thumb;
/**
* @return The cid
*/
public String getCid() {
return cid;
}
/**
* @param cid
* The cid to set
*/
public void setCid(String cid) {
this.cid = cid;
}
/**
* @return The uniqueID
*/
public String getUniqueID() {
return uniqueID;
}
/**
* @param uniqueID
* The uniqueID to set
*/
public void setUniqueID(String uniqueID) {
this.uniqueID = uniqueID;
}
/**
* @return The name
*/
public String getName() {
return name;
}
/**
* @param name
* The name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the logo
*/
public String getLogo() {
return logo;
}
/**
* @param logo
* The logo to set
*/
public void setLogo(String logo) {
this.logo = logo;
}
/**
* @return the thumb
*/
public String getThumb() {
return thumb;
}
/**
* @param thumb
* The thumb to set
*/
public void setThumb(String thumb) {
this.thumb = thumb;
}
public Channel(Parcel in) {
super();
readFromParcel(in);
}
public static final Parcelable.Creator<Channel> CREATOR = new Parcelable.Creator<Channel>() {
public Channel createFromParcel(Parcel in) {
return new Channel(in);
}
public Channel[] newArray(int size) {
return new Channel[size];
}
};
public void readFromParcel(Parcel in) {
String[] result = new String[5];
in.readStringArray(result);
this.cid = result[0];
this.uniqueID = result[1];
this.name = result[2];
this.logo = result[3];
this.thumb = result[4];
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] { this.cid, this.uniqueID,
this.name, this.logo, this.thumb});
}
}
在活动A中,如下所示:
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("channel",(ArrayList<Channel>) channels);
Intent intent = new Intent(ActivityA.this,ActivityB.class);
intent.putExtras(bundle);
startActivity(intent);
在ActivityB中,使用如下方法获取数据:
Bundle getBundle = this.getIntent().getExtras();
List<Channel> channelsList = getBundle.getParcelableArrayList("channel");
不可能序列化任何类型的对象。例如,不能序列化携带代码而不是数据的委托方法或接口。因此,我编写了一个“Box”类,您可以使用它来传递任何类型的数据,而无需序列化。
1-将数据用于预期用途:
Intent I = new Intent(this, YourActivity.class);
CustomClass Date = new CustomClass();
Box.Add(I, "Name", Data);
2-用于从意向检索数据:
CustomClass Data = Box.Get(getIntent(), "Name");
3-要在使用后删除数据,请将此方法添加到活动中:
@Override
protected void onDestroy() {
Box.Remove(getIntent());
super.onDestroy();
}
4-并将此代码添加到项目中:
package ir.namirasoft.Utility;
import android.content.Intent;
import java.util.HashMap;
import java.util.Vector;
public class Box {
// Number
private static int Number = 1;
public static int NextNumber() {
return Number++;
}
//
private static String _Intent_Identifier = "_Intent_Identifier";
private static HashMap<Integer, Vector<Integer>> DeleteList = new HashMap<Integer, Vector<Integer>>();
private static HashMap<Integer, HashMap<String, Object>> ObjectList = new HashMap<Integer, HashMap<String, Object>>();
public static int GetIntent_Identifier(Intent I) {
int Intent_Identifier = I.getIntExtra(_Intent_Identifier, 0);
if (Intent_Identifier == 0)
I.putExtra(_Intent_Identifier, Intent_Identifier = NextNumber());
return Intent_Identifier;
}
public static void Add(Intent I, String Name, Object O) {
int Intent_Identifier = GetIntent_Identifier(I);
synchronized (ObjectList) {
if (!ObjectList.containsKey(Intent_Identifier))
ObjectList.put(Intent_Identifier, new HashMap<String, Object>());
ObjectList.get(Intent_Identifier).put(Name, O);
}
}
public static <T> T Get(Intent I, String Name) {
int Intent_Identifier = GetIntent_Identifier(I);
synchronized (DeleteList) {
DeleteList.remove(Intent_Identifier);
}
return (T) ObjectList.get(Intent_Identifier).get(Name);
}
public static void Remove(final Intent I) {
final int Intent_Identifier = GetIntent_Identifier(I);
final int ThreadID = NextNumber();
synchronized (DeleteList) {
if (!DeleteList.containsKey(Intent_Identifier))
DeleteList.put(Intent_Identifier, new Vector<Integer>());
DeleteList.get(Intent_Identifier).add(ThreadID);
}
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(60 * 1000);
} catch (InterruptedException e) {
}
synchronized (DeleteList) {
if (DeleteList.containsKey(Intent_Identifier))
if (DeleteList.get(Intent_Identifier).contains(ThreadID))
synchronized (ObjectList) {
ObjectList.remove(Intent_Identifier);
}
}
}
}).start();
}
}
**Box类是线程安全的。
使用Serializable实现类。假设这是您的实体类:
import java.io.Serializable;
@SuppressWarnings("serial") //With this annotation we are going to hide compiler warnings
public class Deneme implements Serializable {
public Deneme(double id, String name) {
this.id = id;
this.name = name;
}
public double getId() {
return id;
}
public void setId(double id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
private double id;
private String name;
}
我们正在将名为dene的对象从X活动发送到Y活动。在X活动的某处;
Deneme dene = new Deneme(4,"Mustafa");
Intent i = new Intent(this, Y.class);
i.putExtra("sampleObject", dene);
startActivity(i);
在Y活动中,我们正在获取对象。
Intent i = getIntent();
Deneme dene = (Deneme)i.getSerializableExtra("sampleObject");
就是这样。
我创建了一个保存临时对象的单例助手类。
public class IntentHelper {
private static IntentHelper _instance;
private Hashtable<String, Object> _hash;
private IntentHelper() {
_hash = new Hashtable<String, Object>();
}
private static IntentHelper getInstance() {
if(_instance==null) {
_instance = new IntentHelper();
}
return _instance;
}
public static void addObjectForKey(Object object, String key) {
getInstance()._hash.put(key, object);
}
public static Object getObjectForKey(String key) {
IntentHelper helper = getInstance();
Object data = helper._hash.get(key);
helper._hash.remove(key);
helper = null;
return data;
}
}
不要将对象放在Intent中,而是使用IntentHelper:
IntentHelper.addObjectForKey(obj, "key");
在新的“活动”中,您可以获取对象:
Object obj = (Object) IntentHelper.getObjectForKey("key");
请记住,一旦加载,对象将被删除,以避免不必要的引用。
您还可以将对象的数据写入临时字符串和int,并将它们传递给活动。当然,这样可以得到传输的数据,但不能得到对象本身。
但是,如果您只想显示它们,而不在其他方法或类似方法中使用对象,那么就足够了。我以同样的方式在另一个活动中显示一个对象的数据。
String fName_temp = yourObject.getFname();
String lName_temp = yourObject.getLname();
String age_temp = yourObject.getAge();
String address_temp = yourObject.getAddress();
Intent i = new Intent(this, ToClass.class);
i.putExtra("fname", fName_temp);
i.putExtra("lname", lName_temp);
i.putExtra("age", age_temp);
i.putExtra("address", address_temp);
startActivity(i);
你也可以直接传递它们,而不是传递临时变量,但在我看来,这样更清楚。此外,您可以将temp ivars设置为null,以便GarbageCollector更快地清理它们。
祝你好运
附带说明:重写toString()而不是编写自己的打印方法。
正如下面的评论中提到的,这是您在另一个活动中获取数据的方式:
String fName = getIntent().getExtras().getInt("fname");