帮助传递ArrayList和parcelable活动 [英] Help with passing ArrayList and parcelable Activity
问题描述
所以,我一直在Google上搜寻最昨天和过去有限的,只是似乎无法总结我的头,围绕如何将一个ArrayList传递给一个子活动。有吨例子和片段通过基本的数据类型,但我已经是一个类型的ArrayList的地址的(以下address.java)。
我发现很多东西在计算器并以此为各地的网络,但得到了很多的关注,除了一用一的GeoPoint的例子罢了。再次,它看起来对我来说,他们只是变平的GeoPoint对象为两个整数并通过它,我不能这样做,因为我的地址类可扩展到包括整数,浮点数,等等。眼下,测试应用程序下面是只有两个简单的字符串。我想,如果我能得到parcelalbe的东西与工作,其余的可以遵循。
能否有人张贴工作示例的非基本对象的ArrayList,或者下面添加code,使这项工作?
更新:低于code现在是后回复/编辑工作。谢谢!
/ * helloParcel.java * /
公共类helloParcel扩展活动
{
//保存类型为地址的对象==名称和状态
私人的ArrayList<地址> myList中;
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
按钮B1 =(按钮)findViewById(R.id.button1);
b1.setOnClickListener(OCL);
myList上=新的ArrayList();
解决坦诚=新地址(坦诚,佛罗里达);
地址玛丽=新地址(玛丽,兰州);
地址蒙蒂=新地址(壮士,蒙大拿);
myList.add(坦诚);
myList.add(玛丽);
myList.add(蒙蒂);
//添加myList中的ArrayList()的群众演员的意图
}
OnClickListener OCL =新OnClickListener()
{
@覆盖
公共无效的onClick(视图v)
{
//填写parceable和发射活动
意向意图=新的意图()setClass(getBaseContext(),subActivity.class)。
//出于某种原因,我记得有一个帖子说这是最好创建一个新的
//对象传递。我不知道为什么..
ArrayList的<地址> addyExtras =新的ArrayList<地址>();
的for(int i = 0; I< myList.size();我++)
addyExtras.add(myList.get(ⅰ));
intent.putParcelableArrayListExtra(myList上,addyExtras);
startActivity(意向);
}
};
}
/ * address.java * /
公共类地址实现Parcelable
{
私人字符串名称;
私人字符串状态;
私有静态字符串变量=**地址**;
公共广播(字符串N,String s)将
{
NAME = N;
状态= S;
Log.d(TAG,新地址);
}
公共广播(包裹中)
{
Log.d(TAG,包裹在);
名称= in.readString();
状态= in.readString();
}
公共字符串的getState()
{
Log.d(TAG的getState());
返回(状态);
}
公共字符串的getName()
{
Log.d(TAG的getName());
返回(名称);
}
公共静态最终Parcelable.Creator<地址> CREATOR
=新Parcelable.Creator<地址>()
{
公共广播createFromParcel(包裹中)
{
Log.d(TAG,createFromParcel());
返回新的地址();
}
公共广播[] newArray(INT尺寸)
{
Log.d(TAG,createFromParcel()newArray);
返回新地址【尺寸】;
}
};
@覆盖
公众诠释describeContents()
{
Log.d(TAG,形容());
返回0;
}
@覆盖
公共无效writeToParcel(包裹DEST,INT标志)
{
Log.d(TAG,writeToParcel);
dest.writeString(名称);
dest.writeString(州);
}
}
/ * subActivity.java * /
公共类子活动延伸活动
{
私人最终字符串变量=**子活动**;
私人的ArrayList<地址> myList中;
@覆盖
保护无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d(TAG的onCreate()的子活动);
的setContentView(R.layout.subactivity);
TextView的TV1 =(TextView中)findViewById(R.id.tv_sub);
myList上= getIntent()getParcelableArrayListExtra(mylist中)。
Log.d(TAG,得到了myList上);
的for(int i = 0; I< myList.size();我++)
{
解决= myList.get(ⅰ);
Log.d(TAG,状态:+ a.getState());
tv1.setText(a.getName()+是从+ a.getState());
}
}
}
我可以看到一些问题在这里:
-
为什么要用addressParcelable?为什么不把地址进行Parcelable,然后使用:
intent.putParcelableArrayListExtra(地址,addyExtras);
-
您parcelable对象必须包括一个静态的创造者。详细信息请参见文档。
-
您实际上并没有增加任何额外的意图在打电话之前
startActivity()
。看点1在这里的建议。
我认为你将需要解决所有这些问题,以得到它的工作。
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).
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.
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:
Why use addressParcelable? Why not make address implement Parcelable, and then use:
intent.putParcelableArrayListExtra( "addresses", addyExtras );
Your parcelable object must include a static CREATOR. See the documentation for details.
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活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!