在RecyclerView中显示SQLite数据

时间:2018-07-07 14:50:30

标签: android android-recyclerview android-sqlite

我有一个RecyclerView,它显示用户输入的2条文本。我正在使用SQL数据库存储这些输入,然后将它们永久显示在RecyclerView上。问题是它不会永久保留在recyclerview中。当我单击recyclerview时,它将带我进行所需的另一项活动。但是当我返回时,整个数据库消失了,并且recyclerview为空。

任何想法为什么会发生这种情况以及如何解决?如何使sql数据库永久显示在recyclerview上?

我们将不胜感激,在此先感谢您。

public class ExerciseRoutine extends Fragment implements 
ExerciseRoutine_Dialog.RoutineDialogListener{

private ArrayList<ExerciseRoutine_Information> Routine_information = new ArrayList<>();

private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;

@Override
public void sendInput(String name, String split) {
 //Receives the user input from a dialogfragment and stores it into Arraylist
    Routine_information.add(new ExerciseRoutine_Information(name, split));

    adapter = new ExerciseRoutineAdapter(getContext(), Routine_information);

//AsynTask that adds the user input into the sql database
    BackendTask_Eroutine backendTask_add = new BackendTask_Eroutine(getContext());
    backendTask_add.execute("add_eRoutine", name, split);

    adapter.notifyItemInserted(Routine_information.size());
    recyclerView.setAdapter(adapter);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.exercise_routine_fragment, container, 
    setHasOptionsMenu(true);

    //AsyncTask that displays user inputs in recyclerview
    BackendTask_Eroutine backendTask_display = new BackendTask_Eroutine(getContext());
    Routine_information = backendTask_display.getRoutineList();
    backendTask_display.execute("display_eRoutine");

    recyclerView = view.findViewById(R.id.ExerciseRoutine_Recycler);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(getActivity());
    adapter = new ExerciseRoutineAdapter(getContext(), Routine_information);

    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);

    return view;
}

AsychTask:负责添加输入和显示输入

 public class BackendTask_Eroutine extends AsyncTask<String, 
 ExerciseRoutine_Information, String> {

private Context mContext;

private ArrayList<ExerciseRoutine_Information> eRoutineData = new ArrayList<>();

public BackendTask_Eroutine(Context context) {
    this.mContext = context;
}

public ArrayList<ExerciseRoutine_Information> getRoutineList() {
    return eRoutineData;
}

@Override
protected String doInBackground(String... strings) {

    ExerciseRoutine_DBHandler dbHandler = new ExerciseRoutine_DBHandler(mContext);

    String method = strings[0];

    if(method.equals("add_eRoutine")){
        String name = strings[1];
        String split = strings[2];

        SQLiteDatabase db = dbHandler.getWritableDatabase();
        dbHandler.insertData(db, name, split);

        return "One Row Inserted...";
    }

    if(method.equals("display_eRoutine")){

        SQLiteDatabase db = dbHandler.getReadableDatabase();
        Cursor cursor = dbHandler.getData(db);

        String name, split;

        while(cursor.moveToNext()){
            name = cursor.getString(cursor.getColumnIndex(ExerciseRoutine_DBHandler.NAME));
            split = cursor.getString(cursor.getColumnIndex(ExerciseRoutine_DBHandler.SPLIT));

            ExerciseRoutine_Information info = new ExerciseRoutine_Information(name, split);

            publishProgress(info);
        }
        return "Display eRoutine";
    }

    return null;
}

@Override
protected void onProgressUpdate(ExerciseRoutine_Information... values) {
    eRoutineData.add(values[0]);
}

1 个答案:

答案 0 :(得分:0)

您可能可以使用CursorAdapter来填充RecyclerView。

在制作完API后很快获得数据后,将其存储到sqlitedatabase中。之后,您可以查询数据库中Cursor形式的数据,然后将其传递给CursorAdapter来填充RecyclerVIew。

enter image description here

获取游标

一旦定义了数据库和表,我们就可以通过使用rawQuery查询数据库来访问Cursor:

// TodoDatabaseHandler is a SQLiteOpenHelper class connecting to SQLite
TodoDatabaseHandler handler = new TodoDatabaseHandler(this);
// Get access to the underlying writeable database
SQLiteDatabase db = handler.getWritableDatabase();
// Query for items from the database and get a cursor back
Cursor todoCursor = db.rawQuery("SELECT  * FROM todo_items", null);

将适配器连接到RecyclerView

// Find ListView to populate
RecyclerView lvItems = (RecyclerView) findViewById(R.id.lvItems);
// Setup cursor adapter using cursor from last step
TodoCursorAdapter todoAdapter = new TodoCursorAdapter(this, todoCursor);
// Attach cursor adapter to the ListView 
lvItems.setAdapter(todoAdapter);

定义适配器

public abstract class TodoCursorAdapter <VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {         

    private Cursor mCursor;

    public TodoCursorAdapter (Cursor cursor) {
        this(cursor, null);
    }

}

Here是一个不错的博客,您可以阅读该博客以加深了解。

希望这行得通。