Tabii! Android için bir veritabanı kullanarak not tutma uygulaması geliştirelim. Bu uygulama, kullanıcıların notlarını kaydedebileceği, listeleyebileceği ve düzenleyebileceği bir arayüze sahip olacak.
- Dependencies ve XML Dosyalarını Ayarlama
- İlk olarak, Android Studio'da yeni bir proje oluşturun.
- build.gradle dosyasına aşağıdaki bağımlılığı ekleyin:
implementation 'androidx.room:room-runtime:2.4.0'
annotationProcessor 'androidx.room:room-compiler:2.4.0'
- activity_main.xml dosyasını aşağıdaki gibi güncelleyin:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Başlık" />
<EditText
android:id="@+id/editTextContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="İçerik" />
<Button
android:id="@+id/buttonAddNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not Ekle" />
<ListView
android:id="@+id/listViewNotes"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
- Veritabanı ve DAO Oluşturma
- Veritabanı için
NoteDatabase adında bir sınıf oluşturun ve RoomDatabase sınıfını genişeltin. Bu sınıf ayrıca Singleton deseni kullanacak şekilde tasarlanmalıdır:
@Database(entities = {Note.class}, version = 1)
public abstract class NoteDatabase extends RoomDatabase {
private static NoteDatabase instance;
public abstract NoteDao noteDao();
public static synchronized NoteDatabase getInstance(Context context) {
if (instance == null) {
instance = Room.databaseBuilder(context.getApplicationContext(),
NoteDatabase.class, "note_database")
.fallbackToDestructiveMigration()
.build();
}
return instance;
}
}
- DAO (Data Access Object) sınıfı olan
NoteDao oluşturun ve veritabanı işlemlerini tanımlayın:
@Dao
public interface NoteDao {
@Insert
void insert(Note note);
@Update
void update(Note note);
@Delete
void delete(Note note);
@Query("SELECT * FROM note_table ORDER BY priority DESC")
LiveData<List<Note>> getAllNotes();
}
- Son olarak,
Note adında bir model sınıfı oluşturun:
@Entity(tableName = "note_table")
public class Note {
@PrimaryKey(autoGenerate = true)
private int id;
private String title;
private String content;
private int priority;
public Note(String title, String content, int priority) {
this.title = title;
this.content = content;
this.priority = priority;
}
// Getter ve Setter metotları
}
- Activity Sınıfını Oluşturma ve Veritabanı Kullanma
- MainActivity sınıfınızı aşağıdaki kod parçasıyla güncelleyin:
public class MainActivity extends AppCompatActivity {
private EditText editTextTitle;
private EditText editTextContent;
private Button buttonAddNote;
private ListView listViewNotes;
private NoteViewModel noteViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextTitle = findViewById(R.id.editTextTitle);
editTextContent = findViewById(R.id.editTextContent);
buttonAddNote = findViewById(R.id.buttonAddNote);
listViewNotes = findViewById(R.id.listViewNotes);
noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
buttonAddNote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = editTextTitle.getText().toString().trim();
String content = editTextContent.getText().toString().trim();
int priority = 1;
if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(content)) {
Note note = new Note(title, content, priority);
noteViewModel.insert(note);
editTextTitle.setText("");
editTextContent.setText("");
} else {
Toast.makeText(MainActivity.this, "Başlık ve içerik girin", Toast.LENGTH_SHORT).show();
}
}
});
noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>() {
@Override
public void onChanged(List<Note> notes) {
// Notes listesini göstermek için bir ArrayAdapter kullanın
ArrayAdapter<Note> adapter = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1, notes);
listViewNotes.setAdapter(adapter);
}
});
}
}
- Son olarak, ViewModel sınıfını
NoteViewModel adıyla oluşturun:
public class NoteViewModel extends AndroidViewModel {
private NoteRepository repository;
private LiveData<List<Note>> allNotes;
public NoteViewModel(@NonNull Application application) {
super(application);
repository = new NoteRepository(application);
allNotes = repository.getAllNotes();
}
public void insert(Note note) {
repository.insert(note);
}
public void update(Note note) {
repository.update(note);
}
public void delete(Note note) {
repository.delete(note);
}
public LiveData<List<Note>> getAllNotes() {
return allNotes;
}
}
- Veritabanı işlemlerini yapacak olan
NoteRepository sınıfını aşağıdaki gibi oluşturun:
public class NoteRepository {
private NoteDao noteDao;
private LiveData<List<Note>> allNotes;
public NoteRepository(Application application) {
NoteDatabase database = NoteDatabase.getInstance(application);
noteDao = database.noteDao();
allNotes = noteDao.getAllNotes();
}
public void insert(Note note) {
new InsertNoteAsyncTask(noteDao).execute(note);
}
public void update(Note note) {
new UpdateNoteAsyncTask(noteDao).execute(note);
}
public void delete(Note note) {
new DeleteNoteAsyncTask(noteDao).execute(note);
}
public LiveData<List<Note>> getAllNotes() {
return allNotes;
}
private static class InsertNoteAsyncTask extends AsyncTask<Note, Void, Void> {
private NoteDao noteDao;
private InsertNoteAsyncTask(NoteDao noteDao) {
this.noteDao = noteDao;
}
@Override
protected Void doInBackground(Note... notes) {
noteDao.insert(notes[0]);
return null;
}
}
private static class UpdateNoteAsyncTask extends AsyncTask<Note, Void, Void> {
private NoteDao noteDao;
private UpdateNoteAsyncTask(NoteDao noteDao) {
this.noteDao = noteDao;
}
@Override
protected Void doInBackground(Note... notes) {
noteDao.update(notes[0]);
return null;
}
}
private static class DeleteNoteAsyncTask extends AsyncTask<Note, Void, Void> {
private NoteDao noteDao;
private DeleteNoteAsyncTask(NoteDao noteDao) {
this.noteDao = noteDao;
}
@Override
protected Void doInBackground(Note... notes) {
noteDao.delete(notes[0]);
return null;
}
}
}
Umarım bu kodlar size yardımcı olur! Not tutma uygulamasının temel işlevlerini yerine getirmenize imkan sağlar. Kodları projenize uyarlayabilir ve geliştirmeler yapabilirsiniz. Başka sorularınız varsa, sorunuz.