İlginiz için Teşekkürler.
Bahsettiğim kod bir başka cihazda (Galaxy Gio) hatasız çalışmakta. Fakat note 2 ve S4 te hala bahsi geçen hatayı veriyor.
Kodu satır satır çalıştırdığımda "yeniNot içinde ki 'Kaydet' " butonuna bastıldığı an uygulama çöküyor.
Sizin istediğiniz kısımları tam olarak anlamadığımdan kodların tamamını paylaşıyorum, kusura bakmayın.
[code]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
[/code]
[code]
package com.omer.DBVeritabani;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
NotlarDatabase notlarr; // references producted for NotlarDatabase class
ArrayAdapter<String> adapter; // adapter references producted
public String[] PROJ = { "id", "subjects", "contents" }; //
static final int KONU_ICERIK_GONDER = 1;
public static ArrayList<String> subjectsArray = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
notlarr = new NotlarDatabase(MainActivity.this);
ListView listV = (ListView) findViewById(R.id.list);
registerForContextMenu(listV);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
subjectsArray);
listV.setAdapter(adapter);
// subjectsArray.add("asdasdd");
subjectsArray.clear();
Cursor cursor = kayitGetir();
tumDBSubjectAl(cursor);
listV.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String subject = subjectsArray.get(arg2).toString();
Cursor curssor = wordQuery(subject);
String contentt = icerikAl(curssor);
AlertDialog.Builder dialogCreater = new AlertDialog.Builder(
MainActivity.this);
dialogCreater.setMessage(contentt);
dialogCreater.setCancelable(true);
dialogCreater.create().show();
}
});
}
@Override
protected void onResume() {
super.onResume();
}
private void tumDBSubjectAl(Cursor cursor) {
while (cursor.moveToNext()) {
// int id = cursor.getInt(cursor.getColumnIndex("id"));
String subjectssa = cursor.getString(cursor
.getColumnIndex("subjects"));
// String operatorkodu =
// cursor.getString(cursor.getColumnIndex("contents"));
subjectsArray.add(subjectssa);
}
cursor.close();
}
public Cursor wordQuery(String notesSubject) { // Verilen konuya göre içerik
// ara
String where = "subjects" + "=?";
String[] whereArgs = new String[] { notesSubject };
SQLiteDatabase db = notlarr.getReadableDatabase();
Cursor cursor = db.query("notes", PROJ, where, whereArgs, null, null,
null, null);
return cursor;
}
public String icerikAl(Cursor cursor) {
cursor.moveToNext();
String contentt = cursor.getString(cursor.getColumnIndex("contents"));
return contentt;
}
public int getId(Cursor cursor) {
cursor.moveToNext();
int idIndex = cursor.getInt(cursor.getColumnIndex("id"));
return idIndex;
}
@Override
// argüman olarak view (listview) alan menu. Viewlere uzun tıklama
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
// uzun süre tıklamadan sonra "sil ve düzenle menulerinden hangisine "
// basılırsa ona göre işlemleri yürütecek olan kısım
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
int index = info.position;
String selectedItem = subjectsArray.get(index);
switch (item.getItemId()) {
case R.id.itemSil:
String forDelete = selectedItem + " Için Sil Tıklandı";
Toast.makeText(MainActivity.this, forDelete, Toast.LENGTH_LONG)
.show();
subjectsArray.remove(index);
Cursor cursor = wordQuery(selectedItem);
int idIndexx = getId(cursor);
notSil(idIndexx);
adapter.notifyDataSetChanged();
return true;
case R.id.itemDuzenle:
String forEdit = selectedItem + " Için Duzenle Tıklandı";
Toast.makeText(MainActivity.this, forEdit, Toast.LENGTH_LONG)
.show();
return true;
default:
return super.onContextItemSelected(item);
}
}
private void notSil(int idIndexx) {
SQLiteDatabase db = notlarr.getWritableDatabase();
String where = "id" + "=" + idIndexx;
db.delete("notes", where, null);
}
public void konuGoster(Cursor cursor) {
cursor.moveToLast();
String konu = cursor.getString(cursor.getColumnIndex("subjects"));
stopManagingCursor(cursor);
cursor.close();
subjectsArray.add(konu); // burası donduğu yer
}
public Cursor kayitGetir() {
SQLiteDatabase dbb = notlarr.getReadableDatabase();
Cursor cursor = dbb.query("notes", PROJ, null, null, null, null, null);
startManagingCursor(cursor);
return cursor;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == KONU_ICERIK_GONDER) {
if (resultCode == RESULT_OK) {
// Toast.makeText(getApplicationContext(), "içeri girdi",
// Toast.LENGTH_LONG).show();
adapter.notifyDataSetChanged();
try {
Cursor cursor = kayitGetir();
konuGoster(cursor);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
notlarr.close();
}
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent(MainActivity.this, Yeninot.class);
startActivityForResult(intent, KONU_ICERIK_GONDER);
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
}
[code]
[/code]
package com.omer.DBVeritabani;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Yeninot extends Activity {
NotlarDatabase notlarr;
String sad = "OOOOO";
public String[] SELECT = { "id", "subjects", "contents" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yeninot);
notlarr = new NotlarDatabase(Yeninot.this);
final EditText subjectET = (EditText) findViewById(R.id.konuEditText);
final EditText contentET = (EditText) findViewById(R.id.icerikEditText);
final Button saveButton = (Button) findViewById(R.id.kaydetButton);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String subject = subjectET.getText().toString();
String content = contentET.getText().toString();
subjectET.setText("");
contentET.setText("");
kayitEkle(subject, content);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i(sad, "KKKKKKKKKKKKK");
} finally {
notlarr.close();
}
// tıklamanın sonunda startactivityforResult ile geri dön mainactivity' e
Intent returnIntent = new Intent();
setResult(RESULT_OK, returnIntent);
finish();
}
public void kayitEkle(String subject, String content) {
SQLiteDatabase db = notlarr.getWritableDatabase();
ContentValues line = new ContentValues();
line.put("subjects", subject);
line.put("contents", content);
db.insertOrThrow("notes", null, line);
}
});
}
}
[/code]
[code]
package com.omer.DBVeritabani;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class NotlarDatabase extends SQLiteOpenHelper {
static final int VERSION = 1;
static final String DATABASE_NAME = "notess.db";
public NotlarDatabase(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE notes (id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "subjects TEXT NOT NULL, contents TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST notes");
onCreate(db);
}
}
[/code]