Sunucu-istemci arası veri yönetimi dışında, yalnızca lokal cihazlarda veri yönetimine ihtiyaç duyulduğunda kullanılabilecek en uygun sistemlerden birisi SQLite denilen oldukça hafif, SQL bilen bir kişinin kolayca adapte olabileceği bir veritabanı yönetim sistemidir.
Meslekî ve Teknik Anadolu Liseleri Bilişim Teknolojileri Alanı Mobil Uygulamar modülünde “Mobil Uygulama Geliştirme Ortamında Yerel Veri Tabanı Kullanmak” başlığı altında (s. 323) yer verilen uygulamadaki bazı eksiklikler sebebiyle uygulamanın tüm komutları paylaşılmaktadır.
Takip edebileceğiniz uygulama adımları şu şekildedir:
- Kendi bilgisayarınızda yüklü API hangisiyse bu ayarı değiştirmeden boş bir Activity (Empty Activity) ile uygulama oluşturunuz.
- Veritabanı işlemleri için Urun.java dosyası, veritabanı modeli için ihtiyacımız olan dosyadır. Dosyayı aşağıdaki kodlar ile oluşturunuz.
- listview_satir.xml adında bir layout dosyası oluşturunuz ve aşağıda verilen kodları kullanınız.
- UrunlerAdapter.java adında, veritabanı ile aktivitelerimiz arasındaki işlemlerin sağlanabilmesi amacıyla bir dosya oluşturunuz.
- activity_main.xml görünüm dosyası ile MainActivity.java dosyalarının içeriğini aşağıda verildiği gibi değiştiriniz. Bu işlemi yaparken Java dosyasındaki ilk satırda “package” bilgisini, projenizle uyumlu olacak şekilde değiştiriniz. (package com.teknikakil.veritabani)
- UrunDetay ve UrunKayit adlarında boş aktiviteler (Empty Activity) oluşturarak proje kodlarını aşağıdaki gibi değiştiriniz.
Görünüm (Layout) Dosyaları
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ListView android:id="@+id/urunListe" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toTopOf="@+id/buttonYeniKayit" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/buttonYeniKayit" android:layout_width="0dp" android:layout_height="wrap_content" android:text="YENİ KAYIT EKLE" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> |
activity_urun_detay.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".UrunDetay"> <ImageView android:id="@+id/urunResim" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@drawable/ic_launcher_foreground" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:includeFontPadding="false" android:text="ÜRÜN ADI" android:textColor="#880808" android:textStyle="bold" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/urunResim" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:includeFontPadding="false" android:text="FİYAT" android:textColor="#880808" android:textStyle="bold" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtUrunadi" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:includeFontPadding="false" android:text="ADET" android:textColor="#880808" android:textStyle="bold" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtUrunFiyati" /> <TextView android:id="@+id/txtUrunadi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" /> <TextView android:id="@+id/txtUrunFiyati" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2" /> <TextView android:id="@+id/txtUrunAdet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView3" /> <Button android:id="@+id/btnGeri" android:layout_width="0dp" android:layout_height="wrap_content" android:text="GERİ" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/btnSil" android:layout_width="0dp" android:layout_height="wrap_content" android:text="SİL" app:layout_constraintBottom_toTopOf="@+id/btnGeri" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/btnDegistir" android:layout_width="0dp" android:layout_height="wrap_content" android:text="DEĞİŞTİR" app:layout_constraintBottom_toTopOf="@+id/btnSil" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/btnResimEkle" android:layout_width="0dp" android:layout_height="wrap_content" android:text="RESİM EKLE" app:layout_constraintBottom_toTopOf="@+id/btnDegistir" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> |
activity_urun_kayit.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".UrunKayit"> <EditText android:id="@+id/editKayitUrunadi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:hint="ÜRÜN ADI" android:inputType="textPersonName" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editKayitFiyat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:hint="FİYAT" android:inputType="textPersonName" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editKayitUrunadi" /> <EditText android:id="@+id/editKayitAdet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:hint="ADET" android:inputType="textPersonName" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editKayitFiyat" /> <Button android:id="@+id/btnKayit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="KAYDET" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editKayitAdet" /> </androidx.constraintlayout.widget.ConstraintLayout> |
listview_satir.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/satirUrunResmi" android:layout_width="80dp" android:layout_height="80dp" android:scaleType="fitCenter" android:src="@drawable/ic_launcher_foreground"/> <LinearLayout android:layout_width="230dp" android:layout_height="wrap_content" android:paddingLeft="20dp" android:orientation="vertical"> <TextView android:id="@+id/satirUrunadi" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#212121" android:text="Ürün Adı" android:padding="5dp" android:textStyle="bold" android:textSize="20sp"/> <TextView android:id="@+id/satirUrunFiyat" android:textColor="#dd2c00" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Ürün Fiyatı" android:padding="5dp" android:textSize="16sp"/> </LinearLayout> <TextView android:id="@+id/satirUrunAdet" android:layout_width="match_parent" android:layout_height="match_parent" android:text="999" android:textColor="#f50057" android:textSize="27sp" android:padding="5dp" android:gravity="center" /> </LinearLayout> |
Java Dosyaları
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | package com.teknikakil.veritabani; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.RecyclerView; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteStatement; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { static UrunlerAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayList<Urun> liste = new ArrayList<Urun>(); Button buttonYeniKayit=findViewById(R.id.buttonYeniKayit); buttonYeniKayit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(getBaseContext(),UrunKayit.class); intent.putExtra("mod","yeni"); startActivity(intent); } }); SQLiteDatabase database; database = this.openOrCreateDatabase("urunler",MODE_PRIVATE,null); //database.execSQL("DROP TABLE urunler"); String TABLO="CREATE TABLE IF NOT EXISTS urunler(id INTEGER PRIMARY KEY, urunadi VARCHAR, fiyat DOUBLE, adet INTEGER, resim BLOB)"; database.execSQL(TABLO); /* String SORGU="INSERT INTO urunler(urunadi,fiyat,adet) VALUES(?,?,?)"; SQLiteStatement durumlar=database.compileStatement(SORGU); durumlar.bindString(1,"Televizyon"); durumlar.bindDouble(2,12500.00); durumlar.bindLong(3,12); durumlar.execute(); */ /* String SORGU="DELETE FROM urunler"; SQLiteStatement durumlar=database.compileStatement(SORGU); durumlar.execute(); */ //database=this.openOrCreateDatabase("urunler",MODE_PRIVATE,null); Cursor cursor=database.rawQuery("SELECT rowid,urunadi,fiyat,adet FROM urunler",null); String veriler=""; int kolonId=cursor.getColumnIndex("id"); int kolonUrunadi=cursor.getColumnIndex("urunadi"); int kolonFiyat= cursor.getColumnIndex("fiyat"); int kolonAdet=cursor.getColumnIndex("adet"); int i=0; while (cursor.moveToNext()){ i++; long id = cursor.getLong(kolonId); String urunadi=cursor.getString(kolonUrunadi); double fiyat=cursor.getDouble(kolonFiyat); int adet=cursor.getInt(kolonAdet); Urun urun = new Urun(id,urunadi,fiyat,adet); liste.add(urun); veriler=" "+urunadi+" "+fiyat+" "+adet+"\n"; //Log.i("KAYIT",veriler); } cursor.close(); //Toast.makeText(this, i+"", Toast.LENGTH_SHORT).show(); //Log.i("KAYIT",liste.get(0).getUrunadi()); //Log.i("KAYIT","toplam: "+liste.size()); adapter = new UrunlerAdapter(getApplicationContext(),liste); //UrunlerAdapter adapter = new UrunlerAdapter(this,R.layout.listview_satir,liste); ListView urunListe = findViewById(R.id.urunListe); urunListe.setAdapter(adapter); adapter.notifyDataSetChanged(); } } |
Urun.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package com.teknikakil.veritabani; import android.graphics.Bitmap; class Urun { private long id; private String urunadi; private double fiyat; private int adet; private Bitmap resim; public Urun(long id, String urunadi, double fiyat, int adet) { this.id = id; this.urunadi = urunadi; this.fiyat = fiyat; this.adet = adet; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getUrunadi() { return urunadi; } public void setUrunadi(String urunadi) { this.urunadi = urunadi; } public double getFiyat() { return fiyat; } public void setFiyat(double fiyat) { this.fiyat = fiyat; } public int getAdet() { return adet; } public void setAdet(int adet) { this.adet = adet; } public Bitmap getResim() { return resim; } public void setResim(Bitmap resim) { this.resim = resim; } } |
UrunDetay.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | package com.teknikakil.veritabani; import android.Manifest; import android.content.ContentValues; import android.content.Intent; import android.content.pm.PackageManager; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteStatement; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.ImageDecoder; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.activity.result.ActivityResult; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import java.io.ByteArrayOutputStream; public class UrunDetay extends AppCompatActivity { SQLiteDatabase database; TextView urunadi; TextView urunFiyat; TextView urunAdet; Button btnDegistir; Button btnGeri; Button btnSil; Button btnResimEkle; ImageView urunResim; long id; ActivityResultLauncher<Intent> galleryLauncher; ActivityResultLauncher<String> galleryPermisson; Bitmap bitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_urun_detay); urunadi=findViewById(R.id.txtUrunadi); urunFiyat=findViewById(R.id.txtUrunFiyati); urunAdet=findViewById(R.id.txtUrunAdet); btnDegistir=findViewById(R.id.btnDegistir); btnGeri=findViewById(R.id.btnGeri); btnSil=findViewById(R.id.btnSil); btnResimEkle=findViewById(R.id.btnResimEkle); urunResim=findViewById(R.id.urunResim); Intent intent=getIntent(); id=intent.getLongExtra("id",0); try { database=this.openOrCreateDatabase("urunler",MODE_PRIVATE,null); Cursor cursor=database.rawQuery("SELECT * FROM urunler WHERE id=?", new String[]{String.valueOf(id)}); int kolonUrunadi=cursor.getColumnIndex("urunadi"); int kolonFiyat=cursor.getColumnIndex("fiyat"); int kolonAdet=cursor.getColumnIndex("adet"); int kolonResim=cursor.getColumnIndex("resim"); while (cursor.moveToNext()){ urunadi.setText(cursor.getString(kolonUrunadi)); urunFiyat.setText(cursor.getString(kolonFiyat)+""); urunAdet.setText(cursor.getInt(kolonAdet)+""); byte[] bytes=cursor.getBlob(kolonResim); if (bytes.length>0) { Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); urunResim.setImageBitmap(bitmap); } } cursor.close(); } catch (Exception e) { e.printStackTrace(); } Log.d("SERVİS", "onCreate: "+ ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)); registerLauncher(); if(ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_EXTERNAL_STORAGE)){ Toast.makeText(this, "İzin gerekli", Toast.LENGTH_SHORT).show(); } else { galleryPermisson.launch(Manifest.permission.READ_EXTERNAL_STORAGE); } } btnResimEkle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intentToGallery=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); galleryLauncher.launch(intentToGallery); } }); btnDegistir.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(getApplicationContext(),UrunKayit.class); intent.putExtra("mod","degistir"); intent.putExtra("id",id); startActivity(intent); finish(); } }); btnSil.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { String SORGU="DELETE FROM urunler WHERE id=?"; SQLiteStatement durumlar=database.compileStatement(SORGU); durumlar.bindLong(1,id); durumlar.execute(); //MainActivity.adapter.notifyDataSetChanged(); Intent intent=new Intent(UrunDetay.this, MainActivity.class); startActivity(intent); }catch (Exception e){ e.printStackTrace(); } finish(); } }); btnGeri.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent1=new Intent(UrunDetay.this,MainActivity.class); startActivity(intent1); finish(); } }); } public Bitmap resimKucultucu(Bitmap b,int buyukluk){ double oran=(double) b.getWidth()/(double) b.getHeight(); double uzunluk=(double)buyukluk/oran; return bitmap.createScaledBitmap(bitmap,buyukluk, (int)uzunluk, true); } public void Kaydet(){ Bitmap kucukResim = resimKucultucu(bitmap, 250); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); kucukResim.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream); byte[] bytes = byteArrayOutputStream.toByteArray(); try { ContentValues contentValues=new ContentValues(); contentValues.put("resim",bytes); database.update("urunler",contentValues,"id="+id,null); } catch (Exception e) { e.printStackTrace(); } } public void registerLauncher(){ galleryLauncher=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() { @Override public void onActivityResult(ActivityResult result) { if(result.getResultCode()==RESULT_OK){ Intent intentResult=result.getData(); if(intentResult!=null){ Uri galleryUri=intentResult.getData(); try { if(Build.VERSION.SDK_INT>=28){ ImageDecoder.Source source=ImageDecoder.createSource(getContentResolver(),galleryUri); bitmap=ImageDecoder.decodeBitmap(source); urunResim.setImageBitmap(bitmap); Kaydet(); }else{ bitmap= MediaStore.Images.Media.getBitmap(getContentResolver(),galleryUri); urunResim.setImageBitmap(bitmap); Kaydet(); } }catch (Exception e){ e.printStackTrace(); } } } } }); galleryPermisson=registerForActivityResult(new ActivityResultContracts.RequestPermission(), new ActivityResultCallback<Boolean>() { @Override public void onActivityResult(Boolean result) { if(result){ Intent intentToGallery=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); galleryLauncher.launch(intentToGallery); }else{ Toast.makeText(UrunDetay.this,"İzin vermeniz gerekli!",Toast. LENGTH_LONG).show(); } } }); } } |
UrunKayit.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | package com.teknikakil.veritabani; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteStatement; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import androidx.appcompat.app.AppCompatActivity; import com.teknikakil.veritabani.MainActivity; import com.teknikakil.veritabani.R; public class UrunKayit extends AppCompatActivity { SQLiteDatabase database; EditText urunadi; EditText urunFiyat; EditText urunAdet; Button btnKaydet; long id; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_urun_kayit); urunadi=findViewById(R.id.editKayitUrunadi); urunFiyat=findViewById(R.id.editKayitFiyat); urunAdet=findViewById(R.id.editKayitAdet); btnKaydet=findViewById(R.id.btnKayit); Intent intent=getIntent(); id=intent.getLongExtra("id",0); String mod=intent.getStringExtra("mod"); database=this.openOrCreateDatabase("urunler",MODE_PRIVATE,null); if(mod.equals("degistir")){ try { Cursor cursor=database.rawQuery("SELECT urunadi,fiyat,adet FROM urunler WHERE id=?", new String[]{String.valueOf(id)}); int kolonUrunadi=cursor.getColumnIndex("urunadi"); int kolonFiyat=cursor.getColumnIndex("fiyat"); int kolonAdet=cursor.getColumnIndex("adet"); while (cursor.moveToNext()){ urunadi.setText(cursor.getString(kolonUrunadi)); urunFiyat.setText(cursor.getString(kolonFiyat)+""); urunAdet.setText(cursor.getInt(kolonAdet)+""); } cursor.close(); }catch (Exception e) { e.printStackTrace(); } } btnKaydet.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(mod.equals("degistir")){ String SORGU="UPDATE urunler SET urunadi=?,fiyat=?,adet=? WHERE id=?"; SQLiteStatement durumlar=database.compileStatement(SORGU); durumlar.bindString(1,urunadi.getText().toString()); durumlar.bindDouble(2,Double.parseDouble(urunFiyat.getText().toString())); durumlar.bindLong(3,Integer.parseInt(urunAdet.getText().toString())); durumlar.bindLong(4,id); durumlar.execute(); }else{ String SORGU="INSERT INTO urunler(urunadi,fiyat,adet) VALUES(?,?,?)"; SQLiteStatement durumlar=database.compileStatement(SORGU); durumlar.bindString(1,urunadi.getText().toString()); durumlar.bindDouble(2,Double.parseDouble(urunFiyat.getText().toString())); durumlar.bindLong(3,Integer.parseInt(urunAdet.getText().toString())); durumlar.execute(); } Intent intent=new Intent(UrunKayit.this, MainActivity.class); startActivity(intent); } }); } } |
UrunlerAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | package com.teknikakil.veritabani; import android.content.Context; import android.content.Intent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class UrunlerAdapter extends ArrayAdapter<Urun> { Context context; public UrunlerAdapter(Context context, ArrayList<Urun> liste) { super(context, R.layout.listview_satir, liste); this.context = context; this.liste = liste; } ArrayList<Urun> liste; @Override public int getCount() { return liste.size(); } /* //It's a BaseAdapter method @Override public Object getItem(int i) { return null; } */ @Override public long getItemId(int i) { return liste.get(i).getId(); } @Override public View getView(int i, View view, ViewGroup viewGroup) { TextView satirUrunadi; TextView satirUrunFiyat; TextView satirUrunAdet; ImageView satirUrunResmi; Urun urun=liste.get(i); if(view==null){ //view= LayoutInflater.from(context).inflate(R.layout.listview_satir,viewGroup,false); LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.listview_satir,null); } satirUrunadi=(TextView) view.findViewById(R.id.satirUrunadi); satirUrunFiyat=view.findViewById(R.id.satirUrunFiyat); satirUrunAdet=view.findViewById(R.id.satirUrunAdet); satirUrunResmi=view.findViewById(R.id.satirUrunResmi); satirUrunadi.setText(urun.getUrunadi()); satirUrunFiyat.setText(String.format("%.02f",urun.getFiyat())+ " TL"); satirUrunAdet.setText(urun.getAdet()+""); if(urun.getResim()!=null) satirUrunResmi.setImageBitmap(urun.getResim()); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Toast.makeText(context,"TIK"+urun.getId(),Toast.LENGTH_SHORT).show(); Intent intent = new Intent(context,UrunDetay.class); intent.putExtra("id",urun.getId()); intent.putExtra("mod","degistir"); context.startActivity(intent); } }); return view; } } |
Diğer Dosyalar
Bu başlık altındaki dosyalar, uygulamada hata aldığınız takdirde işinize yarayacak ayarlamaları içermektedir.
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.teknikakil.veritabani"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Veritabani"> <activity android:name=".UrunKayit"></activity> <activity android:name=".UrunDetay"></activity> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
build.gradle (Project)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath "com.android.tools.build:gradle:4.1.1" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir } |
build.gradle (Module)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | plugins { id 'com.android.application' } android { compileSdkVersion 32 buildToolsVersion "30.0.3" defaultConfig { applicationId "com.teknikakil.veritabani" minSdkVersion 25 targetSdkVersion 32 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.6.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' } |
gradle-wrapper.properties
1 2 3 4 5 | distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip |