我正在尝试实现parceable将数据传递到下一个屏幕,所以我的家庭活动我正在调用一堆方法(oncreate等)现在我试图从oncreate中创建的查询中获取并传递它在onclick上进入下一个屏幕,但是在onclick之前我想通过告诉parceable来处理它来添加信息...但是我不知道如何将onclick中的信息传递给实现的新类Parceable ......
这是我的代码尝试:(不知道我传递给额外的东西,android网站上缺少文档)
@Override
public void onClick(View v) {
if (v == add_new_dictionary) {
Log.e("clicked add button", "adding the dictionary... ");
// no return true because it is void on intents
Intent add_new_dictionary_intent = new Intent(MainActivity.this,
add_new_dictionary.class);
startActivity(add_new_dictionary_intent);
} else {
// used to get tag for dictionary
Object tag = v.getTag();
//call parceable
SendIdOfDictionary(tag);
// start new intent based on the tag -
Intent new_dictionary_view = new Intent(MainActivity.this,
dictionary_view.class);
new_dictionary_view.putExtra("d_id", WhatAmISupposeToPassInHere);
startActivity(new_dictionary_view);
}
// parceable is faster than serialization - create class to implement it
}
private void SendIdOfDictionary(Object tag) {
class ParceDictionaryData implements Parcelable {
private String tag;
/* everything below here is for implementing Parcelable */
// 99.9% of the time you can just ignore this
public int describeContents() {
return 0;
}
// write your object's data to the passed-in Parcel
public void writeToParcel(Parcel out, int flags) {
out.writeString(tag);
}
// this is used to regenerate your object. All Parcelables must have
// a CREATOR that implements these two methods
public final Parcelable.Creator<ParceDictionaryData> CREATOR = new Parcelable.Creator<ParceDictionaryData>() {
public ParceDictionaryData createFromParcel(Parcel in) {
return new ParceDictionaryData(in);
}
public ParceDictionaryData[] newArray(int size) {
return new ParceDictionaryData[size];
}
};
// example constructor that takes a Parcel and gives you an object
// populated with it's values
private ParceDictionaryData(Parcel in) {
tag = in.readString();
}
}
答案 0 :(得分:1)
使用捆绑包,此处不需要Parceable。
将其添加到启动第二个活动的Intent:
Intent i = new Intent();
i.putExtra("tag", yourTag);
然后点击
getIntent()getExtras()的getString( “标签”);
答案 1 :(得分:0)
传递值
public void onClick(View view) {
//Launching All products Activity
Intent in = new Intent(getApplicationContext(), ViewMap.class);
// sending address to next activity
in.putExtra("address",strAdd);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
并接收值
Intent i = getIntent();
// getting product id (pid) from intent
Bundle extras = getIntent().getExtras();
if (extras != null) {
adress=extras.getString("address");
}