帮助传递 ArrayList 和 Parcelable Activity [英] Help with passing ArrayList and parcelable Activity

查看:19
本文介绍了帮助传递 ArrayList 和 Parcelable Activity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我昨天和昨晚的大部分时间都在用谷歌搜索,但似乎无法理解如何将数组列表传递给子活动.有大量传递原始数据类型的示例和片段,但我拥有的是一个address类型的数组列表(下面的address.java).

So I've been googling most of yesterday and last nite and just can't seem to wrap my head around how to pass an arraylist to a subactivity. There are tons of examples and snippets passing primitive data types, but what I have is an arraylist of type address (address.java below).

我在 stackoverflow 和网络上找到了很多关于此的内容,但除了一个 GeoPoint 示例之外,没有什么引起了很多关注.同样,在我看来,他们只是将 GeoPoint 对象展平为两个整数并将其传入.我不能这样做,因为我的地址类可能会扩展为包括整数、浮点数等.现在,为了简单起见,下面的测试应用程序只有两个字符串.我想如果我能让 Parcelalbe 的东西与它一起工作,剩下的就可以了.

I've found a lot of stuff on stackoverflow and around the web on this, but nothing that got a lot of attention except for one with a GeoPoint example. Again, it looked to me like they just flattened the GeoPoint object into two integers and passed it in. I can't do that because my address class may expand to include integers, floats, whatever. Right now, the test app below is only two strings for simplicity. I thought if I could get the parcelalbe stuff working with that, the rest could follow.

有人可以为非原始对象的 ArrayList 发布一个工作示例,或者在下面添加代码以使其工作吗?

Can someone post a working example for an ArrayList of a non-primitive object, or perhaps add code below to make this work?

更新:回复/编辑后,下面的代码现在可以工作了.谢谢!

UPDATE: code below is now working after replies/editing. Thanks!

/* helloParcel.java */        
       public class helloParcel extends Activity
{
    // holds objects of type 'address' == name and state
    private ArrayList <address> myList;

    @Override
    public void onCreate (Bundle savedInstanceState)
    {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.main);

        Button b1 = (Button) findViewById(R.id.button1);
        b1.setOnClickListener(ocl);

        myList = new ArrayList();
        address frank   = new address ("frank", "florida");
        address mary    = new address ("mary", "maryland");
        address monty   = new address ("monty", "montana");

        myList.add (frank);
        myList.add (mary);
        myList.add (monty);

        // add the myList ArrayList() the the extras for the intent

    }

    OnClickListener ocl = new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            // fill parceable and launch activity
            Intent intent = new Intent().setClass(getBaseContext (), subActivity.class);

            // for some reason, I remember a posting saying it's best to create a new
            // object to pass.  I have no idea why..
            ArrayList <address> addyExtras = new ArrayList <address>();

            for (int i = 0; i < myList.size(); i++)
                addyExtras.add (myList.get(i));

            intent.putParcelableArrayListExtra ("mylist", addyExtras);
            startActivity(intent);
        }
    };
} 



/* address.java */
  public class address implements Parcelable
{
    private String name;
    private String state;
    private static String TAG = "** address **";

    public address (String n, String s)
    {
        name = n;
        state = s;
        Log.d (TAG, "new address");
    }

    public address (Parcel in)
   {
    Log.d (TAG, "parcel in");
        name = in.readString ();
        state = in.readString ();
   }

    public String getState ()
    {
        Log.d (TAG, "getState()");
        return (state);
    }

    public String getName ()
    {
        Log.d (TAG, "getName()");
        return (name);
    }

    public static final Parcelable.Creator<address> CREATOR
    = new Parcelable.Creator<address>() 
   {
         public address createFromParcel(Parcel in) 
         {
            Log.d (TAG, "createFromParcel()");
             return new address(in);
         }

         public address[] newArray (int size) 
         {
            Log.d (TAG, "createFromParcel() newArray ");
             return new address[size];
         }
    };

    @Override
   public int describeContents ()
   {
        Log.d (TAG, "describe()");
       return 0;
   }

    @Override
   public void writeToParcel (Parcel dest, int flags)
   {
        Log.d (TAG, "writeToParcel");
       dest.writeString (name);
       dest.writeString (state);
   }

}


/* subActivity.java */
  public class subActivity extends Activity
{
    private final String TAG = "** subActivity **";
    private ArrayList <address> myList;

    @Override
    protected void onCreate (Bundle savedInstanceState)
    {
       super.onCreate (savedInstanceState);
       Log.d (TAG, "onCreate() in subActivity");

       setContentView(R.layout.subactivity);
       TextView tv1 = (TextView) findViewById(R.id.tv_sub);

       myList = getIntent().getParcelableArrayListExtra ("mylist");
       Log.d (TAG, "got myList");

       for (int i = 0; i < myList.size (); i++)
       {
        address a = myList.get (i);
        Log.d (TAG, "state:" + a.getState ());
        tv1.setText (a.getName () + " is from " + a.getState ());
       }

    }

}

推荐答案

我可以在这里看到一些问题:

I can see a number of problems here:

  1. 为什么要使用addressParcelable?为什么不让地址实现Parcelable,然后使用:

  1. Why use addressParcelable? Why not make address implement Parcelable, and then use:

intent.putParcelableArrayListExtra( "addresses", addyExtras );

  • 您的 Parcelable 对象必须包含一个静态 CREATOR.有关详细信息,请参阅文档.

    在调用 startActivity() 之前,您实际上并未向意图添加任何额外内容.有关建议,请参阅此处的第 1 点.

    You are not actually adding any extras to the intent before you call startActivity(). See point 1 for a suggestion here.

    我认为您需要解决所有这些问题才能使其正常工作.

    I think that you will need to address all of these issues in order to get it working.

    这篇关于帮助传递 ArrayList 和 Parcelable Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