如何在android?</colorsaver>中使我的ArrayList <colorsaver>持久化

时间:2013-10-03 17:46:22

标签: android arraylist

我有ArrayList ...

 ArrayList<ColorSaver> tempList = new ArrayList<ColorSaver>();

我希望它能够在用户关闭应用或离开应用时,当用户重新打开应用时,ColorSaver中的所有ArrayList对象都会存在。我更喜欢使用SharedPreferences,但我不能这样做因为列表是自定义对象... 我环顾四周,发现我可以做一个序列化,但我尝试了这个并且失败了,所以如果有人可以引导我通过可序列化的交易,这将是伟大的。哦,我在哪里放置代码,例如我onCreate()或显示mainActivity的{​​{1}}中的activity

我的mainActivity类

ArrayList

我的 public class MainActivity extends Activity { ArrayList<ColorSaver> tempList = new ArrayList<ColorSaver>(); private static final String TAG = "Main Activity"; public static final String PREFS_NAME = "MyPrefsFile"; final Intent intent = new Intent(); @Override public boolean onOptionsItemSelected(MenuItem item) { final NumberPicker rednp = (NumberPicker) findViewById(R.id.redNumberPicker1); final NumberPicker bluenp = (NumberPicker) findViewById(R.id.blueNumberPicker); final NumberPicker greennp = (NumberPicker) findViewById(R.id.greenNumberPicker); switch(item.getItemId()) { case R.id.save: Log.i(TAG, "Save item clicked!"); Intent intent = new Intent(this, SaveActivity.class); intent.putExtra("RedValue", rednp.getValue()); intent.putExtra("BlueValue", bluenp.getValue()); intent.putExtra("GreenValue", greennp.getValue()); intent.putExtra("temparray", tempList); startActivity(intent); return true; case R.id.recall: Log.i(TAG, "Recall item clicked!"); Intent intent2 = new Intent(this, RecallActivity.class); intent2.putExtra("temparray", tempList); startActivity(intent2); return true; default: return super.onOptionsItemSelected(item); }//End Switch } @SuppressWarnings("unchecked") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //ArrayList<ColorSaver> tempList = new ArrayList<ColorSaver>(); Bundle extras = getIntent().getExtras(); final SurfaceView sView = (SurfaceView) findViewById(R.id.surfaceView1); final NumberPicker np = (NumberPicker) findViewById(R.id.redNumberPicker1); np.setMaxValue(255); np.setMinValue(0); final NumberPicker np2 = (NumberPicker) findViewById(R.id.greenNumberPicker); np2.setMaxValue(255); np2.setMinValue(0); final NumberPicker np3 = (NumberPicker) findViewById(R.id.blueNumberPicker); np3.setMaxValue(255); np3.setMinValue(0); if( extras != null ) { np.setValue(extras.getInt("savedRValue")); //np.setValue(intent.getIntExtra("savedRValue", 255)); np2.setValue(extras.getInt("savedGValue")); //np2.setValue(intent.getIntExtra("savedGValue", 255)); np3.setValue(extras.getInt("savedBValue")); //np3.setValue(intent.getIntExtra("savedBValue", 255)); tempList = (ArrayList<ColorSaver>) extras.getSerializable("array"); sView.setBackgroundColor(Color.argb(255, np.getValue(), np2.getValue(), np3.getValue())); } else { Log.i(TAG, "I just don't get it...WTF"); } np.setOnValueChangedListener( new NumberPicker. OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { int rednum, greennum, bluenum; rednum = np.getValue(); greennum = np2.getValue(); bluenum = np3.getValue(); sView.setBackgroundColor(Color.argb(255, rednum, greennum, bluenum)); } }); //GREEN NUMBERPICKER LISTENER np2.setOnValueChangedListener( new NumberPicker. OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { int rednum, greennum, bluenum; rednum = np.getValue(); greennum = np2.getValue(); bluenum = np3.getValue(); sView.setBackgroundColor(Color.argb(255, rednum, greennum, bluenum)); } }); np3.setOnValueChangedListener( new NumberPicker. OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { int rednum, greennum, bluenum; rednum = np.getValue(); greennum = np2.getValue(); bluenum = np3.getValue(); sView.setBackgroundColor(Color.argb(255, rednum, greennum, bluenum)); } }); }//End onCreate() @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }//END onCreateOptionsMenu() }//END CLASS ,用户将其颜色组合保存到saveActivity ...

ArrayList

我的 public class SaveActivity extends Activity implements Serializable { private static final String TAG = "Save Activity"; public ArrayList<ColorSaver> savedColors = new ArrayList<ColorSaver>(); @SuppressWarnings("unchecked") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_save); // Show the Up button in the action bar. setupActionBar(); Bundle extras = getIntent().getExtras(); final Intent intent1 = new Intent(this, MainActivity.class); Button saveButton = (Button) findViewById(R.id.saveButton1); final EditText nameField = (EditText) findViewById(R.id.colorNameField); //final Intent intent = new Intent(); savedColors = (ArrayList<ColorSaver>) extras.getSerializable("temparray"); //Making sure the savedColors arrayList has something in it. if( savedColors.isEmpty() ) { ColorSaver temp = new ColorSaver("Rockies Purple", 180, 80, 255); savedColors.add(temp); } saveButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click Bundle extras = getIntent().getExtras(); int redcolor, greencolor, bluecolor; redcolor = extras.getInt("RedValue"); greencolor = extras.getInt("GreenValue"); bluecolor = extras.getInt("BlueValue"); String colorName = nameField.getText().toString(); //Build the new color and add it to the arrayList ColorSaver saver = new ColorSaver(colorName, redcolor, greencolor, bluecolor); savedColors.add(saver); intent1.putExtra("array", savedColors); Log.i(TAG, savedColors.get(savedColors.size()-1).getColorName()); startActivity(intent1); } }); }//END OnCreate() /** * Set up the {@link android.app.ActionBar}. */ private void setupActionBar() { getActionBar().setDisplayHomeAsUpEnabled(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.save, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // This ID represents the Home or Up button. In the case of this // activity, the Up button is shown. Use NavUtils to allow users // to navigate up one level in the application structure. For // more details, see the Navigation pattern on Android Design: // // http://developer.android.com/design/patterns/navigation.html#up-vs-back // NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } }//END CLASS 用户回想起他们的颜色组合......

recallActivity

我很满意任何帮助!

1 个答案:

答案 0 :(得分:1)

了解Android的Parcelable实施。

所以,我只是猜测你的ColorSaver类,因为它没有发布,但是你可以通过以下方式实现它:

ColorSaver.java

public class ColorSaver implements Parcelable {
     private String mName;
     private int    mRed;
     private int    mGreen;
     private int    mBlue;

     public int describeContents() {
         return 0;
     }

     public void writeToParcel(Parcel out, int flags) {
         out.writeString(mName);
         out.writeInt(mRed);
         out.writeInt(mGreen);
         out.writeInt(mBlue);
     }

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

         public ColorSaver[] newArray(int size) {
             return new ColorSaver[size];
         }
     };

     private ColorSaver(Parcel in) {
         mName  = in.readString();
         mRed   = in.readInt();
         mGreen = in.readInt();
         mBlue  = in.readInt();
     }
 }

MyActivity.java

public class MyActivity extends Activity {
    private static final String COLOR_SAVER_LIST = "com.example.android.ColorSaverList";
    private List<ColorSaver> colorSaverList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null && savedInstanceState.containsKey(COLOR_SAVER_LIST)) {
            colorSaverList = new ArrayList<ColorSaver>();
            colorSaverList = savedInstanceState.getParcelableArrayList(COLOR_SAVER_LIST);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList(COLOR_SAVER_LIST, colorSaverList);
    }
}