Bu derste yazılan veya değiştirilen kodlar aşağıdaki gibidir.
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();
//kategoriGoster();
// notEkle();
notlariGoster();
/* notOlustur();
notGuncelle();
notSil();
notlariOku();*/
spinner = (Spinner) findViewById(R.id.spinner);
notlarListesi = (ListView) findViewById(R.id.lvNotlar);
String[] notlar = getResources().getStringArray(R.array.notlar);
ArrayAdapter
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(); } 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, 15); 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); } }
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 |
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; 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) { 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); } } |
Burası çorba olmuş gibi