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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
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 { private static final int TUM_NOTLAR = -1; private static final int TUM_KATEGORILER = -1; 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); kategoriGoster(); notlariGoster(); 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; } if (id==R.id.action_tum_kategorileri_sil){ kategorileriSil(TUM_KATEGORILER); kategoriGoster(); return true; } if(id==R.id.action_tum_notlari_sil){ notlariSil(TUM_NOTLAR); notlariGoster(); 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); getContentResolver().insert(NotDefterimContract.KategoriEntry.CONTENT_URI, values); } } private void testNotlarOlustur(){ for(int i=1; i<=20; i++){ ContentValues yeniKayit = new ContentValues(); yeniKayit.put(NotlarEntry.COLUMN_NOT_ICERIK, "YENİ NOT #"+i); yeniKayit.put(NotlarEntry.COLUMN_KATEGORI_ID, i); yeniKayit.put(NotlarEntry.COLUMN_OLUSTURULMA_TARIHI, "06-05-2017"); yeniKayit.put(NotlarEntry.COLUMN_YAPILDI, 1); Uri _uri=getContentResolver().insert(NotlarEntry.CONTENT_URI, yeniKayit); } } 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(); } 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 notEkle(){ ContentValues yeniKayit = new ContentValues(); yeniKayit.put(NotlarEntry.COLUMN_NOT_ICERIK, "YENİ NOT EKLENDI"); yeniKayit.put(NotlarEntry.COLUMN_KATEGORI_ID, 1); yeniKayit.put(NotlarEntry.COLUMN_OLUSTURULMA_TARIHI, "06-05-2017"); yeniKayit.put(NotlarEntry.COLUMN_YAPILDI, 1); Uri _uri=getContentResolver().insert(NotlarEntry.CONTENT_URI, yeniKayit); Toast.makeText(this, "Not Eklendi:"+_uri, Toast.LENGTH_LONG).show(); } private void notlariGoster(){ String[] projection={"notlar._id","notlar.notIcerik","kategoriler._id", "kategoriler.kategori"}; Cursor cursor= getContentResolver().query(NotlarEntry.CONTENT_URI, projection,null,null,null); String tumNotlar=""; while(cursor.moveToNext()){ for(int i=0; i<=3; i++){ tumNotlar=tumNotlar+cursor.getString(i)+" - "; } tumNotlar += "\n"; } Toast.makeText(this, tumNotlar, Toast.LENGTH_LONG).show(); Log.d("VERI", tumNotlar); } 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; } int id= getContentResolver().delete(NotlarEntry.CONTENT_URI,selection,args); Toast.makeText(this, "Kayıt Silindi:"+id, Toast.LENGTH_LONG).show(); } 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(); } } |
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 |
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); } 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; } 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; } 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; } return id; } } |