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 |
package emrealtunbilek.com.notdefterim; import android.content.Context; import android.database.Cursor; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CursorAdapter; import android.widget.TextView; import emrealtunbilek.com.notdefterim.data.NotDefterimContract; public class NotlarCursorAdapter extends CursorAdapter { public NotlarCursorAdapter(Context context, Cursor c, boolean autoRequery) { super(context, c, autoRequery); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return LayoutInflater.from(context).inflate(R.layout.notlar_tek_satir, parent, false); } @Override public void bindView(View view, Context context, Cursor cursor) { TextView not= (TextView) view.findViewById(R.id.tvNot); int notColumnIndex=cursor.getColumnIndex(NotDefterimContract.NotlarEntry.COLUMN_NOT_ICERIK); String notIcerigi=cursor.getString(notColumnIndex); not.setText(notIcerigi); } } |
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 |
package emrealtunbilek.com.notdefterim.data; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import emrealtunbilek.com.notdefterim.data.NotDefterimContract.KategoriEntry; import emrealtunbilek.com.notdefterim.data.NotDefterimContract.NotlarEntry; public class DatabaseHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME="notdefterim.db"; private static final int DATABASE_VERSION=2; private static final String TABLE_KATEGORILER_CREATE= "CREATE TABLE " + KategoriEntry.TABLE_NAME + " ("+ KategoriEntry._ID + " INTEGER PRIMARY KEY, "+ KategoriEntry.COLUMN_KATEGORI + " TEXT)"; private static final String TABLE_NOTLAR_CREATE= "CREATE TABLE " + NotlarEntry.TABLE_NAME + " ("+ NotlarEntry._ID + " INTEGER PRIMARY KEY, "+ NotlarEntry.COLUMN_NOT_ICERIK + " TEXT, " + NotlarEntry.COLUMN_OLUSTURULMA_TARIHI + " TEXT DEFAULT CURRENT_TIMESTAMP, "+ NotlarEntry.COLUMN_BITIS_TARIHI + " TEXT,"+ NotlarEntry.COLUMN_YAPILDI + " INTEGER," + NotlarEntry.COLUMN_KATEGORI_ID + " INTEGER,"+ " FOREIGN KEY ( "+ NotlarEntry.COLUMN_KATEGORI_ID + ")" + " REFERENCES "+ KategoriEntry.TABLE_NAME + "("+ KategoriEntry._ID + ") " + ")"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(TABLE_KATEGORILER_CREATE); db.execSQL(TABLE_NOTLAR_CREATE); } @Override public void onConfigure(SQLiteDatabase db) { db.setForeignKeyConstraintsEnabled(true); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + KategoriEntry.TABLE_NAME); db.execSQL("DROP TABLE IF EXISTS "+ NotlarEntry.TABLE_NAME); onCreate(db); } } |
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 266 267 |
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(); Cursor cursor=notlariGoster(); spinner = (Spinner) findViewById(R.id.spinner); notlarListesi = (ListView) findViewById(R.id.lvNotlar); NotlarCursorAdapter 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) { // 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<=520; 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<=520; 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); } notlariGoster(); } 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 Cursor 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);*/ return cursor; } 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(); } } |