[英]Pass ArrayList<? implements Parcelable> to Activity
I have searched a few topics but not found a solution to my problem.我搜索了一些主题,但没有找到解决我的问题的方法。
public class Series implements Parcelable {
private String name;
private int numOfSeason;
private int numOfEpisode;
/** Constructors and Getters/Setters have been removed to make reading easier **/
public Series(Parcel in) {
String[] data = new String[3];
in.readStringArray(data);
this.name = data[0];
this.numOfSeason = Integer.parseInt(data[1]);
this.numOfEpisode = Integer.parseInt(data[2]);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] { this.name,
String.valueOf(this.numOfSeason),
String.valueOf(this.numOfEpisode) });
}
private void readFromParcel(Parcel in) {
name = in.readString();
numOfSeason = in.readInt();
numOfEpisode = in.readInt();
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
@Override
public Series createFromParcel(Parcel in) {
return new Series(in);
}
@Override
public Series[] newArray(int size) {
return new Series[size];
}
};
} }
In my MainActivity I have an ArrayList.在我的 MainActivity 中,我有一个 ArrayList。 To make the list dynamically editeable I need to pass it to another activity where I can edit it.
为了使列表可动态编辑,我需要将它传递给另一个我可以编辑它的活动。
ArrayList<Series> listOfSeries = new ArrayList<Series>();
public void openAddActivity() {
Intent intent = new Intent(this, AddActivity.class);
intent.putParcelableArrayListExtra(
"com.example.episodetracker.listofseries",
(ArrayList<? extends Parcelable>) listOfSeries);
startActivity(intent);
}
I need to cast the list, otherwise Eclipse gives me the following Error message.我需要转换列表,否则 Eclipse 会给我以下错误消息。 The method putParcelableArrayListExtra(String, ArrayList) in the type Intent is not applicable for the arguments (String, List)
Intent 类型中的 putParcelableArrayListExtra(String, ArrayList) 方法不适用于参数 (String, List)
Is this the correct way to do it?这是正确的方法吗?
ArrayList<Series> list = savedInstanceState
.getParcelableArrayList("com.example.episodetracker.listofseries");
This is the way I try to read the data in another activity.这是我尝试在另一个活动中读取数据的方式。
It's crashing on the line above.它在上面的线上崩溃。 namely the getParcelableArrayList part.
即 getParcelableArrayList 部分。
The problem is in writing out to the parcel and reading in from the parcel ...问题在于写出包裹并从包裹中读取......
@Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(numOfSeason); dest.writeInt(numOfEpisode); } private void readFromParcel(Parcel in) { name = in.readString(); numOfSeason = in.readInt(); numOfEpisode = in.readInt(); }
What you write out has to match what you read in...你写出来的东西必须和你读到的东西相匹配……
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent i = new Intent(this,SecondActivity.class); ArrayList<testparcel> testing = new ArrayList<testparcel>(); i.putParcelableArrayListExtra("extraextra", testing); startActivity(i); } /**********************************************/ public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayList<testparcel> testing = this.getIntent().getParcelableArrayListExtra("extraextra"); } }
The above code is having onCreate() from two different activities.上面的代码有来自两个不同活动的 onCreate()。 The first one launches the second one;
第一个启动第二个; and it works fine I was able to pull the parcelable without issue.
并且它工作正常我能够毫无问题地拉出parcelable。
您应该在Intent
类上使用putParcelableArrayListExtra()
方法。
I've used putParcelableArrayList(<? extends Parcelable>)
from a Bundle
Object.我已经使用了
Bundle
对象中的putParcelableArrayList(<? extends Parcelable>)
。 Not directly from an Intent Object.(I don't really know what's the difference).不是直接来自意图对象。(我真的不知道有什么区别)。 but i use to use in this way:
但我习惯以这种方式使用:
ArrayList<ParcelableRow> resultSet = new ArrayList<ParcelableRow>();
resultSet = loadData();
Bundle data = new Bundle();
data.putParcelableArrayList("search.resultSet", resultSet);
yourIntent.putExtra("result.content", data);
startActivity(yourIntent);
Later on your new activity you can populate the data recently inserted on the Bundle object like this:稍后在您的新活动中,您可以填充最近插入 Bundle 对象的数据,如下所示:
Bundle data = this.getIntent().getBundleExtra("result.content");
ArrayList<ParcelableRow> result = data.getParcelableArrayList("search.resultset");
Just remember that your ArrayList<>
must contain only parcelable objects.请记住,您的
ArrayList<>
必须仅包含可分割的对象。 and just to make sure that your have passed the data you may check if the data received is null or not, just to avoid issues.并且只是为了确保您已经通过了数据,您可以检查收到的数据是否为空,以避免出现问题。
也许这对某人有帮助……否则我的问题是我使用了 write 和 readValue 但它应该匹配 writeInt、readInt writeString、readString 等类型
I am doing it in this way:我是这样做的:
var intent = Intent(this@McqActivity,ResultActivity::class.java)
intent.putParcelableArrayListExtra("keyResults", ArrayList(resultList))
// resultList is of type mutableListOf<ResultBO>()
startActivity(intent)
And for making the class Parcelable .并用于制作Parcelable 类。 I simply do
two operation
first I used @Parcelize Annotation
above my data class and secondly I inherit it with Parcelable
like below..我简单地做
two operation
起初我用@Parcelize Annotation
我上面的数据类,其次我继承它Parcelable
像下面..
import kotlinx.android.parcel.Parcelize
import android.os.Parcelable
@Parcelize // Include Annotion
data class ResultBO(val questionBO: QuestionBO) : Parcelable {
constructor() : this(QuestionBO())
}
And at receiving end并在接收端
if (intent != null) {
var results = intent.getParcelableArrayListExtra<Parcelable>("keyResults")
}
You can pass Parcelable ArrayList您可以通过 Parcelable ArrayList
Sender Activity ->发件人活动 ->
startActivity(Intent(this, SenderActivity::class.java).apply { putExtra("getList",list)})
Receiver Activity ->接收器活动 ->
private lateinit var list: ArrayList<List>
list = this.intent.extras?.getParcelableArrayList("getList")!!
And you will get all Arraylist in list.您将获得列表中的所有 Arraylist。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.