翔如菲菲

其实天很蓝,阴云总会散;其实海不宽,此岸连彼岸.

导航

Android开发问题汇总

1.Benefit of using Parcelable instead of serializing object

As I understand, Bundle and Parcelable belongs to the way Android performs serialization in. It is used for example in passing data between activities. But I wonder, if there are any benefits in usingParcelable instead of classic serialization in case of saving state of my business objects to the internal memory for example? Will it be simpler or faster than the classic way? Where should I use classic serialization and where better to use bundles?

the answer from 'Pro Android2' as follows:

NOTE: Seeing Parcelable might have triggered the question, why is Android not using the built-in Java serialization mechanism? It turns out that the Android team came to the conclusion that the serialization in Java is far too slow to satisfy Android’s interprocess-communication requirements. So the team built the Parcelable solution. The Parcelable approach requires that you explicitly serialize the members of your class, but in the end, you get a much faster serialization of your objects.

Also realize that Android provides two mechanisms that allow you to pass data to another process. The first is to pass a bundle to an activity using an intent, and the second is to pass a Parcelable to a service. These two mechanisms are not interchangeable and should not be confused. That is, the Parcelable is not meant to be passed to an activity. If you want to start an activity and pass it some data, use a bundle. Parcelable is meant to be used only as part of an AIDL definition. 

this artile reprint from http://stackoverflow.com/questions/5550670/benefit-of-using-parcelable-instead-of-serializing-object

2.报错column ‘_id’ does not exist的解决

 用Sqlite存储数据,查询数据并bind到SimpleCursorAdapter时报错:java.lang.IllegalArgumentException: column '_id' does not exist

相关代码如下:
  SqliteDBHelper helper = new SqliteDBHelper(getApplicationContext());
  String[] from = { "_id","name", "cellPhone", "memo" };
  Cursor cursor = helper.Query(from);
  int[] to = { R.id.text0, R.id.text1, R.id.text2, R.id.text3 };
  SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.row, cursor, from, to);
  setListAdapter(adapter);
解决办法:
使用Cursor相关的Adapter(这里是SimpleCursorAdapter )时需要一个自增的列,且名字必需为 _id。
1)创建数据表时插入一个名为_id的列,类型为自增量,因为在使用Cursor相关的Adapter时需要用到这个列
2)如果实在不需要这个列的话,可以把数据表中某一列使用AS映射为 _id,再进行操作

posted on 2012-10-16 14:08  翔如飞飞  阅读(185)  评论(0)    收藏  举报