Во-первых, как поместить последний элемент в списке сверху вверху списка? Может быть приоритет для каждой заметки. Пример номера очереди. И когда я создаю заметку, я хочу выбрать цвет фона заметки для каждой заметки. У каждой заметки свой цвет фона.
Основная деятельность
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mlistViewNotes = findViewById(R.id.main_listview_notes);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_main_new_note:
//start noteaactivity
Intent newNoteActivity =new Intent(this,NoteActivity.class);
startActivity(newNoteActivity);
break;
}
return true;
}
@Override
protected void onResume() {
super.onResume();
mlistViewNotes.setAdapter(null);
ArrayList<Note> notes = Utilities.getAllSavedNotes(this);
if(notes == null || notes.size() == 0){
Toast.makeText(this, "You have no saved notes",Toast.LENGTH_SHORT).show();
return;
}else{
NoteAdapter na= new NoteAdapter(this, R.layout.item_note,notes);
mlistViewNotes.setAdapter(na);
mlistViewNotes.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String fileName =((Note)mlistViewNotes.getItemAtPosition(position)).getmDateTime() + Utilities.FILE_EXTENSION;
Intent viewNoteIntent= new Intent(getApplicationContext(), NoteActivity.class);
viewNoteIntent.putExtra("NOTE_FILE",fileName);
startActivity(viewNoteIntent);
}
});
}
}
Класс примечания
public Note(long mDateTime, String mTitle, String mContent) {
this.mDateTime = mDateTime;
this.mTitle = mTitle;
this.mContent = mContent;
}
public void setmDateTime(long mDateTime) {
this.mDateTime = mDateTime;
}
public void setmTitle(String mTitle) {
this.mTitle = mTitle;
}
public void setmContent(String mContent) {
this.mContent = mContent;
}
public long getmDateTime() {
return mDateTime;
}
public String getmTitle() {
return mTitle;
}
public String getmContent() {
return mContent;
}
public String getDateTimeFormatted(Context context){
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss",context.getResources().getConfiguration().locale);
sdf.setTimeZone(TimeZone.getDefault());
return sdf.format(new Date(mDateTime));
}
NoteActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
mEtTitle = findViewById(R.id.note_et_title);
mEtContent = findViewById(R.id.note_et_content);
mNoteFileName = getIntent().getStringExtra("NOTE_FILE");
if(mNoteFileName != null && !mNoteFileName.isEmpty()){
mLoadedNote = Utilities.getNoteByName(this,mNoteFileName);
if(mLoadedNote != null){
mEtTitle.setText(mLoadedNote.getmTitle());
mEtContent.setText(mLoadedNote.getmContent());
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_note_new, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_note_save:
saveNote();
case R.id.action_note_delete:
deleteNote();
break;
}
return true;
}
private void saveNote(){
Note note;
if(mEtTitle.getText().toString().trim().isEmpty() || mEtContent.getText().toString().trim().isEmpty()){
Toast.makeText(this," Please enter a title and a content",Toast.LENGTH_SHORT).show();
return;
}
if(mLoadedNote == null){
note =new Note(System.currentTimeMillis(), mEtTitle.getText().toString(),mEtContent.getText().toString());
}else{
note =new Note(mLoadedNote.getmDateTime(), mEtTitle.getText().toString(),mEtContent.getText().toString());
}
if(Utilities.saveNote(this,note)){
Toast.makeText(this,"Your note is saved", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"Can not save the note.", Toast.LENGTH_SHORT).show();
}
finish();
}
private void deleteNote() {
if(mLoadedNote == null){
finish();
}else{
AlertDialog.Builder dialog = new AlertDialog.Builder(this).setTitle("Delete").setMessage("You are about to delete " + mEtTitle.getText().toString() + ", are you sure?").setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Utilities.deleteNote(getApplicationContext(),mLoadedNote.getmDateTime() + Utilities.FILE_EXTENSION);
Toast.makeText(getApplicationContext(),mEtTitle.getText().toString() + " is deleted",Toast.LENGTH_SHORT).show();
finish();
}
})
.setNegativeButton("no",null).setCancelable(false);
dialog.show();
}
}
NoteAdapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_note,null);
}
Note note = getItem(position);
if(note !=null){
TextView title = convertView.findViewById(R.id.list_note_title);
TextView date = convertView.findViewById(R.id.list_note_date);
TextView content = convertView.findViewById(R.id.list_note_content);
title.setText(note.getmTitle());
date.setText(note.getDateTimeFormatted(getContext()));
if(note.getmContent().length() > 50 ){
content.setText(note.getmContent().substring(0,50));
}else{
content.setText(note.getmContent());
}
}
return convertView;
}
Utilies
public static boolean saveNote(Context context, Note note) {
String fileName = String.valueOf(note.getmDateTime()) + FILE_EXTENSION;
FileOutputStream fos;
ObjectOutputStream oos;
try {
fos = context.openFileOutput(fileName, context.MODE_PRIVATE);
oos = new ObjectOutputStream(fos);
oos.writeObject(note);
oos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false; //tell the user something went wrong.
}
return true;
}
public static ArrayList<Note> getAllSavedNotes(Context context) {
ArrayList<Note> notes = new ArrayList<>();
File filesDir = context.getFilesDir();
ArrayList<String> noteFiles = new ArrayList<>();
for (String file : filesDir.list()) {
if (file.endsWith(FILE_EXTENSION)) {
noteFiles.add(file);
}
}
FileInputStream fis;
ObjectInputStream ois;
for (int i = 0; i < noteFiles.size(); i++) {
try {
fis = context.openFileInput(noteFiles.get(i));
ois = new ObjectInputStream(fis);
notes.add((Note) ois.readObject());
fis.close();
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
return notes;
}
public static Note getNoteByName(Context context,String fileName){
File file = new File(context.getFilesDir(), fileName);
Note note;
if(file.exists()){
FileInputStream fis;
ObjectInputStream ois;
try{
fis =context.openFileInput(fileName);
ois = new ObjectInputStream(fis);
note = (Note) ois.readObject();
fis.close();
ois.close();
}catch (IOException | ClassNotFoundException e){
e.printStackTrace();
return null;
}
return note;
}
return null;
}
public static void deleteNote(Context context, String fileName){
File dir = context.getFilesDir();
File file = new File(dir, fileName);
if(file.exists()){
file.delete();
}
}