2014-07-27 33 views
0

所以我想我會嘗試一下在今天的Android瓜分,趕緊嘲笑這件事:的Android Parcelable理解 - 瓜分不工作

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,並嘗試使用實現Parcelable接口的對象,以包裹的ArrayList當按鈕。它試圖把它傳遞給類是一個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"));抱怨說,而不是獲取單元對象,它得到Parcelable對象,因爲類Unit實現它們。

這是否意味着在我的適配器中,我必須更改列表以期望Parcelable對象,如果它意味着它將改變我如何閱讀它們?

我也不明白writeToParcel,該文件說,它會扁平化的對象,所以我認爲,因爲我想扁平化列表,我將需要writeList方法,但之後呢?它似乎是扁平單位對象而不是列表。

這也導致我問構造:

public Unit(Parcel in) { 
     // 
} 

我猜測,我需要做這樣的事情:

readTypedList() - 我不明白爲什麼我必須這樣做因爲我會創建包裹。所以我不知道如何去實現這個東西......我真的很想知道parcelling是如何工作的,我沒有找到有用的文檔,以及在這個例子中它如何成功地工作。非常感謝幫助和建議。

回答

0

我想你需要明確地告訴Parcelable造物主需要創建什麼類型的,所以請嘗試更改您的代碼如下:

public static final Parcelable.Creator<Unit> CREATOR = new Parcelable.Creator<Unit>() { 
    public Unit createFromParcel(Parcel in) { 
     return new Unit(in); 
    } 

    public Unit[] newArray(int size) { 
     return new Unit[size]; 
    } 
}; 
+0

很抱歉,但它不工作,UnitListAdapter mTypeAdapter = new UnitListAdapter(getApplicationContext(),bundle.getParcelableArrayList(「units」));不斷要求我將bundle.getParcelableArrayList轉換爲ArrayList 但我的Unit類實現它,所以我不知道它爲什麼在抱怨,不能擴展接口所以... – user2405469

0
public static final Parcelable.Creator<Unit> CREATOR = new Parcelable.Creator<Unit>() { 
    public Unit createFromParcel(Parcel in) { 
     Unit u = new Unit(); 
     u.setUnitName(in.readString()); 
     //... 
     //populate all atributes 
     return u; 
    } 

    public Unit[] newArray(int size) { 
     return new Unit[size]; 
    } 
}; 
+0

您能否提供有關您的答案的更多信息?對你發佈的答案進行一些解釋通常是很好的做法。 – Bono