I have a SQLite database, that includes, id, taskName, startMonth and EndMonth
while starting an activity I want to get the text from the button that initialized this activity and check if the text on button is the same as one of the taskName-s. if I found that one of the taskName-s store the same string as the string on the button, then I want to get respective row, that includes this taskName, and set row elements to views.
But I cannot retrieve data from database...
in DBHandler class I have ArrayList but I do not know how to retrieve from an array. then I watched a tutorial and I wrote a method
public Cursor getInfo(){
SQLiteDatabase db = this.getWritableDatabase();
String [] columns = {KEY_ID, KEY_NAME, KEY_START, KEY_END};
Cursor CR = db.query(TABLE_TASKS, columns, null, null, null, null, null);
return CR;
}
Here is a method that saves extra information about button text
private View.OnClickListener btnListener = new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getActivity(), AddGoalActivity.class);
switch (view.getId()) {
case R.id.button:
intent.putExtra("called", buttonAdd.getText().toString());
startActivityForResult(intent, BUTTON_1);
break;
} }
here is a method that saves the data to database, I deleted some extra lines that is not interesting now
public void onClick(View view) {
insertTask();
putTextOnButton();
break;
}
public void putTextOnButton () {
taskName = editText.getText().toString();
Intent i = new Intent(AddGoalActivity.this, MainActivity.class);
i.putExtra("Goal", taskName);
setResult(AddGoalActivity.RESULT_OK, i);
finish();
}
}
public void insertTask(){
dbHandler.getWritableDatabase();
Task t = new Task();
dbHandler.addTask(t);
dbHandler.close();
}
and here is a code in onCreate method where all these checks should take place
if (dbHandler.getTotalTasks() != 0) {
Cursor CR = dbHandler.getInfo();
String called_from = getIntent().getStringExtra("called");
CR.moveToFirst();
do {
taskName = CR.getString(1);
if (called_from.equals(taskName)) {
// get single task by its taskName element and set all task elements to views
Task t = dbHandler.getTask(called_from);
editText.setText(t.getTaskName());
}
} while (CR.moveToNext());
}
dbHandler.close();
Aucun commentaire:
Enregistrer un commentaire