gpt4 book ai didi

java - 使用 SQLite 数据库保存 CheckBox 状态

转载 作者:太空宇宙 更新时间:2023-11-04 10:39:12 25 4
gpt4 key购买 nike

我正在构建一个列表应用程序,它使用 SQLite 数据库来保存列表项。列表项由 UUID、名称和 boolean 值组成,用于确定其是否已解决(数据库将其存储为 int)。我注意到,当用户检查任务并添加新任务时,列表将重新加载,以便检查的任务现在取消检查。此外,当应用程序重新启动时,列表将重新加载,因此所有已选中的任务都将变为未选中状态。

现在我一直在思考想法,到目前为止我所做的是在我的 TaskAdapter 类中的 onBindViewHolder() 方法中调用 setOnCheckedChangedListener() ,以便当用户检查任务时启动 toast。我觉得我应该在这里做一些事情来更新数据库中的特定任务,但我不知道如何做。

附加说明:我使用的是单例类,其中包含所有数据库方法,例如添加任务、更新任务以及从数据库中检索所有任务。

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(单例类)

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;
}

最佳答案

在适配器中,当您使用onclick时,当前项目将更新,但滚动时单击的复选框的位置将不匹配。因为适配器只考虑您从代码中编写的位置。因此,请使用任何 getter/setter 来避免此问题。

关于java - 使用 SQLite 数据库保存 CheckBox 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49185922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com