为什么我必须重新启动我的RecyclerView应用程序才能显示来自SQLite的数据?

时间:2018-02-23 22:34:58

标签: java android sqlite android-recyclerview

编辑1 :我使用了调试语句,事实证明,当我向数据库添加项目时,它是成功的。但是,不调用updateUI()方法,因为我正在启动的是DialogFragment(),因此我的ListActivity不会进入暂停状态。但是,如果我退出应用程序并再次启动,则适配器会更新。那么现在的问题是如何在退出对话框片段后更新适配器

我正在构建一个简单的列表应用程序,并添加了SQLite数据库来存储任务对象。我成功地将项目添加到数据库,但它们不会立即显示在我用来显示所有任务的RecyclerView中。只有当我完全退出应用程序并重新加载任务项目才会显示在RecyclerView中。

ListFragment.java这将创建RecyclerView

public class ListFragment extends Fragment {

private static final String DEBUG_LOG = "debug_message";
private static final String ADD_DIALOG = "add_dialog";
private static final int REQUEST_TITLE = 0;
private RecyclerView mRecyclerView;
private TaskAdapter mAdapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_list, container, false);

    mRecyclerView = view.findViewById(R.id.the_task_list);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(linearLayoutManager);

    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerView.getContext(), linearLayoutManager.getOrientation());
    mRecyclerView.addItemDecoration(dividerItemDecoration);

    updateUI();

    return view;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == Activity.RESULT_OK && requestCode == ListFragment.REQUEST_TITLE) {

        UUID taskId = (UUID) data.getSerializableExtra(EditTaskFragment.EXTRA_TASK_ID);
        String newTaskName = data.getStringExtra(EditTaskFragment.EXTRA_TASK_TITLE);

        if(newTaskName != null) {
            Task task = TaskLab.get(getActivity()).getTask(taskId);
            task.setTitle(newTaskName);
            TaskLab.get(getActivity()).updateTask(task);
        }

        updateUI();
    }

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_list_item, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()) {
        case R.id.add_new_task:
            FragmentManager manager = getFragmentManager();
            AddTaskFragment dialog = new AddTaskFragment();
            dialog.show(manager, ADD_DIALOG);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}


@Override
public void onResume() {
    super.onResume();
    updateUI();
}


public void updateUI() {

    // Retrieves ArrayList<Task>
    List<Task> taskList = TaskLab.get(getActivity()).getTaskList();

    if(mAdapter == null) {
        mAdapter = new TaskAdapter(taskList);
        mRecyclerView.setAdapter(mAdapter);

    }else {
        mAdapter.setCrimes(taskList);
        mAdapter.notifyDataSetChanged();
    }
}

private class TaskAdapter extends RecyclerView.Adapter<TaskHolder>{

    private List<Task> mTaskList;

    public TaskAdapter(List<Task> taskList) {

        mTaskList = taskList;
    }

    @Override
    public TaskHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.task_list_layout, parent, false);
        return new TaskHolder(view);
    }

    @Override
    public void onBindViewHolder(TaskHolder holder, int position) {
        final Task currentTask = mTaskList.get(position);
        holder.bindData(currentTask);

        final TextView taskTitle = holder.mTaskTitle;

        holder.mSolved.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
               if(isChecked) {
                   taskTitle.setPaintFlags(taskTitle.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                   currentTask.setSolved(true);
               }else {
                   taskTitle.setPaintFlags(taskTitle.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
               }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mTaskList.size();
    }

    public void setCrimes(List<Task> taskList) {
        mTaskList = taskList;
    }
}

private class TaskHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    private Task mTask;
    private TextView mTaskTitle;
    private CheckBox mSolved;

    private static final String EDIT_DIALOG = "edit_dialog";

    public TaskHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);

        mTaskTitle = itemView.findViewById(R.id.task_title);
        mSolved = itemView.findViewById(R.id.task_solved);
    }

    public void bindData(Task task) {
        mTask = task;
        mTaskTitle.setText(mTask.getTitle());
        mSolved.setChecked(mTask.isSolved());
    }

    @Override
    public void onClick(View view) {
        if(!mSolved.isChecked()) {
            FragmentManager manager = getFragmentManager();
            EditTaskFragment dialog = EditTaskFragment.newInstance(mTask.getId());
            dialog.setTargetFragment(ListFragment.this, REQUEST_TITLE);
            dialog.show(manager, EDIT_DIALOG);
        }
    }

}

