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 |
package emrealtunbilek.com.notdefterim.data; import android.content.AsyncQueryHandler; import android.content.ContentResolver; public class NotDefterimQueryHandler extends AsyncQueryHandler { public NotDefterimQueryHandler(ContentResolver cr) { super(cr); } } |
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 |
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.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.Log; import android.widget.Toast; 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) { Cursor cursor; SQLiteQueryBuilder builder; String tabloBirlestir="notlar inner join kategoriler on notlar.kategoriID = kategoriler._id"; switch (matcher.match(uri)){ case URICODE_NOTLAR: builder=new SQLiteQueryBuilder(); builder.setTables(tabloBirlestir); cursor= builder.query(db, projection,selection,selectionArgs,null,null,null); break; case URICODE_KATEGORILER: cursor=db.query(NotDefterimContract.KategoriEntry.TABLE_NAME, projection,selection,selectionArgs,null,null,null); break; default: throw new IllegalArgumentException("BILINMEYEN QUERY URI"+uri); } cursor.setNotificationUri(getContext().getContentResolver(), uri); return cursor; } @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) { //Toast.makeText(getContext(), "Eslesen kod:"+matcher.match(uri)+" Link:"+uri, Toast.LENGTH_LONG).show(); switch (matcher.match(uri)){ case URICODE_NOTLAR: return kayitSil(uri, selection, selectionArgs,NotDefterimContract.NotlarEntry.TABLE_NAME); case URICODE_KATEGORILER: return kayitSil(uri, selection, selectionArgs,NotDefterimContract.KategoriEntry.TABLE_NAME); default: throw new IllegalArgumentException("BILINMEYEN DELETE URI"+uri); } } @Override public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) { switch (matcher.match(uri)){ case URICODE_NOTLAR: return kayitGuncelle(uri,values,selection,selectionArgs, NotDefterimContract.NotlarEntry.TABLE_NAME); case URICODE_KATEGORILER: return kayitGuncelle(uri,values,selection,selectionArgs, NotDefterimContract.KategoriEntry.TABLE_NAME); default: throw new IllegalArgumentException("BILINMEYEN UPDATE URI"+uri); } } 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; } getContext().getContentResolver().notifyChange(uri, null); return ContentUris.withAppendedId(uri, id); } private int kayitGuncelle(Uri uri, ContentValues values, String selection, String[] selectionArgs, String tabloAdi){ int id= db.update(tabloAdi, values, selection,selectionArgs); if(id==0) { Log.e("HATA", "UPDATE HATA"+uri); return -1; } getContext().getContentResolver().notifyChange(uri, null); return id; } private int kayitSil(Uri uri, String selection, String[] selectionArgs, String tabloAdi){ int id= db.delete(tabloAdi, selection,selectionArgs); if(id==0) { Log.e("HATA", "UPDATE HATA"+uri); return -1; } getContext().getContentResolver().notifyChange(uri, null); return 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 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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
package emrealtunbilek.com.notdefterim; import android.app.LoaderManager; import android.content.ContentValues; import android.content.CursorLoader; import android.content.Intent; import android.content.Loader; import android.database.Cursor; 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.ListView; import android.widget.Spinner; import android.widget.Toast; import emrealtunbilek.com.notdefterim.data.NotDefterimContract; import emrealtunbilek.com.notdefterim.data.NotDefterimContract.NotlarEntry; import emrealtunbilek.com.notdefterim.data.NotDefterimQueryHandler; public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> { private static final int TUM_NOTLAR = -1; private static final int TUM_KATEGORILER = -1; Spinner spinner; ListView notlarListesi; NotlarCursorAdapter adapter; Cursor cursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); //loader initialize edilir ve oncreateloader tetiklenir getLoaderManager().initLoader(100, null, this); spinner = (Spinner) findViewById(R.id.spinner); notlarListesi = (ListView) findViewById(R.id.lvNotlar); adapter = new NotlarCursorAdapter(this, cursor, false); 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) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); 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; } if (id == R.id.action_tum_kategorileri_sil) { kategorileriSil(TUM_KATEGORILER); return true; } if (id == R.id.action_tum_notlari_sil) { notlariSil(TUM_NOTLAR); return true; } if (id == R.id.action_test_kategoriler) { testKategorilerOlustur(); return true; } if (id == R.id.action_test_notlar) { testNotlarOlustur(); return true; } return super.onOptionsItemSelected(item); } private void testKategorilerOlustur() { for (int i = 1; i <= 20; i++) { ContentValues values = new ContentValues(); values.put(NotDefterimContract.KategoriEntry.COLUMN_KATEGORI, "Deneme Kategori #" + i); NotDefterimQueryHandler handler = new NotDefterimQueryHandler(this.getContentResolver()); handler.startInsert(1, null, NotDefterimContract.KategoriEntry.CONTENT_URI, values); } } private void testNotlarOlustur() { for (int i = 1; i <= 1000; i++) { ContentValues yeniKayit = new ContentValues(); yeniKayit.put(NotlarEntry.COLUMN_NOT_ICERIK, "YENİ NOT #" + i); yeniKayit.put(NotlarEntry.COLUMN_KATEGORI_ID, (i % 20) + 1); yeniKayit.put(NotlarEntry.COLUMN_OLUSTURULMA_TARIHI, "06-05-2017"); yeniKayit.put(NotlarEntry.COLUMN_YAPILDI, 1); NotDefterimQueryHandler handler = new NotDefterimQueryHandler(this.getContentResolver()); handler.startInsert(1, null, NotlarEntry.CONTENT_URI, yeniKayit); } } private void kategoriGoster() { String[] projection = {"_id", "kategori"}; Cursor cursor = getContentResolver().query(NotDefterimContract.KategoriEntry.CONTENT_URI, projection, null, null, null, null); String tumKategoriler = ""; while (cursor.moveToNext()) { String id = cursor.getString(0); String kategori = cursor.getString(1); tumKategoriler = tumKategoriler + "id:" + id + " kategori:" + kategori + "\n"; } Toast.makeText(this, tumKategoriler, Toast.LENGTH_LONG).show(); Log.d("VERI", tumKategoriler); } private void notlariGuncelle() { ContentValues values = new ContentValues(); values.put(NotlarEntry.COLUMN_NOT_ICERIK, "güncellenen yeni değer"); int id = getContentResolver().update(NotlarEntry.CONTENT_URI, values, "_id=?", new String[]{"8"}); Toast.makeText(this, "Kayıt Güncellendi:" + id, Toast.LENGTH_LONG).show(); } private void notlariSil(int silinecekID) { String selection = "_id=?"; String[] args = {String.valueOf(silinecekID)}; if (silinecekID == TUM_NOTLAR) { selection = null; args = null; } NotDefterimQueryHandler handler = new NotDefterimQueryHandler(this.getContentResolver()); handler.startDelete(1, null, NotlarEntry.CONTENT_URI, selection, args); } private void kategorileriSil(int silinecekID) { String selection = "_id=?"; String[] args = {String.valueOf(silinecekID)}; if (silinecekID == TUM_KATEGORILER) { selection = null; args = null; } int id = getContentResolver().delete(NotDefterimContract.KategoriEntry.CONTENT_URI, selection, args); Toast.makeText(this, "Kayıt Silindi:" + id, Toast.LENGTH_LONG).show(); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { if (id == 100) { String[] projection = {"notlar._id", "notlar.notIcerik", "kategoriler._id", "kategoriler.kategori"}; return new CursorLoader(this, NotlarEntry.CONTENT_URI, projection, null, null, null); } return null; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { adapter.swapCursor(data); } @Override public void onLoaderReset(Loader<Cursor> loader) { adapter.swapCursor(null); } } |
Emre bey merhaba.
Video eğitimleriniz çok güzel. İyi ki sizi tanıdım. Sayenizde konuları mantığıyla birlikte anlayabiliyorum.
Anlatımınız çok güzel. Her gün sizi izliyorum. Java, android hepsini de keyifle izliyorum. Bunlar bitince fluter a geçeceğim. Teşekkür ediyorum. Allah sizden razı olsun. Saygılarımla;