Я пытаюсь передать данные SQLite между двумя действиями и отобразить их в виде перезапуска. Я запускаю приложение, но оно останавливается. Как я могу сделать эту работу?
Деятельность:
База данных 1.SQL
DatabseHandler.java
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "tasksManager";
private static final String TABLE_TASKS = "tasks";
private static final String KEY_TASK_ID = "_id";
private static final String KEY_TASK_NAME = "name";
private static final String KEY_TASK_DATE = "date";
public DatabaseHandler(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TASKS_TABLE = "CREATE TABLE " + TABLE_TASKS + "(" + KEY_TASK_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
KEY_TASK_NAME + " TEXT," + KEY_TASK_DATE + " TEXT" + ")";
db.execSQL(CREATE_TASKS_TABLE);}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS tasks" + TABLE_TASKS);
onCreate(db);
}
public void addTask(Task task){
SQLiteDatabase db= this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TASK_NAME, task.getTaskName());
values.put(KEY_TASK_DATE, task.getTaskDate());
db.insert(TABLE_TASKS, null, values);
db.close();
}
public ArrayList<Task> getAllTasks(){
String[] columns = {
KEY_TASK_ID,
KEY_TASK_NAME,
KEY_TASK_DATE};
ArrayList<Task> taskList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_TASKS,
columns,
null, null, null, null, null);
if(cursor != null) {
cursor.moveToFirst();
do {
Task task = new Task(
parseInt(cursor.getString(0)),
cursor.getString(1),
cursor.getString(2));
taskList.add(task);
}
while (cursor.moveToNext());
}
cursor.close();
db.close();
return taskList;
}
2.Действие, откуда я хочу получить данные
AddTaskActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.ArrayList;
public class AddTaskActivity extends AppCompatActivity {
TextView addNewTask, setStartTime, setEndTime;
EditText addTaskName;
TimePicker timePicker1, timePicker2;
int hour1, minute1;
int hour2, minute2;
String theNewTask;
Button addTaskButton;
TasksListActivity tla;
DatabaseHandler dth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_task);
dth = new DatabaseHandler(this);
addNewTask = findViewById(R.id.addNewTask);
setStartTime = findViewById(R.id.setStartTime);
setEndTime = findViewById(R.id.setEndTime);
addTaskName = findViewById(R.id.addTaskName);
timePicker1 = findViewById(R.id.timePicker1);
timePicker2 = findViewById(R.id.timePicker2);
addTaskButton = findViewById(R.id.addTaskButton);
addTaskButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = addTaskName.getText().toString();
Intent intent = new Intent(AddTaskActivity.this, TasksListActivity.class);
Task task = new Task();
task.setTaskName(name);
task.setTaskDate("date");
dth.addTask(task);
intent.putExtra("name", name);
startActivity(intent);
}
}
);
}
}
3.Действие, в котором я хочу отобразить данные
TasksListActivity.java
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.widget.TextView;
import java.util.ArrayList;
public class TasksListActivity extends AppCompatActivity {
RecyclerView recyclerView;
TaskAdapter taskAdapter;
LinearLayoutManager layoutManager;
ArrayList<Task> tasksList;
DatabaseHandler dth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tasks_list);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.taskListItem:
Intent intent1 = new Intent(TasksListActivity.this, TasksListActivity.class);
startActivity(intent1);
break;
case R.id.addTasks:
Intent intent5 = new Intent(TasksListActivity.this, AddTaskActivity.class);
startActivity(intent5);
break;
case R.id.noteListItem:
Intent intent2 = new Intent(TasksListActivity.this, NotesListActivity.class);
startActivity(intent2);
break;
}
return true;
}
});
recyclerView = findViewById(R.id.recyclerView);
tasksList = new ArrayList<>();
taskAdapter = new TaskAdapter(tasksList, TasksListActivity.this);
layoutManager = new LinearLayoutManager(getApplicationContext());
dth = new DatabaseHandler(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(taskAdapter);
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
getDataFromSQLiteDatabase();
}
private void getDataFromSQLiteDatabase(){
new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... params){
tasksList.clear();
tasksList.addAll(dth.getAllTasks());
return null;
}
@Override
protected void onPostExecute(Void aVoid){
super.onPostExecute(aVoid);
taskAdapter.notifyDataSetChanged();
}
}.execute();
}
}
Редактировать: я изменил Bundle bundle = getIntent (). GetExtras ();
String name = bundle.getString ("name");
в
String name = getIntent (). GetStringExtra ("name");
и Logcat показывает эту ошибку:
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:468)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.android.todolistapp.DatabaseHandler.getAllTasks(DatabaseHandler.java:182)
at com.example.android.todolistapp.TasksListActivity$2.doInBackground(TasksListActivity.java:74)
at com.example.android.todolistapp.TasksListActivity$2.doInBackground(TasksListActivity.java:70)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)