我正在制作一个Android应用程序,让我想起它的某个人的生日。
我现在可以添加名称和相应的日期。 但是,我目前所处的位置是储蓄部分。我已经尝试过查找某些解决方案但无法将其移植到我的需求中。因为它是一个ArrayList而不是一个简单的String,所以它不能被序列化(如果我正确吗?),因此不能保存。那么还有其他方法来保存这种数据吗?
这是我的代码:
Item.java:
public class Item implements Serializable{
private String name;
private String date;
public Item(String name, String date){
this.name = name;
this.date = date;
}
public String getName(){
return name;
}
public String getDate(){
return date;
}
public void setName(String name){
this.name = name;
}
public void setDate(String date){
this.date = date;
}
}
MainActivity.java:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
static RecyclerviewAdapter adapter;
static ArrayList<Item> itemList = new ArrayList<>();
private static String nameText;
private static String birthString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new RecyclerviewAdapter(itemList);
recyclerView.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AddBirthday();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
month++;
birthString = day + "-" + month + "-" + year;
itemList.add(new Item(nameText, birthString));
adapter.notifyDataSetChanged();
saveArrayList(getContext(), itemList);
}
}
private void AddBirthday() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Name");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
nameText = input.getText().toString();
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
public static void saveArrayList(Context c, ArrayList<Item> arrayList) {
}
}
答案 0 :(得分:0)
在Java中序列化或反序列化ArrayLists没有问题:
List<Item> list = new ArrayList<>();
list.add(new Item("A", "2006"));
list.add(new Item("B", "2016"));
// Serialize
FileOutputStream fos = context.openFileOutput("list.ser", Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(list);
os.close();
fos.close();
// Deserialize
FileInputStream fileIn = context.openFileInput("list.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
list = (List<Item>) in.readObject();
in.close();
fileIn.close();
// Implement toString() in Item
System.out.println(list);