如何在两个活动之间传递数据并将其显示在recyclerview中?

时间:2019-05-14 14:34:35

标签: android database sqlite android-recyclerview

我尝试在2个活动之间传递SQLite数据并将其显示在recyclerview中。我运行了该应用程序,但它停止了。我该如何工作?

活动:

1.SQLite数据库

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();                 字符串名称= bundle.getString(“ 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)

2 个答案:

答案 0 :(得分:1)

只需替换

Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");

与此 String name =getIntent().getStringExtras("name")

答案 1 :(得分:0)

问题

您的问题是您不检查游标是否有任何行并且没有任何行。

更具体地说,从任何返回Cursor的SQLiteDatabase方法返回的Cursor不会为null。检查Cursor是否为null是没有用的,因为Cursor永远不会为null。

因此, moveToFirst 随后无法移至一行并返回false。不检查是否为假,因此您收到消息Index 0 requested, with a size of 0索引0 是该列(因此,您需要从偏移量为0的列中请求数据,可以)。 大小为0 表示游标中的行数为0 ,因此没有要检索的数据。

修复

因此,您需要删除该行,然后进行适当的检查以查看是否可以移至一行。幸运的是,move方法(moveToFirst,moveToNext .....)返回一个布尔值,指示移动是否成功。

  • 一种替代方法是使用Cursor的getCount方法,以查看Cursor中是否有任何行。

因此,我建议使用:-

while (cursor.moveToNext() {
    Task task = new Task(
             parseInt(cursor.getString(0)),
             cursor.getString(1),
             cursor.getString(2));
    taskList.add(task);
}

代替:-

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());

}

重要

但是,因为上面的操作失败了……...... 大小为0 ,并且您没有选择标准,所以任务表格为空。