gpt4 book ai didi

java - 实现 SQLite 数据库后,我的 Recyclerview 列表停止工作

转载 作者:行者123 更新时间:2023-12-02 12:29:14 25 4
gpt4 key购买 nike

我正在构建一个待办事项列表应用程序。我正在学习 SQLite 数据库,并尝试将用户输入保存到 SQLite 数据库表中,并将其显示在我的 recyclerview 列表中。当我在没有 SQLite 的情况下执行此操作时,它起作用了,因此问题显然出在我对数据库表的实现上。该应用程序应该在每次单击按钮时将数据添加到列表中:

addingItems.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(itemsInput.getText() != null){
items.add(new todo(itemsInput.getText().toString()));
itemsInput.setText("");
}else {
Toast.makeText(MainActivity.this, "Please enter something to do", Toast.LENGTH_SHORT).show();
}
itemAdapter.notifyDataSetChanged();
}
});

但现在它不这样做了。该应用程序不会崩溃。问题是我的列表完全没有显示任何数据。以下是我的相关Java文件:

MainActivity.java

public class MainActivity extends AppCompatActivity {

private SQLiteDatabase sqLiteDatabase;
private List<todo> items = new ArrayList<>();
private ItemAdapter itemAdapter;
private RecyclerView listItemsRecyclerView;
EditText itemsInput;
Button addingItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

itemsInput = (EditText)findViewById(R.id.to_do_editText);
addingItems = (Button)findViewById(R.id.to_do_btn);
listItemsRecyclerView = (RecyclerView) findViewById(R.id.to_do_list);

ToDoListDatabaseHelper databaseHelper = new ToDoListDatabaseHelper(this);
sqLiteDatabase = databaseHelper.getWritableDatabase();

items = new ArrayList<>();
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
listItemsRecyclerView.setLayoutManager(layoutManager);
listItemsRecyclerView.setItemAnimator(new DefaultItemAnimator());;
itemAdapter = new ItemAdapter(items);
listItemsRecyclerView.setAdapter(itemAdapter);

new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

}
});

addingItems.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(itemsInput.getText().length() > 0){
addNewToDo(itemsInput.getText().toString());
itemsInput.setText("");
}else {
Toast.makeText(MainActivity.this, "Please enter something to do", Toast.LENGTH_SHORT).show();
}
itemAdapter.notifyDataSetChanged();
}
});
}

private List<todo> getAllToDos(){
Cursor cursor = sqLiteDatabase.rawQuery("select * from " + ToDoContract.ToDoEntry.TABLE_NAME, new String[]{});
List<todo> todos = new ArrayList<>();
todo todo;
if (cursor != null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {

String name = cursor.getString(cursor.getColumnIndex(ToDoContract.ToDoEntry.COLUMN_TODO_NAME));
int priority = cursor.getInt(cursor.getColumnIndex(ToDoContract.ToDoEntry.COLUMN_TODO_PRIORITY));
int timestamp = cursor.getInt(cursor.getColumnIndex(ToDoContract.ToDoEntry.COLUMN_TODO_TIMESTAMP));
todo = new todo(name);
todos.add(todo);
} while (cursor.moveToNext());
}
cursor.close();
}
return todos;
}

private long addNewToDo(String name){
ContentValues cv = new ContentValues();
cv.put(ToDoContract.ToDoEntry.COLUMN_TODO_NAME, name);
return sqLiteDatabase.insert(ToDoContract.ToDoEntry.TABLE_NAME, null, cv);
}
}

我的自定义适配器:

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {

private List<todo> todoList;

public class ViewHolder extends RecyclerView.ViewHolder {
public TextView toDoTextView;

public ViewHolder(View itemView) {
super(itemView);

toDoTextView = (TextView) itemView.findViewById(R.id.to_do);
}
}

public ItemAdapter(List<todo> todoList) {
this.todoList = todoList;
}

@Override
public ItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(ItemAdapter.ViewHolder holder, int position) {
todo toDo = todoList.get(position);
holder.toDoTextView.setText(toDo.getToDo());
}


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

我对我的 SQLite 类运行了单元测试,因此它们没有问题。

最佳答案

您不应该使用Cursor作为 Adapter 的数据源.为什么不从游标中获取数据(todos)然后将数据放入 Adapter ?我们知道,在数据读取结束时,光标应该CLOSED。你应该这样编码:

private List<todo> getAllToDos() {
Cursor cursor = sqLiteDatabase.rawQuery("select * from " + ToDoContract.ToDoEntry.TABLE_NAME, new String[]{});
List<todo> todos = new ArrayList<>();
todo todo;
if (cursor != null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {

String name = cursor.getString(cursor.getColumnIndex(ToDoContract.ToDoEntry.COLUMN_TODO_NAME));
int priority = cursor.getInt(cursor.getColumnIndex(ToDoContract.ToDoEntry.COLUMN_TODO_PRIORITY));
int timestamp = cursor.getInt(cursor.getColumnIndex(ToDoContract.ToDoEntry.COLUMN_TODO_TIMESTAMP));
todo = new todo(name);
todo.setPriority(priority);
todo.setTimestamp(timestamp);
todos.add(todo);
} while (cursor.moveToNext());
}
cursor.close();
}
return todos;
}

还有你的ItemAdapter会更简单。

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {

private List<todo> todoList;

public class ViewHolder extends RecyclerView.ViewHolder {
public TextView toDoTextView;

public ViewHolder(View itemView) {
super(itemView);

toDoTextView = (TextView) itemView.findViewById(R.id.to_do);
}
}

public ItemAdapter(List<todo> todoList) {
this.todoList = todoList;
}

@Override
public ItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(ItemAdapter.ViewHolder holder, int position) {
todo toDo = todoList.get(position);
holder.toDoTextView.setText(toDo.getToDo());
}


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

还有一些其他更改。但我认为您可以按照我上面告诉您的方式修复它们。如果您对此有任何其他问题,请告诉我。我会帮助您。

关于java - 实现 SQLite 数据库后,我的 Recyclerview 列表停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45363402/

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