所以我想我今天会在 Android 中尝试 Parcelling,很快就模拟了这个:
public class Unit implements Parcelable {
private String unitName;
private int view;
private Double value;
/**
* Constructor
*/
public Unit() { }
/**
* Constructor
* @param unitName
*/
public Unit(String unitName) {
this.unitName = unitName;
}
/**
* Constructor
* @param unitName
* @param view
*/
public Unit(String unitName, int view, Double value) {
this.unitName = unitName;
this.view = view;
this.value = value;
}
/**
* Set the name of the unit of measurement
* @param name
*/
public void setUnitName(String name) {
this.unitName = name;
}
/**
* Set whether the unit of measurement is viewable
* @param view
*/
public void setView(int view) {
this.view = view;
}
/**
* Set the value of the unit
* @param value
*/
public void setValue(Double value) { this.value = value; }
/**
* Return the unit name
* @return unitName
*/
public String getUnitName() {
return unitName;
}
/**
* Return whether the unit is viewable
* @return view
*/
public int getView() {
return view;
}
/**
* Return the value of this unit
* @return value
*/
public Double getValue() { return value; }
/**
* Methods for parcelling here
*/
/**
* Constructor for parcelling
* @param in
*/
public Unit(Parcel in) {
//
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(new ArrayList<Unit>() {
//huh?
});
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Unit createFromParcel(Parcel in) {
return new Unit(in);
}
public Unit[] newArray(int size) {
return new Unit[size];
}
};
}
您可能会注意到,我从Parcelable
接口覆盖的方法的实现是不完整的。但是继续说,这个类只是一个应该可以被另一个类包裹的对象。
现在这是我的主要活动:
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button send = (Button) findViewById(R.id.sendParcel);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ArrayList<Unit> units = getUnits();
Intent i = new Intent(getApplicationContext(), UnitListActivity.class);
i.putParcelableArrayListExtra("units", units);
startActivity(i);
}
});
}
/**
* Return an arraylist of units
* @return units
*/
public ArrayList<Unit> getUnits() {
ArrayList<Unit> units = new ArrayList<Unit>();
units.add(new Unit("acceleration", 1, 56.45));
units.add(new Unit("kilograms", 0, 5830.54345));
units.add(new Unit("metres", 1, 543.54));
units.add(new Unit("kilometres", 1, 32.43));
units.add(new Unit("grams", 1, 453.654));
units.add(new Unit("kilograms", 0, 8.5));
units.add(new Unit("litres", 1, 344.3));
units.add(new Unit("millilitres", 0, 4543.879));
units.add(new Unit("decimeter", 1, 5084903.564567));
return units;
}
}
基本上有一个按钮在按下时会创建一个Intent
并尝试将一个与实现接口ArrayList
的对象打包在一起。Parcelable
它试图将其传递给的类ListActivity
是此处的代码:
public class UnitListActivity extends ListActivity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
UnitListAdapter mTypeAdapter = new UnitListAdapter(getApplicationContext(), bundle.getParcelableArrayList("units"));
setListAdapter(mTypeAdapter);
}
}
我为这里制作了这个适配器ListActivity
:
public class UnitListAdapter extends ArrayAdapter<Unit> {
private ArrayList<Unit> unitsList;
private Context context;
public UnitListAdapter(Context c, ArrayList<Unit> units) {
super(c, R.layout.unit_values, units);
this.context = c;
this.unitsList = units;
}
@Override
public View getView(int position, View convertView, ViewGroup parentView) {
View view = null;
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflator.inflate(R.layout.unit_values, null);
TextView textView = (TextView) view.findViewById(R.id.unit_name);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.check);
//check box set on check changed listener if I get this far for later
if(unitsList.get(position).getView() == 1) {
textView.setText(unitsList.get(position).getUnitName());
checkBox.setChecked(true);
} else {
textView.setText(unitsList.get(position).getUnitName());
}
return view;
}
}
现在我不明白为什么在ListActivity
这一行:
UnitListAdapter mTypeAdapter = new UnitListAdapter(getApplicationContext(), bundle.getParcelableArrayList("units"));
抱怨它不是获取 Unit 对象,而是获取Parcelable
对象,因为类Unit
实现了它们。
这是否意味着在我的适配器中我必须更改列表以期望 Parcelable 对象,如果,这意味着它将改变我阅读它们的方式?
我也不明白writeToParcel
,文档说它会展平对象,所以我想既然我想展平列表,我将需要该writeList
方法,但那之后呢?似乎它正在展平 Unit 对象而不是列表。
这也导致我询问构造函数:
public Unit(Parcel in) {
//
}
我猜我需要做这样的事情:
readTypedList()
- 我不明白为什么我必须这样做,因为我将创建包裹。所以我不知道如何去实现这个东西......我真的很想了解 parcelling 是如何工作的,我没有找到有用的文档以及在这个例子中它是如何成功工作的。非常感谢帮助和建议。