누군가의 생일이 언제인지 알려주는 Android 앱을 만들고 있습니다.java 클래스의 ArrayList 파일 만들기
이제 이름과 해당 날짜를 추가 할 수있게되었습니다. 하지만 현재 내가 갇혀있는 곳은 저축 부분입니다. 이미 몇 가지 해결책을 찾아 보았지만 내 필요에 이식 할 수 없었습니다. ArrayList이기 때문에 간단한 문자열이 아니기 때문에 직렬화 할 수 없습니다 (맞으면?) 따라서 저장되지 않습니다. 이런 종류의 데이터를 저장하는 다른 방법이 있습니까?
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 :
자바 ArrayLists 직렬화 또는 직렬화 복원 아무 문제가 없습니다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) {
}
}
저장 내용을 표시하십시오. 당신이 찾은 옵션과 그들이 당신의 필요에 부적합한 방법. 그렇지 않으면 "너무 광범위하게"들립니다. – YakovL
'ArrayList'는 직렬화 가능 클래스입니다. – RamenChef
공유 환경 설정을 사용하여 작업을 시도했지만 효과적이지는 않을 것이라고 읽었습니다. 그럼 File 출력을 사용해 보았지만 ArrayList- 과 함께 사용하는 방법을 실제로 얻지는 못했습니다. 충분한 정보를 제공 했습니까? 이 작업을 수행하는 방법을 정말 알고 싶습니다. –