AddTaskFragment.java这将允许用户将任务项添加到RecyclerView

public class AddTaskFragment extends DialogFragment {

private EditText mTaskTitle;

private void addTask() {

    if(!mTaskTitle.getText().toString().equals("")) {
        Task task = new Task();
        task.setTitle(mTaskTitle.getText().toString());

        TaskLab.get(getActivity()).addTask(task);
    }
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    //return super.onCreateDialog(savedInstanceState);

    View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_task_dialog,null);
    mTaskTitle = view.findViewById(R.id.task_title);

    return new AlertDialog.Builder(getActivity())
            .setView(view)
            .setTitle(R.string.add_new_task_dialog_title)
            .setPositiveButton(R.string.add_new_task_dialog_positive, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    addTask();
                }
            })
            .create();
}
}

TaskLab.java这是一个单例类

public class TaskLab {

private static TaskLab sTaskLab;

private Context mContext;
private SQLiteDatabase mDatabase;

private TaskLab(Context context){
    mContext = context.getApplicationContext();
    mDatabase = new TaskBaseHelper(mContext).getWritableDatabase();
}

public static TaskLab get(Context context) {
    if(sTaskLab == null) {
        sTaskLab = new TaskLab(context);
    }

    return sTaskLab;
}


public List<Task> getTaskList(){

    List<Task> taskList = new ArrayList<Task>();
    TaskCursorWrapper cursor = queryTasks(null, null);

    // If there is something in the table query it
    try {
        cursor.moveToFirst();
        while(!cursor.isAfterLast()) {
            Task task = cursor.getTask();
            taskList.add(task);
            cursor.moveToNext();
        }
    } finally {
        cursor.close();
    }
    return taskList;
}

public void addTask(Task task) {
    ContentValues values = getContentValues(task);
    mDatabase.insert(TaskTable.NAME, null, values);
}

public void updateTask(Task task) {
    ContentValues values = getContentValues(task);
    String uuidString = task.getId().toString();
    mDatabase.update(TaskTable.NAME,values, TaskTable.Cols.UUID + " = ?", new String[] {uuidString});
}

public Task getTask(UUID id){

    TaskCursorWrapper cursor = queryTasks(TaskTable.Cols.UUID + " = ?",
            new String[] {id.toString()});

    try {
        if(cursor.getCount() == 0) {
            return null;
        }

        cursor.moveToFirst();
        return cursor.getTask();
    } finally {
        cursor.close();
    }
}

private static ContentValues getContentValues(Task task) {
    ContentValues values = new ContentValues();

    values.put(TaskTable.Cols.UUID, task.getId().toString());
    values.put(TaskTable.Cols.TITLE, task.getTitle());
    values.put(TaskTable.Cols.SOLVED, task.isSolved());

    return values;
}

private TaskCursorWrapper queryTasks(String whereClause, String[] whereArgs) {
    Cursor cursor = mDatabase.query(TaskTable.NAME,
            null,
            whereClause,
            whereArgs,
            null,
            null,
            null);
    return new TaskCursorWrapper(cursor);
}

}

2 个答案:

答案 0 :(得分:0)

每次从头开始都必须在updateUI中重新创建适配器

答案 1 :(得分:0)

尝试这样,看看。在updateUI()之前的片段的onCreateView()中添加下面的代码行。

注意:将taskList创建为全局变量。

mRecyclerView = view.findViewById(R.id.the_task_list);
mAdapter = new TaskAdapter(taskList);
mRecyclerView.setAdapter(mAdapter);

updateUI();

之后创建updateUI()方法,如下所示

public void updateUI() {
    taskList.clear;
    taskList = TaskLab.get(getActivity()).getTaskList();
    mAdapter.notifyDataSetChanged();
}

<强>更新

根据您更新的问题,将ListFragment中的一个全局变量声明为

public static ListFragment listFragment;

在膨胀视图后,在ListFragment的onCreate方法中添加以下代码行。

// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_list, container, false);

listFragment = this; 

然后在AddTaskFragment中更改addTask方法,如下所示。

private void addTask() {
    if(!mTaskTitle.getText().toString().equals("")) {
        Task task = new Task();
        task.setTitle(mTaskTitle.getText().toString());

        TaskLab.get(getActivity()).addTask(task);

        //import listfragment from your ListFragment
        listFragment.updateUI();
    }
}