我正在构建一个使用SQLite数据库来保存列表项的列表应用程序。列表项由UUID,名称和布尔值组成,以确定其是否已解决(数据库将其存储为int)。我注意到的是,当用户关闭任务并添加新任务时,将重新加载列表,以便现在取消选中已检查的任务。此外,重新启动应用程序时,将重新加载列表,以便检查所有已检查的任务。
现在我一直在思考这些想法,到目前为止我所做的是onBindViewHolder()方法中的TaskAdapter类,我调用setOnCheckedChangedListener(),以便当用户检查任务时吐司推出。我觉得我应该在这里做些什么来更新数据库中的特定任务,但我不知道如何。
附加说明:我使用单例类,我拥有所有数据库方法,例如添加任务,更新任务以及从数据库中检索所有任务。
ListFragment.java
public class ListFragment extends Fragment implements AddTaskDialog.addTask{
private static final String ADD_TASK_TAG = "ADD_TASK_DIALOG";
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.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(layoutManager);
updateUI();
return view;
}
private void updateUI(){
List<Task> tasks = TaskLab.getTaskLab(getContext()).getTaskList();
if(mAdapter == null) {
mAdapter = new TaskAdapter(tasks);
mRecyclerView.setAdapter(mAdapter);
}else{
mAdapter.setTaskList(tasks);
mAdapter.notifyDataSetChanged();
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.toolbar_layout,menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.add_task_button:
// Launch a dialog.
AddTaskDialog dialog = new AddTaskDialog();
FragmentManager manager = getFragmentManager();
dialog.setTargetFragment(ListFragment.this,1);
dialog.show(manager,ADD_TASK_TAG);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// Part of the addTask interface created in AddTaskDialog
@Override
public void addTask(String taskName) {
Task task = new Task();
task.setName(taskName);
TaskLab.getTaskLab(getContext()).addTask(task);
updateUI();
}
}
TaskAdapter.java
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskHolder> {
private List<Task> mTaskList;
public TaskAdapter(List<Task> tasks){
mTaskList = tasks;
}
@Override
public TaskAdapter.TaskHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_layout,parent,false);
return new TaskHolder(view);
}
@Override
public void onBindViewHolder(TaskAdapter.TaskHolder holder, int position) {
Task task = mTaskList.get(position);
holder.bindData(task);
holder.mTaskCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){
Toast.makeText(compoundButton.getContext(),"Task Solved!",Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public int getItemCount() {
return mTaskList.size();
}
public void setTaskList(List<Task> tasks) {
mTaskList = tasks;
}
public class TaskHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView mTaskName;
private CheckBox mTaskCheckBox;
public TaskHolder(View itemView) {
super(itemView);
mTaskName = itemView.findViewById(R.id.task_name);
mTaskCheckBox = itemView.findViewById(R.id.task_checkbox);
}
private void bindData(Task task) {
mTaskName.setText(task.getName());
mTaskCheckBox.setChecked(task.isSolved());
}
@Override
public void onClick(View view) {
}
}
}
TaskLab.java (Singleton类)
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 getTaskLab(Context context){
if(sTaskLab == null) {
sTaskLab = new TaskLab(context);
}
return sTaskLab;
}
// Add DataBase operation method here.
private ContentValues getContentValues(Task task) {
ContentValues values = new ContentValues();
values.put(TaskTable.Cols.UUID, task.getId().toString());
values.put(TaskTable.Cols.NAME, task.getName());
values.put(TaskTable.Cols.SOLVED, task.isSolved() ? 1 : 0);
return values;
}
public void addTask(Task task) {
ContentValues values = getContentValues(task);
mDatabase.insert(TaskTable.TABLE_NAME,null,values);
}
// Don't really understand this method
public void updateTask(Task task) {
String uuidString = task.getId().toString();
ContentValues values = getContentValues(task);
mDatabase.update(TaskTable.TABLE_NAME,values, TaskTable.Cols.UUID + " = ?",
new String[] {uuidString});
}
public TaskCursorWrapper queryTasks(String whereClause, String[] whereArgs) {
Cursor cursor = mDatabase.query(TaskTable.TABLE_NAME,null,whereClause,whereArgs,null,null,null);
return new TaskCursorWrapper(cursor);
}
public List<Task> getTaskList(){
List<Task> taskList = new ArrayList<>();
TaskCursorWrapper cursor = queryTasks(null,null);
try{
cursor.moveToFirst();
while(!cursor.isAfterLast()){
Task task = cursor.getTask();
taskList.add(task);
cursor.moveToNext();
}
}finally {
cursor.close();
}
return taskList;
}
答案 0 :(得分:0)
在适配器中,当您使用onclick时,当前项目将更新,但滚动时单击复选框的位置将不匹配。因为适配器只考虑你从你的代码写的位置。所以请使用任何getter / setter来避免这个问题。