Bu derste yazılan veya değiştirilen kodlar aşağıdaki gibidir.
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 205 206 207 208 209 210 211 |
package emrealtunbilek.com.notdefterim; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Spinner; import android.widget.Toast; import emrealtunbilek.com.notdefterim.data.DatabaseHelper; import emrealtunbilek.com.notdefterim.data.NotDefterimContract; import emrealtunbilek.com.notdefterim.data.NotDefterimContract.NotlarEntry; public class MainActivity extends AppCompatActivity { Spinner spinner; ListView notlarListesi; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); kategoriEkle(); /* notOlustur(); notGuncelle(); notSil(); notlariOku();*/ spinner = (Spinner) findViewById(R.id.spinner); notlarListesi = (ListView) findViewById(R.id.lvNotlar); String[] notlar = getResources().getStringArray(R.array.notlar); ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.notlar_tek_satir, R.id.tvNot, notlar); notlarListesi.setAdapter(adapter); notlarListesi.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(MainActivity.this, NotActivity.class); String tiklanilanNot = (String) notlarListesi.getItemAtPosition(position); intent.putExtra("notIcerik", tiklanilanNot); startActivity(intent); } }); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, NotActivity.class); startActivity(intent); } }); } @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; } if (id == R.id.action_kategori) { Intent intent = new Intent(this, KategoriActivity.class); startActivity(intent); return true; } return super.onOptionsItemSelected(item); } private void notOlustur() { DatabaseHelper helper = new DatabaseHelper(this); SQLiteDatabase db = helper.getWritableDatabase(); String insertSorgusu = "INSERT INTO notlar (" + NotlarEntry.COLUMN_NOT_ICERIK + "," + NotlarEntry.COLUMN_KATEGORI_ID + "," + NotlarEntry.COLUMN_OLUSTURULMA_TARIHI + "," + NotlarEntry.COLUMN_BITIS_TARIHI + " ," + NotlarEntry.COLUMN_YAPILDI + ")" + " VALUES (\"SPORA GIT\", 1, \"07-05-2017\", \"\", 0)"; db.execSQL(insertSorgusu); ContentValues yeniKayit = new ContentValues(); yeniKayit.put(NotlarEntry.COLUMN_NOT_ICERIK, "Okula Uğra"); yeniKayit.put(NotlarEntry.COLUMN_KATEGORI_ID, 1); yeniKayit.put(NotlarEntry.COLUMN_OLUSTURULMA_TARIHI, "06-05-2017"); yeniKayit.put(NotlarEntry.COLUMN_YAPILDI, 0); long id = db.insert(NotlarEntry.TABLE_NAME, null, yeniKayit); db.close(); } private void notlariOku() { DatabaseHelper helper = new DatabaseHelper(this); SQLiteDatabase db = helper.getReadableDatabase(); String[] projection = {NotlarEntry._ID, NotlarEntry.COLUMN_NOT_ICERIK, NotlarEntry.COLUMN_OLUSTURULMA_TARIHI, NotlarEntry.COLUMN_BITIS_TARIHI, NotlarEntry.COLUMN_YAPILDI, NotlarEntry.COLUMN_KATEGORI_ID}; String selection = NotlarEntry.COLUMN_KATEGORI_ID + " = ?"; String[] selectionArgs = {"1"}; Cursor c = db.query(NotlarEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, null); int i = c.getCount(); Toast.makeText(this, "Satır sayısı=" + i, Toast.LENGTH_LONG).show(); String tumNotlar = ""; while (c.moveToNext()) { for (int j = 0; j <= 5; j++) { tumNotlar += c.getString(j) + " - "; } tumNotlar += "\n"; } Log.e("SONUC", tumNotlar); c.close(); db.close(); } private void notGuncelle(){ DatabaseHelper helper=new DatabaseHelper(this); SQLiteDatabase db=helper.getReadableDatabase(); ContentValues guncellenenDegerler=new ContentValues(); guncellenenDegerler.put(NotlarEntry.COLUMN_NOT_ICERIK, "YENİ GÜNCELLENEN DEĞER"); String[] args={"45"}; int etkilenenSatirSayisi=db.update(NotlarEntry.TABLE_NAME,guncellenenDegerler, NotlarEntry._ID + " = ?", args); Toast.makeText(this, "Güncellenen satır sayısı: "+etkilenenSatirSayisi, Toast.LENGTH_LONG).show(); } private void notSil(){ DatabaseHelper helper=new DatabaseHelper(this); SQLiteDatabase db=helper.getReadableDatabase(); for(int i=50; i<=60; i++){ String[] args={String.valueOf(i)}; int etkilenenSatirSayisi=db.delete(NotlarEntry.TABLE_NAME, NotlarEntry._ID + " = ?", args); } // Toast.makeText(this, "Silinen satır sayısı: "+etkilenenSatirSayisi, Toast.LENGTH_LONG).show(); } private void kategoriEkle(){ ContentValues values=new ContentValues(); values.put(NotDefterimContract.KategoriEntry.COLUMN_KATEGORI, "Deneme Kategori"); Uri _uri=getContentResolver().insert(NotDefterimContract.KategoriEntry.CONTENT_URI, values); Toast.makeText(this, "Eklendi:"+_uri, Toast.LENGTH_LONG).show(); } } |
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 |
package emrealtunbilek.com.notdefterim.data; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.Log; public class NotDefterimProvider extends ContentProvider { private static final UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH); private static final int URICODE_NOTLAR=1; private static final int URICODE_KATEGORILER=2; static { matcher.addURI(NotDefterimContract.CONTENT_AUTHORITY, NotDefterimContract.PATH_NOTLAR, URICODE_NOTLAR); matcher.addURI(NotDefterimContract.CONTENT_AUTHORITY, NotDefterimContract.PATH_KATEGORILER, URICODE_KATEGORILER); } private SQLiteDatabase db; private DatabaseHelper helper; @Override public boolean onCreate() { helper=new DatabaseHelper(getContext()); db=helper.getWritableDatabase(); return true; } @Nullable @Override public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) { return null; } @Nullable @Override public String getType(@NonNull Uri uri) { return null; } @Nullable @Override public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) { switch (matcher.match(uri)){ case URICODE_NOTLAR: return kayitEkle(uri, values, NotDefterimContract.NotlarEntry.TABLE_NAME); case URICODE_KATEGORILER: return kayitEkle(uri, values, NotDefterimContract.KategoriEntry.TABLE_NAME); default: throw new IllegalArgumentException("INSERT BILINMEYEN URI:"+uri); } } @Override public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) { return 0; } @Override public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) { return 0; } private Uri kayitEkle(Uri uri, ContentValues values, String tabloAdi){ long id= db.insert(tabloAdi, null, values); if(id==-1){ Log.e("NotDefterimProvider", "INSERT HATA"+uri); return null; } return ContentUris.withAppendedId(uri, id); } } |
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 |
package emrealtunbilek.com.notdefterim.data; import android.net.Uri; import android.provider.BaseColumns; public class NotDefterimContract { public static final String CONTENT_AUTHORITY="emrealtunbilek.com.notdefterim.notdefterimprovider"; public static final String PATH_NOTLAR="notlar"; public static final String PATH_KATEGORILER="kategoriler"; public static final Uri BASE_CONTENT_URI=Uri.parse("content://" +CONTENT_AUTHORITY); public static final class NotlarEntry implements BaseColumns { public static final Uri CONTENT_URI=Uri.withAppendedPath(BASE_CONTENT_URI, PATH_NOTLAR); //content://emrealtunbilek.com.notdefterim.notdefterimprovider/notlar public static final String TABLE_NAME = "notlar"; public static final String _ID = BaseColumns._ID; public static final String COLUMN_NOT_ICERIK = "notIcerik"; public static final String COLUMN_OLUSTURULMA_TARIHI = "olusturulmaTarihi"; public static final String COLUMN_BITIS_TARIHI = "bitisTarihi"; public static final String COLUMN_YAPILDI = "yapildi"; public static final String COLUMN_KATEGORI_ID = "kategoriID"; } public static final class KategoriEntry implements BaseColumns { public static final Uri CONTENT_URI=Uri.withAppendedPath(BASE_CONTENT_URI, PATH_KATEGORILER); //content://emrealtunbilek.com.notdefterim.notdefterimprovider/kategoriler public static final String TABLE_NAME = "kategoriler"; public static final String _ID = BaseColumns._ID; public static final String COLUMN_KATEGORI = "kategori"; } } |