I need help! I downloaded the open souce project git hub http://ift.tt/24Rg7Bg. It is a project that creates notes. when I add a note or I click the back button the app crashes and gives me the following error trying to requery an Already closed cursor android.database.sqlite.SQLiteCursor@b56f2d6 what can I do? thanks to all `` that will help me!
createnote
public class CreateNote extends Activity {
private static final String TAG = "notepad";
private Button addNoteToDB;
private EditText titleEditText;
private EditText contentEditText;
//this variable will inform us if user want to create a new note or just update
private boolean isEdit;
private DBHelper dbhelper;
private SQLiteDatabase db;
//variable will contain the title of editing note
private String editTitle;
private int id = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createlayout);
//binding views from layout to variable
//quite simple, huh ? :)
addNoteToDB = (Button) findViewById(R.id.addNoteToDB);
titleEditText = (EditText) findViewById(R.id.TitleEditText);
contentEditText = (EditText) findViewById(R.id.ContentEditText);
//initialization of DBHelper
//again :)
//it takes context as argument
dbhelper = new DBHelper(getApplicationContext());
//getting the intent
Intent mIntent = getIntent();
//if user is editting the note we bind the title of this note to editTitle variable
editTitle = mIntent.getStringExtra("title");
id = mIntent.getIntExtra("id", 0);
//we're getting the isEdit value
//if user is editing note, the value if true
//otherwise the default value is false
isEdit = mIntent.getBooleanExtra("isEdit", false);
//we're checking if user want to edit note
if(isEdit) {
Log.d(TAG, "isEdit");
//getting the readable database
db = dbhelper.getReadableDatabase();
Cursor c = dbhelper.getNote(db, id);
//closing db connection
db.close();
//here we're set title and content of note to editText views
titleEditText.setText(c.getString(0));
contentEditText.setText(c.getString(1));
//and we're changing the button text to something more appropriate
//from add note to update note
//you can change button text in /res/values/strings.xml file
addNoteToDB.setText(getResources().getString(R.string.updateNoteButton));
}
//setting listener for button
addNoteToDB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//when user clicks button
//we're grabbing the title and content from editText
String title = titleEditText.getText().toString();
String content = contentEditText.getText().toString();
//if user left title or content field empty
//we show the toast, and tell to user to fill the fields
if (title.equals("") || content.equals("")) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.validation), Toast.LENGTH_LONG).show();
return;
}
//adding note to db
if (!isEdit) {
//if it isn't edit mode we just add a new note to db
dbhelper = new DBHelper(getApplicationContext());
dbhelper.addNote(title, content);
//and finish the activity here
//so we came back to Simple_NotepadActivity
finish();
} else {
//if this is edit mode, we just update the old note
dbhelper.updateNote(title, content, editTitle);
//and the same finish activity
finish();
}
}
});
}
@Override
protected void onPause() {
super.onPause();
dbhelper.close();
}
onenote
public class OneNote extends Activity {
private static final String TAG = "notepad";
//our views for display note title and content
private TextView noteTitle;
private TextView createdAt;
private TextView noteContent;
//dbhelper
private DBHelper dbhelper;
private SQLiteDatabase db;
//default values for title and content variables
private String title = "defaultTitle";
private int id = 0;
private String content = "defaultContent";
private String date = "date";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.onenote);
//initialization of DBHelper
dbhelper = new DBHelper(getApplicationContext());
noteTitle = (TextView) findViewById(R.id.noteTitle);
noteContent = (TextView) findViewById(R.id.noteContent);
createdAt = (TextView) findViewById(R.id.createdAt);
// getting intent
Intent mIntent = getIntent();
id = mIntent.getIntExtra("id", 0);
//getting the readable database
db = dbhelper.getReadableDatabase();
//getting the note from database
Cursor c = dbhelper.getNote(db, id);
//closing the database connection
db.close();
//getting the content from cursor
//getString(1) because first column is noteTitle and second is noteContent and the third column is date
title = c.getString(0).toString();
content = c.getString(1).toString();
date = c.getString(2).toString();
//setting notes to our views
noteTitle.setText(title);
noteContent.setText(content);
createdAt.setText(date);
}
Aucun commentaire:
Enregistrer un commentaire