【问题标题】:Passing Arraylist data from one activity to other in OnPostExecute在 OnPostExecute 中将 Arraylist 数据从一个活动传递到另一个活动
【发布时间】:2015-03-21 17:14:07
【问题描述】:

我正在尝试使用 OnPostExecute 方法中的 Bundles 将 arraylist 从一个活动传递给另一个活动,但我无法这样做。我也没有得到正确的错误来进行类型转换或做一些事情来删除错误。我不确定是什么这里错了。

   ***here reminderList is List<GetReminder> reminderList;***

private class AsyncCallWS extends AsyncTask<String, Void, Void> {
            @Override
            protected Void doInBackground(String... params) {
                //Invoke webservice
                vaildUserId=WebService.invokeAuthenticateUserWS(loginUserName, loginPassword, "AuthenticateUser");
                if(vaildUserId>=0){
                    reminderList=WebService.invokeHelloWorldWS("GetReminder");
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                Bundle bundle = new Bundle();
                bundle.putStringArrayList("reminderList", reminderList);
                reminderIntent.putExtras(bundle);
                startActivity(new Intent(getApplicationContext(), ReminderActivity.class));
                startActivity(reminderIntent);
            }

【问题讨论】:

  • 能贴一下log cat错误输出吗?
  • makeGetReminder class as Parceble or Serializable 然后使用 `putSerializable()` 方法传递,因为您的 reminderList 不是字符串数组或列表。
  • 它显示不能将 StringArrayList 应用到 bundle,我也尝试过使用其他选项,但失败了
  • @user370305 你应该写这个作为答案

标签: android


【解决方案1】:

要解决您的问题,您可以使用Intent 类中定义的putParcelableArrayListExtra()getParcelableArrayListExtra() 方法。

1.确保您的GetReminder 类实现Parcelable

HereParcelable的文档,也包含了Parcelable的典型实现。

Here 是一个网站,可以帮助您自动生成一个类的 Parcelable 实现。

2.在您的onPostExecute() 方法中添加如下内容:

//Remember to declare reminderList as ArrayList here.
ArrayList<GetReminder> reminderList = ...;

Intent intent = new Intent(getApplicationContext(), ReminderActivity.class);
intent.putParcelableArrayListExtra("reminderList", reminderList);
startActivity(intent);

然后在您的 ReminderActivity 类中获取 ArrayList ,如下所示:

ArrayList<GetReminder> list = getIntent().getParcelableArrayListExtra("reminderList");

顺便说一句,还有另一种方法可以解决你的问题,你可以参考我的回答here

【讨论】:

  • 获取 putParcelableArrayListExtra 不能应用于意图,一个问题只是在 GetReminder 中实现 parcealbe 就足够了吗?
  • @jenil 确保您传递给 putParcelableArrayListExtra() 的提醒列表类型声明为 ArrayList。是的,只需在 GetReminder 中实现 parcealbe 就足够了。我将更改代码以说明如何解决您刚刚遇到的问题。
  • java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.sampleapp/com.example.sampleapp.ReminderActivity}: android.os.BadParcelableException: Parcelable 协议需要 Parcelable.Creator 对象调用com.example.sampleapp.GetReminder 类上的 CREATOR
  • 我改变了你说现在我得到了这个
  • 终于工作了,我关注了你使用可序列化发布stackoverflow.com/questions/28733439/…
【解决方案2】:

问题是您的List&lt;GetReminder&gt; reminderList; 不是字符串列表。

要通过自定义对象列表,您必须将自定义类设为SerializableParcelable。在您的情况下,将GetReminder 设为SerializableParcelable

然后使用putExtra()putSerializable()的Intent传递Serializable对象。

还有一些我从您的onPostExecute() 中记下的代码,正如您所写的那样

startActivity(new Intent(getApplicationContext(), ReminderActivity.class));
startActivity(reminderIntent);

将导致创建两个活动实例。

所以删除第一个,

startActivity(new Intent(getApplicationContext(), ReminderActivity.class));

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-11
  • 2021-03-15
  • 1970-01-01
  • 2015-12-18
  • 2012-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多