因此,我一直在搜寻昨天和最后一个夜晚的大部分时间,似乎似乎无法绕过如何将arraylist传递给子 Activity 的想法。有大量传递基本数据类型的示例和摘要,但是我所拥有的是地址类型的数组列表(下面的address.java)。

我在stackoverflow和网络上发现了很多东西,但是除了带有GeoPoint示例的东西之外,没有其他东西引起很多关注。再一次,在我看来,他们只是将GeoPoint对象展平为两个整数并将其传递。我无法这样做,因为我的地址类可能会扩展为包括整数,浮点数或其他任何内容。现在,为了简单起见,下面的测试应用程序只有两个字符串。我以为,如果我可以让parcelalbe的东西可以工作的话,剩下的事就可以了。

有人可以为非原始对象的ArrayList发布工作示例,还是可以在下面添加代码以使工作正常?

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

/* 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 ());
       }

    }

}

最佳答案

我可以在这里看到许多问题:

  • 为什么使用addressParcelable?为什么不使地址实现Parcelable,然后使用:
    intent.putParcelableArrayListExtra( "addresses", addyExtras );
    
  • 您的可包裹对象必须包含一个静态CREATOR。有关详细信息,请参见documentation
  • 在调用startActivity()之前,您实际上并未在意图中添加任何其他功能。有关建议,请参见第1点。

  • 我认为您需要解决所有这些问题才能使其正常运行。

    关于android - 帮助传递ArrayList和parcelable Activity,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5819238/

    10-12 01:42
    查看更多